1.0.43.82: Make compiling a MAKE-ARRAY call with a bad keyword list signal warning
[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 ;;;; utilities for optimizing array operations
15
16 ;;; Return UPGRADED-ARRAY-ELEMENT-TYPE for LVAR, or do
17 ;;; GIVE-UP-IR1-TRANSFORM if the upgraded element type can't be
18 ;;; determined.
19 (defun upgraded-element-type-specifier-or-give-up (lvar)
20   (let ((element-type-specifier (upgraded-element-type-specifier lvar)))
21     (if (eq element-type-specifier '*)
22         (give-up-ir1-transform
23          "upgraded array element type not known at compile time")
24         element-type-specifier)))
25
26 (defun upgraded-element-type-specifier (lvar)
27   (type-specifier (array-type-upgraded-element-type (lvar-type lvar))))
28
29 ;;; Array access functions return an object from the array, hence its type is
30 ;;; going to be the array upgraded element type. Secondary return value is the
31 ;;; known supertype of the upgraded-array-element-type, if if the exact
32 ;;; U-A-E-T is not known. (If it is NIL, the primary return value is as good
33 ;;; as it gets.)
34 (defun array-type-upgraded-element-type (type)
35   (typecase type
36     ;; Note that this IF mightn't be satisfied even if the runtime
37     ;; value is known to be a subtype of some specialized ARRAY, because
38     ;; we can have values declared e.g. (AND SIMPLE-VECTOR UNKNOWN-TYPE),
39     ;; which are represented in the compiler as INTERSECTION-TYPE, not
40     ;; array type.
41     (array-type
42      (values (array-type-specialized-element-type type) nil))
43     ;; Deal with intersection types (bug #316078)
44     (intersection-type
45      (let ((intersection-types (intersection-type-types type))
46            (element-type *wild-type*)
47            (element-supertypes nil))
48        (dolist (intersection-type intersection-types)
49          (multiple-value-bind (cur-type cur-supertype)
50              (array-type-upgraded-element-type intersection-type)
51            ;; According to ANSI, an array may have only one specialized
52            ;; element type - e.g. '(and (array foo) (array bar))
53            ;; is not a valid type unless foo and bar upgrade to the
54            ;; same element type.
55            (cond
56              ((eq cur-type *wild-type*)
57               nil)
58              ((eq element-type *wild-type*)
59               (setf element-type cur-type))
60              ((or (not (csubtypep cur-type element-type))
61                   (not (csubtypep element-type cur-type)))
62               ;; At least two different element types where given, the array
63               ;; is valid iff they represent the same type.
64               ;;
65               ;; FIXME: TYPE-INTERSECTION already takes care of disjoint array
66               ;; types, so I believe this code should be unreachable. Maybe
67               ;; signal a warning / error instead?
68               (setf element-type *empty-type*)))
69            (push (or cur-supertype (type-*-to-t cur-type))
70                  element-supertypes)))
71        (values element-type
72                (when (and (eq *wild-type* element-type) element-supertypes)
73                  (apply #'type-intersection element-supertypes)))))
74     (union-type
75      (let ((union-types (union-type-types type))
76            (element-type nil)
77            (element-supertypes nil))
78        (dolist (union-type union-types)
79          (multiple-value-bind (cur-type cur-supertype)
80              (array-type-upgraded-element-type union-type)
81            (cond
82              ((eq element-type *wild-type*)
83               nil)
84              ((eq element-type nil)
85               (setf element-type cur-type))
86              ((or (eq cur-type *wild-type*)
87                   ;; If each of the two following tests fail, it is not
88                   ;; possible to determine the element-type of the array
89                   ;; because more than one kind of element-type was provided
90                   ;; like in '(or (array foo) (array bar)) although a
91                   ;; supertype (or foo bar) may be provided as the second
92                   ;; returned value returned. See also the KLUDGE below.
93                   (not (csubtypep cur-type element-type))
94                   (not (csubtypep element-type cur-type)))
95               (setf element-type *wild-type*)))
96            (push (or cur-supertype (type-*-to-t cur-type))
97                  element-supertypes)))
98        (values element-type
99                (when (eq *wild-type* element-type)
100                  (apply #'type-union element-supertypes)))))
101     (t
102      ;; KLUDGE: there is no good answer here, but at least
103      ;; *wild-type* won't cause HAIRY-DATA-VECTOR-{REF,SET} to be
104      ;; erroneously optimized (see generic/vm-tran.lisp) -- CSR,
105      ;; 2002-08-21
106      (values *wild-type* nil))))
107
108 (defun array-type-declared-element-type (type)
109   (if (array-type-p type)
110       (array-type-element-type type)
111       *wild-type*))
112
113 ;;; The ``new-value'' for array setters must fit in the array, and the
114 ;;; return type is going to be the same as the new-value for SETF
115 ;;; functions.
116 (defun assert-new-value-type (new-value array)
117   (let ((type (lvar-type array)))
118     (when (array-type-p type)
119       (assert-lvar-type
120        new-value
121        (array-type-specialized-element-type type)
122        (lexenv-policy (node-lexenv (lvar-dest new-value))))))
123   (lvar-type new-value))
124
125 ;;; Return true if ARG is NIL, or is a constant-lvar whose
126 ;;; value is NIL, false otherwise.
127 (defun unsupplied-or-nil (arg)
128   (declare (type (or lvar null) arg))
129   (or (not arg)
130       (and (constant-lvar-p arg)
131            (not (lvar-value arg)))))
132
133 (defun supplied-and-true (arg)
134   (and arg
135        (constant-lvar-p arg)
136        (lvar-value arg)
137        t))
138 \f
139 ;;;; DERIVE-TYPE optimizers
140
141 ;;; Array operations that use a specific number of indices implicitly
142 ;;; assert that the array is of that rank.
143 (defun assert-array-rank (array rank)
144   (assert-lvar-type
145    array
146    (specifier-type `(array * ,(make-list rank :initial-element '*)))
147    (lexenv-policy (node-lexenv (lvar-dest array)))))
148
149 (defun derive-aref-type (array)
150   (multiple-value-bind (uaet other)
151       (array-type-upgraded-element-type (lvar-type array))
152     (or other uaet)))
153
154 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
155   (assert-array-rank array (length indices))
156   *universal-type*)
157
158 (deftransform array-in-bounds-p ((array &rest subscripts))
159   (flet ((give-up ()
160            (give-up-ir1-transform
161             "~@<lower array bounds unknown or negative and upper bounds not ~
162              negative~:@>"))
163          (bound-known-p (x)
164            (integerp x))) ; might be NIL or *
165     (block nil
166       (let ((dimensions (array-type-dimensions-or-give-up
167                          (lvar-conservative-type array))))
168         ;; shortcut for zero dimensions
169         (when (some (lambda (dim)
170                       (and (bound-known-p dim) (zerop dim)))
171                     dimensions)
172           (return nil))
173         ;; we first collect the subscripts LVARs' bounds and see whether
174         ;; we can already decide on the result of the optimization without
175         ;; even taking a look at the dimensions.
176         (flet ((subscript-bounds (subscript)
177                  (let* ((type (lvar-type subscript))
178                         (low (numeric-type-low type))
179                         (high (numeric-type-high type)))
180                    (cond
181                      ((and (or (not (bound-known-p low)) (minusp low))
182                            (or (not (bound-known-p high)) (not (minusp high))))
183                       ;; can't be sure about the lower bound and the upper bound
184                       ;; does not give us a definite clue either.
185                       (give-up))
186                      ((and (bound-known-p high) (minusp high))
187                       (return nil))     ; definitely below lower bound (zero).
188                      (t
189                       (cons low high))))))
190           (let* ((subscripts-bounds (mapcar #'subscript-bounds subscripts))
191                  (subscripts-lower-bound (mapcar #'car subscripts-bounds))
192                  (subscripts-upper-bound (mapcar #'cdr subscripts-bounds))
193                  (in-bounds 0))
194             (mapcar (lambda (low high dim)
195                       (cond
196                         ;; first deal with infinite bounds
197                         ((some (complement #'bound-known-p) (list low high dim))
198                          (when (and (bound-known-p dim) (bound-known-p low) (<= dim low))
199                            (return nil)))
200                         ;; now we know all bounds
201                         ((>= low dim)
202                          (return nil))
203                         ((< high dim)
204                          (aver (not (minusp low)))
205                          (incf in-bounds))
206                         (t
207                          (give-up))))
208                     subscripts-lower-bound
209                     subscripts-upper-bound
210                     dimensions)
211             (if (eql in-bounds (length dimensions))
212                 t
213                 (give-up))))))))
214
215 (defoptimizer (aref derive-type) ((array &rest indices) node)
216   (assert-array-rank array (length indices))
217   (derive-aref-type array))
218
219 (defoptimizer (%aset derive-type) ((array &rest stuff))
220   (assert-array-rank array (1- (length stuff)))
221   (assert-new-value-type (car (last stuff)) array))
222
223 (macrolet ((define (name)
224              `(defoptimizer (,name derive-type) ((array index))
225                 (derive-aref-type array))))
226   (define hairy-data-vector-ref)
227   (define hairy-data-vector-ref/check-bounds)
228   (define data-vector-ref))
229
230 #!+(or x86 x86-64)
231 (defoptimizer (data-vector-ref-with-offset derive-type) ((array index offset))
232   (derive-aref-type array))
233
234 (macrolet ((define (name)
235              `(defoptimizer (,name derive-type) ((array index new-value))
236                 (assert-new-value-type new-value array))))
237   (define hairy-data-vector-set)
238   (define hairy-data-vector-set/check-bounds)
239   (define data-vector-set))
240
241 #!+(or x86 x86-64)
242 (defoptimizer (data-vector-set-with-offset derive-type) ((array index offset new-value))
243   (assert-new-value-type new-value array))
244
245 ;;; Figure out the type of the data vector if we know the argument
246 ;;; element type.
247 (defun derive-%with-array-data/mumble-type (array)
248   (let ((atype (lvar-type array)))
249     (when (array-type-p atype)
250       (specifier-type
251        `(simple-array ,(type-specifier
252                         (array-type-specialized-element-type atype))
253                       (*))))))
254 (defoptimizer (%with-array-data derive-type) ((array start end))
255   (derive-%with-array-data/mumble-type array))
256 (defoptimizer (%with-array-data/fp derive-type) ((array start end))
257   (derive-%with-array-data/mumble-type array))
258
259 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
260   (assert-array-rank array (length indices))
261   *universal-type*)
262
263 (defoptimizer (row-major-aref derive-type) ((array index))
264   (derive-aref-type array))
265
266 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
267   (assert-new-value-type new-value array))
268
269 (defoptimizer (make-array derive-type)
270               ((dims &key initial-element element-type initial-contents
271                 adjustable fill-pointer displaced-index-offset displaced-to))
272   (let* ((simple (and (unsupplied-or-nil adjustable)
273                       (unsupplied-or-nil displaced-to)
274                       (unsupplied-or-nil fill-pointer)))
275          (spec
276           (or `(,(if simple 'simple-array 'array)
277                  ,(cond ((not element-type) t)
278                         ((constant-lvar-p element-type)
279                          (let ((ctype (careful-specifier-type
280                                        (lvar-value element-type))))
281                            (cond
282                              ((or (null ctype) (unknown-type-p ctype)) '*)
283                              (t (sb!xc:upgraded-array-element-type
284                                  (lvar-value element-type))))))
285                         (t
286                          '*))
287                  ,(cond ((constant-lvar-p dims)
288                          (let* ((val (lvar-value dims))
289                                 (cdims (if (listp val) val (list val))))
290                            (if simple
291                                cdims
292                                (length cdims))))
293                         ((csubtypep (lvar-type dims)
294                                     (specifier-type 'integer))
295                          '(*))
296                         (t
297                          '*)))
298               'array)))
299     (if (and (not simple)
300              (or (supplied-and-true adjustable)
301                  (supplied-and-true displaced-to)
302                  (supplied-and-true fill-pointer)))
303         (careful-specifier-type `(and ,spec (not simple-array)))
304         (careful-specifier-type spec))))
305 \f
306 ;;;; constructors
307
308 ;;; Convert VECTOR into a MAKE-ARRAY.
309 (define-source-transform vector (&rest elements)
310   `(make-array ,(length elements) :initial-contents (list ,@elements)))
311
312 ;;; Just convert it into a MAKE-ARRAY.
313 (deftransform make-string ((length &key
314                                    (element-type 'character)
315                                    (initial-element
316                                     #.*default-init-char-form*)))
317   `(the simple-string (make-array (the index length)
318                        :element-type element-type
319                        ,@(when initial-element
320                            '(:initial-element initial-element)))))
321
322 (defun rewrite-initial-contents (rank initial-contents env)
323   (if (plusp rank)
324       (if (and (consp initial-contents)
325                (member (car initial-contents) '(list vector sb!impl::backq-list)))
326           `(list ,@(mapcar (lambda (dim)
327                              (rewrite-initial-contents (1- rank) dim env))
328                            (cdr initial-contents)))
329           initial-contents)
330       ;; This is the important bit: once we are past the level of
331       ;; :INITIAL-CONTENTS that relates to the array structure, reinline LIST
332       ;; and VECTOR so that nested DX isn't screwed up.
333       `(locally (declare (inline list vector))
334          ,initial-contents)))
335
336 ;;; Prevent open coding DIMENSION and :INITIAL-CONTENTS arguments, so that we
337 ;;; can pick them apart in the DEFTRANSFORMS, and transform '(3) style
338 ;;; dimensions to integer args directly.
339 (define-source-transform make-array (dimensions &rest keyargs &environment env)
340   (if (or (and (fun-lexically-notinline-p 'list)
341                (fun-lexically-notinline-p 'vector))
342           (oddp (length keyargs)))
343       (values nil t)
344       (multiple-value-bind (new-dimensions rank)
345           (flet ((constant-dims (dimensions)
346                    (let* ((dims (constant-form-value dimensions env))
347                           (canon (if (listp dims) dims (list dims)))
348                           (rank (length canon)))
349                      (values (if (= rank 1)
350                                  (list 'quote (car canon))
351                                  (list 'quote canon))
352                              rank))))
353             (cond ((sb!xc:constantp dimensions env)
354                    (constant-dims dimensions))
355                   ((and (consp dimensions) (eq 'list dimensions))
356                    (values dimensions (length (cdr dimensions))))
357                   (t
358                    (values dimensions nil))))
359         (let ((initial-contents (getf keyargs :initial-contents)))
360           (when (and initial-contents rank)
361             (setf (getf keyargs :initial-contents)
362                   (rewrite-initial-contents rank initial-contents env))))
363         `(locally (declare (notinline list vector))
364            (make-array ,new-dimensions ,@keyargs)))))
365
366 ;;; This baby is a bit of a monster, but it takes care of any MAKE-ARRAY
367 ;;; call which creates a vector with a known element type -- and tries
368 ;;; to do a good job with all the different ways it can happen.
369 (defun transform-make-array-vector (length element-type initial-element
370                                     initial-contents call)
371   (aver (or (not element-type) (constant-lvar-p element-type)))
372   (let* ((c-length (when (constant-lvar-p length)
373                      (lvar-value length)))
374          (elt-spec (if element-type
375                        (lvar-value element-type)
376                        t))
377          (elt-ctype (ir1-transform-specifier-type elt-spec))
378          (saetp (if (unknown-type-p elt-ctype)
379                     (give-up-ir1-transform "~S is an unknown type: ~S"
380                                            :element-type elt-spec)
381                     (find-saetp-by-ctype elt-ctype)))
382          (default-initial-element (sb!vm:saetp-initial-element-default saetp))
383          (n-bits (sb!vm:saetp-n-bits saetp))
384          (typecode (sb!vm:saetp-typecode saetp))
385          (n-pad-elements (sb!vm:saetp-n-pad-elements saetp))
386          (n-words-form
387           (if c-length
388               (ceiling (* (+ c-length n-pad-elements) n-bits)
389                        sb!vm:n-word-bits)
390               (let ((padded-length-form (if (zerop n-pad-elements)
391                                             'length
392                                             `(+ length ,n-pad-elements))))
393                 (cond
394                   ((= n-bits 0) 0)
395                   ((>= n-bits sb!vm:n-word-bits)
396                    `(* ,padded-length-form
397                        ;; i.e., not RATIO
398                        ,(the fixnum (/ n-bits sb!vm:n-word-bits))))
399                   (t
400                    (let ((n-elements-per-word (/ sb!vm:n-word-bits n-bits)))
401                      (declare (type index n-elements-per-word)) ; i.e., not RATIO
402                      `(ceiling ,padded-length-form ,n-elements-per-word)))))))
403          (result-spec
404           `(simple-array ,(sb!vm:saetp-specifier saetp) (,(or c-length '*))))
405          (alloc-form
406           `(truly-the ,result-spec
407                       (allocate-vector ,typecode (the index length) ,n-words-form))))
408     (cond ((and initial-element initial-contents)
409            (abort-ir1-transform "Both ~S and ~S specified."
410                                 :initial-contents :initial-element))
411           ;; :INITIAL-CONTENTS (LIST ...), (VECTOR ...) and `(1 1 ,x) with a
412           ;; constant LENGTH.
413           ((and initial-contents c-length
414                 (lvar-matches initial-contents
415                               :fun-names '(list vector sb!impl::backq-list)
416                               :arg-count c-length))
417            (let ((parameters (eliminate-keyword-args
418                               call 1 '((:element-type element-type)
419                                        (:initial-contents initial-contents))))
420                  (elt-vars (make-gensym-list c-length))
421                  (lambda-list '(length)))
422              (splice-fun-args initial-contents :any c-length)
423              (dolist (p parameters)
424                (setf lambda-list
425                      (append lambda-list
426                              (if (eq p 'initial-contents)
427                                  elt-vars
428                                  (list p)))))
429              `(lambda ,lambda-list
430                 (declare (type ,elt-spec ,@elt-vars)
431                          (ignorable ,@lambda-list))
432                 (truly-the ,result-spec
433                  (initialize-vector ,alloc-form ,@elt-vars)))))
434           ;; constant :INITIAL-CONTENTS and LENGTH
435           ((and initial-contents c-length (constant-lvar-p initial-contents))
436            (let ((contents (lvar-value initial-contents)))
437              (unless (= c-length (length contents))
438                (abort-ir1-transform "~S has ~S elements, vector length is ~S."
439                                     :initial-contents (length contents) c-length))
440              (let ((parameters (eliminate-keyword-args
441                                 call 1 '((:element-type element-type)
442                                          (:initial-contents initial-contents)))))
443                `(lambda (length ,@parameters)
444                   (declare (ignorable ,@parameters))
445                   (truly-the ,result-spec
446                    (initialize-vector ,alloc-form
447                                       ,@(map 'list (lambda (elt)
448                                                      `(the ,elt-spec ',elt))
449                                              contents)))))))
450           ;; any other :INITIAL-CONTENTS
451           (initial-contents
452            (let ((parameters (eliminate-keyword-args
453                               call 1 '((:element-type element-type)
454                                        (:initial-contents initial-contents)))))
455              `(lambda (length ,@parameters)
456                 (declare (ignorable ,@parameters))
457                 (unless (= length (length initial-contents))
458                   (error "~S has ~S elements, vector length is ~S."
459                          :initial-contents (length initial-contents) length))
460                 (truly-the ,result-spec
461                            (replace ,alloc-form initial-contents)))))
462           ;; :INITIAL-ELEMENT, not EQL to the default
463           ((and initial-element
464                 (or (not (constant-lvar-p initial-element))
465                     (not (eql default-initial-element (lvar-value initial-element)))))
466            (let ((parameters (eliminate-keyword-args
467                               call 1 '((:element-type element-type)
468                                        (:initial-element initial-element))))
469                  (init (if (constant-lvar-p initial-element)
470                            (list 'quote (lvar-value initial-element))
471                            'initial-element)))
472              `(lambda (length ,@parameters)
473                 (declare (ignorable ,@parameters))
474                 (truly-the ,result-spec
475                            (fill ,alloc-form (the ,elt-spec ,init))))))
476           ;; just :ELEMENT-TYPE, or maybe with :INITIAL-ELEMENT EQL to the
477           ;; default
478           (t
479            #-sb-xc-host
480            (unless (ctypep default-initial-element elt-ctype)
481              ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
482              ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
483              ;; INITIAL-ELEMENT is not supplied, the consequences of later
484              ;; reading an uninitialized element of new-array are undefined,"
485              ;; so this could be legal code as long as the user plans to
486              ;; write before he reads, and if he doesn't we're free to do
487              ;; anything we like. But in case the user doesn't know to write
488              ;; elements before he reads elements (or to read manuals before
489              ;; he writes code:-), we'll signal a STYLE-WARNING in case he
490              ;; didn't realize this.
491              (if initial-element
492                  (compiler-warn "~S ~S is not a ~S"
493                                 :initial-element default-initial-element
494                                 elt-spec)
495                  (compiler-style-warn "The default initial element ~S is not a ~S."
496                                       default-initial-element
497                                       elt-spec)))
498            (let ((parameters (eliminate-keyword-args
499                               call 1 '((:element-type element-type)
500                                        (:initial-element initial-element)))))
501              `(lambda (length ,@parameters)
502                 (declare (ignorable ,@parameters))
503                 ,alloc-form))))))
504
505 ;;; IMPORTANT: The order of these three MAKE-ARRAY forms matters: the least
506 ;;; specific must come first, otherwise suboptimal transforms will result for
507 ;;; some forms.
508
509 (deftransform make-array ((dims &key initial-element element-type
510                                      adjustable fill-pointer)
511                           (t &rest *))
512   (when (null initial-element)
513     (give-up-ir1-transform))
514   (let* ((eltype (cond ((not element-type) t)
515                        ((not (constant-lvar-p element-type))
516                         (give-up-ir1-transform
517                          "ELEMENT-TYPE is not constant."))
518                        (t
519                         (lvar-value element-type))))
520          (eltype-type (ir1-transform-specifier-type eltype))
521          (saetp (find-if (lambda (saetp)
522                            (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
523                          sb!vm:*specialized-array-element-type-properties*))
524          (creation-form `(make-array dims
525                           :element-type ',(type-specifier (sb!vm:saetp-ctype saetp))
526                           ,@(when fill-pointer
527                                   '(:fill-pointer fill-pointer))
528                           ,@(when adjustable
529                                   '(:adjustable adjustable)))))
530
531     (unless saetp
532       (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
533
534     (cond ((and (constant-lvar-p initial-element)
535                 (eql (lvar-value initial-element)
536                      (sb!vm:saetp-initial-element-default saetp)))
537            creation-form)
538           (t
539            ;; error checking for target, disabled on the host because
540            ;; (CTYPE-OF #\Null) is not possible.
541            #-sb-xc-host
542            (when (constant-lvar-p initial-element)
543              (let ((value (lvar-value initial-element)))
544                (cond
545                  ((not (ctypep value (sb!vm:saetp-ctype saetp)))
546                   ;; this case will cause an error at runtime, so we'd
547                   ;; better WARN about it now.
548                   (warn 'array-initial-element-mismatch
549                         :format-control "~@<~S is not a ~S (which is the ~
550                                          ~S of ~S).~@:>"
551                         :format-arguments
552                         (list
553                          value
554                          (type-specifier (sb!vm:saetp-ctype saetp))
555                          'upgraded-array-element-type
556                          eltype)))
557                  ((not (ctypep value eltype-type))
558                   ;; this case will not cause an error at runtime, but
559                   ;; it's still worth STYLE-WARNing about.
560                   (compiler-style-warn "~S is not a ~S."
561                                        value eltype)))))
562            `(let ((array ,creation-form))
563              (multiple-value-bind (vector)
564                  (%data-vector-and-index array 0)
565                (fill vector (the ,(sb!vm:saetp-specifier saetp) initial-element)))
566              array)))))
567
568 ;;; The list type restriction does not ensure that the result will be a
569 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
570 ;;; and displaced-to keywords ensures that it will be simple.
571 ;;;
572 ;;; FIXME: should we generalize this transform to non-simple (though
573 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
574 ;;; deal with those? Maybe when the DEFTRANSFORM
575 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
576 ;;; CSR, 2002-07-01
577 (deftransform make-array ((dims &key
578                                 element-type initial-element initial-contents)
579                           (list &key
580                                 (:element-type (constant-arg *))
581                                 (:initial-element *)
582                                 (:initial-contents *))
583                           *
584                           :node call)
585   (block make-array
586     (when (lvar-matches dims :fun-names '(list) :arg-count 1)
587       (let ((length (car (splice-fun-args dims :any 1))))
588         (return-from make-array
589           (transform-make-array-vector length
590                                        element-type
591                                        initial-element
592                                        initial-contents
593                                        call))))
594     (unless (constant-lvar-p dims)
595       (give-up-ir1-transform
596        "The dimension list is not constant; cannot open code array creation."))
597     (let ((dims (lvar-value dims)))
598       (unless (every #'integerp dims)
599         (give-up-ir1-transform
600          "The dimension list contains something other than an integer: ~S"
601          dims))
602       (if (= (length dims) 1)
603           `(make-array ',(car dims)
604                        ,@(when element-type
605                                '(:element-type element-type))
606                        ,@(when initial-element
607                                '(:initial-element initial-element))
608                        ,@(when initial-contents
609                                '(:initial-contents initial-contents)))
610           (let* ((total-size (reduce #'* dims))
611                  (rank (length dims))
612                  (spec `(simple-array
613                          ,(cond ((null element-type) t)
614                                 ((and (constant-lvar-p element-type)
615                                       (ir1-transform-specifier-type
616                                        (lvar-value element-type)))
617                                  (sb!xc:upgraded-array-element-type
618                                   (lvar-value element-type)))
619                                 (t '*))
620                          ,(make-list rank :initial-element '*))))
621             `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank))
622                    (data (make-array ,total-size
623                                      ,@(when element-type
624                                              '(:element-type element-type))
625                                      ,@(when initial-element
626                                              '(:initial-element initial-element)))))
627                ,@(when initial-contents
628                        ;; FIXME: This is could be open coded at least a bit too
629                        `((sb!impl::fill-data-vector data ',dims initial-contents)))
630                (setf (%array-fill-pointer header) ,total-size)
631                (setf (%array-fill-pointer-p header) nil)
632                (setf (%array-available-elements header) ,total-size)
633                (setf (%array-data-vector header) data)
634                (setf (%array-displaced-p header) nil)
635                (setf (%array-displaced-from header) nil)
636                ,@(let ((axis -1))
637                       (mapcar (lambda (dim)
638                                 `(setf (%array-dimension header ,(incf axis))
639                                        ,dim))
640                               dims))
641                (truly-the ,spec header)))))))
642
643 (deftransform make-array ((dims &key element-type initial-element initial-contents)
644                           (integer &key
645                                    (:element-type (constant-arg *))
646                                    (:initial-element *)
647                                    (:initial-contents *))
648                           *
649                           :node call)
650   (transform-make-array-vector dims
651                                element-type
652                                initial-element
653                                initial-contents
654                                call))
655 \f
656 ;;;; miscellaneous properties of arrays
657
658 ;;; Transforms for various array properties. If the property is know
659 ;;; at compile time because of a type spec, use that constant value.
660
661 ;;; Most of this logic may end up belonging in code/late-type.lisp;
662 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
663 ;;; maybe this is just too sloppy for actual type logic.  -- CSR,
664 ;;; 2004-02-18
665 (defun array-type-dimensions-or-give-up (type)
666   (labels ((maybe-array-type-dimensions (type)
667              (typecase type
668                (array-type
669                 (array-type-dimensions type))
670                (union-type
671                 (let* ((types (remove nil (mapcar #'maybe-array-type-dimensions
672                                                   (union-type-types type))))
673                        (result (car types)))
674                   (dolist (other (cdr types) result)
675                     (unless (equal result other)
676                       (give-up-ir1-transform
677                        "~@<dimensions of arrays in union type ~S do not match~:@>"
678                        (type-specifier type))))))
679                (intersection-type
680                 (let* ((types (remove nil (mapcar #'maybe-array-type-dimensions
681                                                   (intersection-type-types type))))
682                        (result (car types)))
683                   (dolist (other (cdr types) result)
684                     (unless (equal result other)
685                       (abort-ir1-transform
686                        "~@<dimensions of arrays in intersection type ~S do not match~:@>"
687                        (type-specifier type)))))))))
688     (or (maybe-array-type-dimensions type)
689         (give-up-ir1-transform
690          "~@<don't know how to extract array dimensions from type ~S~:@>"
691          (type-specifier type)))))
692
693 (defun conservative-array-type-complexp (type)
694   (typecase type
695     (array-type (array-type-complexp type))
696     (union-type
697      (let ((types (union-type-types type)))
698        (aver (> (length types) 1))
699        (let ((result (conservative-array-type-complexp (car types))))
700          (dolist (type (cdr types) result)
701            (unless (eq (conservative-array-type-complexp type) result)
702              (return-from conservative-array-type-complexp :maybe))))))
703     ;; FIXME: intersection type
704     (t :maybe)))
705
706 ;;; If we can tell the rank from the type info, use it instead.
707 (deftransform array-rank ((array))
708   (let ((array-type (lvar-type array)))
709     (let ((dims (array-type-dimensions-or-give-up array-type)))
710       (cond ((listp dims)
711              (length dims))
712             ((eq t (array-type-complexp array-type))
713              '(%array-rank array))
714             (t
715              `(if (array-header-p array)
716                   (%array-rank array)
717                   1))))))
718
719 ;;; If we know the dimensions at compile time, just use it. Otherwise,
720 ;;; if we can tell that the axis is in bounds, convert to
721 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
722 ;;; (if it's simple and a vector).
723 (deftransform array-dimension ((array axis)
724                                (array index))
725   (unless (constant-lvar-p axis)
726     (give-up-ir1-transform "The axis is not constant."))
727   ;; Dimensions may change thanks to ADJUST-ARRAY, so we need the
728   ;; conservative type.
729   (let ((array-type (lvar-conservative-type array))
730         (axis (lvar-value axis)))
731     (let ((dims (array-type-dimensions-or-give-up array-type)))
732       (unless (listp dims)
733         (give-up-ir1-transform
734          "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
735       (unless (> (length dims) axis)
736         (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
737                              dims
738                              axis))
739       (let ((dim (nth axis dims)))
740         (cond ((integerp dim)
741                dim)
742               ((= (length dims) 1)
743                (ecase (conservative-array-type-complexp array-type)
744                  ((t)
745                   '(%array-dimension array 0))
746                  ((nil)
747                   '(vector-length array))
748                  ((:maybe)
749                   `(if (array-header-p array)
750                        (%array-dimension array axis)
751                        (vector-length array)))))
752               (t
753                '(%array-dimension array axis)))))))
754
755 ;;; If the length has been declared and it's simple, just return it.
756 (deftransform length ((vector)
757                       ((simple-array * (*))))
758   (let ((type (lvar-type vector)))
759     (let ((dims (array-type-dimensions-or-give-up type)))
760       (unless (and (listp dims) (integerp (car dims)))
761         (give-up-ir1-transform
762          "Vector length is unknown, must call LENGTH at runtime."))
763       (car dims))))
764
765 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
766 ;;; simple, it will extract the length slot from the vector. It it's
767 ;;; complex, it will extract the fill pointer slot from the array
768 ;;; header.
769 (deftransform length ((vector) (vector))
770   '(vector-length vector))
771
772 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
773 ;;; compile-time constant.
774 (deftransform vector-length ((vector))
775   (let ((vtype (lvar-type vector)))
776     (let ((dim (first (array-type-dimensions-or-give-up vtype))))
777       (when (eq dim '*)
778         (give-up-ir1-transform))
779       (when (conservative-array-type-complexp vtype)
780         (give-up-ir1-transform))
781       dim)))
782
783 ;;; Again, if we can tell the results from the type, just use it.
784 ;;; Otherwise, if we know the rank, convert into a computation based
785 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
786 ;;; multiplications because we know that the total size must be an
787 ;;; INDEX.
788 (deftransform array-total-size ((array)
789                                 (array))
790   (let ((array-type (lvar-type array)))
791     (let ((dims (array-type-dimensions-or-give-up array-type)))
792       (unless (listp dims)
793         (give-up-ir1-transform "can't tell the rank at compile time"))
794       (if (member '* dims)
795           (do ((form 1 `(truly-the index
796                                    (* (array-dimension array ,i) ,form)))
797                (i 0 (1+ i)))
798               ((= i (length dims)) form))
799           (reduce #'* dims)))))
800
801 ;;; Only complex vectors have fill pointers.
802 (deftransform array-has-fill-pointer-p ((array))
803   (let ((array-type (lvar-type array)))
804     (let ((dims (array-type-dimensions-or-give-up array-type)))
805       (if (and (listp dims) (not (= (length dims) 1)))
806           nil
807           (ecase (conservative-array-type-complexp array-type)
808             ((t)
809              t)
810             ((nil)
811              nil)
812             ((:maybe)
813              (give-up-ir1-transform
814               "The array type is ambiguous; must call ~
815                ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
816
817 ;;; Primitive used to verify indices into arrays. If we can tell at
818 ;;; compile-time or we are generating unsafe code, don't bother with
819 ;;; the VOP.
820 (deftransform %check-bound ((array dimension index) * * :node node)
821   (cond ((policy node (= insert-array-bounds-checks 0))
822          'index)
823         ((not (constant-lvar-p dimension))
824          (give-up-ir1-transform))
825         (t
826          (let ((dim (lvar-value dimension)))
827            ;; FIXME: Can SPEED > SAFETY weaken this check to INTEGER?
828            `(the (integer 0 (,dim)) index)))))
829 \f
830 ;;;; WITH-ARRAY-DATA
831
832 ;;; This checks to see whether the array is simple and the start and
833 ;;; end are in bounds. If so, it proceeds with those values.
834 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
835 ;;; may be further optimized.
836 ;;;
837 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
838 ;;; START-VAR and END-VAR to the start and end of the designated
839 ;;; portion of the data vector. SVALUE and EVALUE are any start and
840 ;;; end specified to the original operation, and are factored into the
841 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
842 ;;; offset of all displacements encountered, and does not include
843 ;;; SVALUE.
844 ;;;
845 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
846 ;;; forced to be inline, overriding the ordinary judgment of the
847 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
848 ;;; fairly picky about their arguments, figuring that if you haven't
849 ;;; bothered to get all your ducks in a row, you probably don't care
850 ;;; that much about speed anyway! But in some cases it makes sense to
851 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
852 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
853 ;;; sense to use FORCE-INLINE option in that case.
854 (def!macro with-array-data (((data-var array &key offset-var)
855                              (start-var &optional (svalue 0))
856                              (end-var &optional (evalue nil))
857                              &key force-inline check-fill-pointer)
858                             &body forms
859                             &environment env)
860   (once-only ((n-array array)
861               (n-svalue `(the index ,svalue))
862               (n-evalue `(the (or index null) ,evalue)))
863     (let ((check-bounds (policy env (plusp insert-array-bounds-checks))))
864       `(multiple-value-bind (,data-var
865                              ,start-var
866                              ,end-var
867                              ,@(when offset-var `(,offset-var)))
868            (if (not (array-header-p ,n-array))
869                (let ((,n-array ,n-array))
870                  (declare (type (simple-array * (*)) ,n-array))
871                  ,(once-only ((n-len (if check-fill-pointer
872                                          `(length ,n-array)
873                                          `(array-total-size ,n-array)))
874                               (n-end `(or ,n-evalue ,n-len)))
875                              (if check-bounds
876                                  `(if (<= 0 ,n-svalue ,n-end ,n-len)
877                                       (values ,n-array ,n-svalue ,n-end 0)
878                                       ,(if check-fill-pointer
879                                            `(sequence-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)
880                                            `(array-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)))
881                                  `(values ,n-array ,n-svalue ,n-end 0))))
882                ,(if force-inline
883                     `(%with-array-data-macro ,n-array ,n-svalue ,n-evalue
884                                              :check-bounds ,check-bounds
885                                              :check-fill-pointer ,check-fill-pointer)
886                     (if check-fill-pointer
887                         `(%with-array-data/fp ,n-array ,n-svalue ,n-evalue)
888                         `(%with-array-data ,n-array ,n-svalue ,n-evalue))))
889          ,@forms))))
890
891 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
892 ;;; DEFTRANSFORMs and DEFUNs.
893 (def!macro %with-array-data-macro (array
894                                    start
895                                    end
896                                    &key
897                                    (element-type '*)
898                                    check-bounds
899                                    check-fill-pointer)
900   (with-unique-names (size defaulted-end data cumulative-offset)
901     `(let* ((,size ,(if check-fill-pointer
902                         `(length ,array)
903                         `(array-total-size ,array)))
904             (,defaulted-end (or ,end ,size)))
905        ,@(when check-bounds
906                `((unless (<= ,start ,defaulted-end ,size)
907                    ,(if check-fill-pointer
908                         `(sequence-bounding-indices-bad-error ,array ,start ,end)
909                         `(array-bounding-indices-bad-error ,array ,start ,end)))))
910        (do ((,data ,array (%array-data-vector ,data))
911             (,cumulative-offset 0
912                                 (+ ,cumulative-offset
913                                    (%array-displacement ,data))))
914            ((not (array-header-p ,data))
915             (values (the (simple-array ,element-type 1) ,data)
916                     (the index (+ ,cumulative-offset ,start))
917                     (the index (+ ,cumulative-offset ,defaulted-end))
918                     (the index ,cumulative-offset)))
919          (declare (type index ,cumulative-offset))))))
920
921 (defun transform-%with-array-data/muble (array node check-fill-pointer)
922   (let ((element-type (upgraded-element-type-specifier-or-give-up array))
923         (type (lvar-type array))
924         (check-bounds (policy node (plusp insert-array-bounds-checks))))
925     (if (and (array-type-p type)
926              (not (array-type-complexp type))
927              (listp (array-type-dimensions type))
928              (not (null (cdr (array-type-dimensions type)))))
929         ;; If it's a simple multidimensional array, then just return
930         ;; its data vector directly rather than going through
931         ;; %WITH-ARRAY-DATA-MACRO. SBCL doesn't generally generate
932         ;; code that would use this currently, but we have encouraged
933         ;; users to use WITH-ARRAY-DATA and we may use it ourselves at
934         ;; some point in the future for optimized libraries or
935         ;; similar.
936         (if check-bounds
937             `(let* ((data (truly-the (simple-array ,element-type (*))
938                                      (%array-data-vector array)))
939                     (len (length data))
940                     (real-end (or end len)))
941                (unless (<= 0 start data-end lend)
942                  (sequence-bounding-indices-bad-error array start end))
943                (values data 0 real-end 0))
944             `(let ((data (truly-the (simple-array ,element-type (*))
945                                     (%array-data-vector array))))
946                (values data 0 (or end (length data)) 0)))
947         `(%with-array-data-macro array start end
948                                  :check-fill-pointer ,check-fill-pointer
949                                  :check-bounds ,check-bounds
950                                  :element-type ,element-type))))
951
952 ;; It might very well be reasonable to allow general ARRAY here, I
953 ;; just haven't tried to understand the performance issues involved.
954 ;; -- WHN, and also CSR 2002-05-26
955 (deftransform %with-array-data ((array start end)
956                                 ((or vector simple-array) index (or index null) t)
957                                 *
958                                 :node node
959                                 :policy (> speed space))
960   "inline non-SIMPLE-vector-handling logic"
961   (transform-%with-array-data/muble array node nil))
962 (deftransform %with-array-data/fp ((array start end)
963                                 ((or vector simple-array) index (or index null) t)
964                                 *
965                                 :node node
966                                 :policy (> speed space))
967   "inline non-SIMPLE-vector-handling logic"
968   (transform-%with-array-data/muble array node t))
969 \f
970 ;;;; array accessors
971
972 ;;; We convert all typed array accessors into AREF and %ASET with type
973 ;;; assertions on the array.
974 (macrolet ((define-bit-frob (reffer setter simplep)
975              `(progn
976                 (define-source-transform ,reffer (a &rest i)
977                   `(aref (the (,',(if simplep 'simple-array 'array)
978                                   bit
979                                   ,(mapcar (constantly '*) i))
980                            ,a) ,@i))
981                 (define-source-transform ,setter (a &rest i)
982                   `(%aset (the (,',(if simplep 'simple-array 'array)
983                                    bit
984                                    ,(cdr (mapcar (constantly '*) i)))
985                             ,a) ,@i)))))
986   (define-bit-frob sbit %sbitset t)
987   (define-bit-frob bit %bitset nil))
988 (macrolet ((define-frob (reffer setter type)
989              `(progn
990                 (define-source-transform ,reffer (a i)
991                   `(aref (the ,',type ,a) ,i))
992                 (define-source-transform ,setter (a i v)
993                   `(%aset (the ,',type ,a) ,i ,v)))))
994   (define-frob svref %svset simple-vector)
995   (define-frob schar %scharset simple-string)
996   (define-frob char %charset string))
997
998 (macrolet (;; This is a handy macro for computing the row-major index
999            ;; given a set of indices. We wrap each index with a call
1000            ;; to %CHECK-BOUND to ensure that everything works out
1001            ;; correctly. We can wrap all the interior arithmetic with
1002            ;; TRULY-THE INDEX because we know the resultant
1003            ;; row-major index must be an index.
1004            (with-row-major-index ((array indices index &optional new-value)
1005                                   &rest body)
1006              `(let (n-indices dims)
1007                 (dotimes (i (length ,indices))
1008                   (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
1009                   (push (make-symbol (format nil "DIM-~D" i)) dims))
1010                 (setf n-indices (nreverse n-indices))
1011                 (setf dims (nreverse dims))
1012                 `(lambda (,',array ,@n-indices
1013                                    ,@',(when new-value (list new-value)))
1014                    (let* (,@(let ((,index -1))
1015                               (mapcar (lambda (name)
1016                                         `(,name (array-dimension
1017                                                  ,',array
1018                                                  ,(incf ,index))))
1019                                       dims))
1020                             (,',index
1021                              ,(if (null dims)
1022                                   0
1023                                 (do* ((dims dims (cdr dims))
1024                                       (indices n-indices (cdr indices))
1025                                       (last-dim nil (car dims))
1026                                       (form `(%check-bound ,',array
1027                                                            ,(car dims)
1028                                                            ,(car indices))
1029                                             `(truly-the
1030                                               index
1031                                               (+ (truly-the index
1032                                                             (* ,form
1033                                                                ,last-dim))
1034                                                  (%check-bound
1035                                                   ,',array
1036                                                   ,(car dims)
1037                                                   ,(car indices))))))
1038                                     ((null (cdr dims)) form)))))
1039                      ,',@body)))))
1040
1041   ;; Just return the index after computing it.
1042   (deftransform array-row-major-index ((array &rest indices))
1043     (with-row-major-index (array indices index)
1044       index))
1045
1046   ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
1047   ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
1048   ;; expression for the row major index.
1049   (deftransform aref ((array &rest indices))
1050     (with-row-major-index (array indices index)
1051       (hairy-data-vector-ref array index)))
1052
1053   (deftransform %aset ((array &rest stuff))
1054     (let ((indices (butlast stuff)))
1055       (with-row-major-index (array indices index new-value)
1056         (hairy-data-vector-set array index new-value)))))
1057
1058 ;; For AREF of vectors we do the bounds checking in the callee. This
1059 ;; lets us do a significantly more efficient check for simple-arrays
1060 ;; without bloating the code. If we already know the type of the array
1061 ;; with sufficient precision, skip directly to DATA-VECTOR-REF.
1062 (deftransform aref ((array index) (t t) * :node node)
1063   (let* ((type (lvar-type array))
1064          (element-ctype (array-type-upgraded-element-type type)))
1065     (cond
1066       ((and (array-type-p type)
1067             (null (array-type-complexp type))
1068             (not (eql element-ctype *wild-type*))
1069             (eql (length (array-type-dimensions type)) 1))
1070        (let* ((declared-element-ctype (array-type-declared-element-type type))
1071               (bare-form
1072                `(data-vector-ref array
1073                  (%check-bound array (array-dimension array 0) index))))
1074          (if (type= declared-element-ctype element-ctype)
1075              bare-form
1076              `(the ,(type-specifier declared-element-ctype) ,bare-form))))
1077       ((policy node (zerop insert-array-bounds-checks))
1078        `(hairy-data-vector-ref array index))
1079       (t `(hairy-data-vector-ref/check-bounds array index)))))
1080
1081 (deftransform %aset ((array index new-value) (t t t) * :node node)
1082   (if (policy node (zerop insert-array-bounds-checks))
1083       `(hairy-data-vector-set array index new-value)
1084       `(hairy-data-vector-set/check-bounds array index new-value)))
1085
1086 ;;; But if we find out later that there's some useful type information
1087 ;;; available, switch back to the normal one to give other transforms
1088 ;;; a stab at it.
1089 (macrolet ((define (name transform-to extra extra-type)
1090              (declare (ignore extra-type))
1091              `(deftransform ,name ((array index ,@extra))
1092                 (let* ((type (lvar-type array))
1093                        (element-type (array-type-upgraded-element-type type))
1094                        (declared-type (array-type-declared-element-type type)))
1095                   ;; If an element type has been declared, we want to
1096                   ;; use that information it for type checking (even
1097                   ;; if the access can't be optimized due to the array
1098                   ;; not being simple).
1099                   (when (and (eql element-type *wild-type*)
1100                              ;; This type logic corresponds to the special
1101                              ;; case for strings in HAIRY-DATA-VECTOR-REF
1102                              ;; (generic/vm-tran.lisp)
1103                              (not (csubtypep type (specifier-type 'simple-string))))
1104                     (when (or (not (array-type-p type))
1105                               ;; If it's a simple array, we might be able
1106                               ;; to inline the access completely.
1107                               (not (null (array-type-complexp type))))
1108                       (give-up-ir1-transform
1109                        "Upgraded element type of array is not known at compile time.")))
1110                   ,(if extra
1111                        ``(truly-the ,declared-type
1112                                     (,',transform-to array
1113                                                      (%check-bound array
1114                                                                    (array-dimension array 0)
1115                                                                    index)
1116                                                      (the ,declared-type ,@',extra)))
1117                        ``(the ,declared-type
1118                            (,',transform-to array
1119                                             (%check-bound array
1120                                                           (array-dimension array 0)
1121                                                           index))))))))
1122   (define hairy-data-vector-ref/check-bounds
1123       hairy-data-vector-ref nil nil)
1124   (define hairy-data-vector-set/check-bounds
1125       hairy-data-vector-set (new-value) (*)))
1126
1127 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
1128 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
1129 ;;; array total size.
1130 (deftransform row-major-aref ((array index))
1131   `(hairy-data-vector-ref array
1132                           (%check-bound array (array-total-size array) index)))
1133 (deftransform %set-row-major-aref ((array index new-value))
1134   `(hairy-data-vector-set array
1135                           (%check-bound array (array-total-size array) index)
1136                           new-value))
1137 \f
1138 ;;;; bit-vector array operation canonicalization
1139 ;;;;
1140 ;;;; We convert all bit-vector operations to have the result array
1141 ;;;; specified. This allows any result allocation to be open-coded,
1142 ;;;; and eliminates the need for any VM-dependent transforms to handle
1143 ;;;; these cases.
1144
1145 (macrolet ((def (fun)
1146              `(progn
1147                (deftransform ,fun ((bit-array-1 bit-array-2
1148                                                 &optional result-bit-array)
1149                                    (bit-vector bit-vector &optional null) *
1150                                    :policy (>= speed space))
1151                  `(,',fun bit-array-1 bit-array-2
1152                    (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
1153                ;; If result is T, make it the first arg.
1154                (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
1155                                    (bit-vector bit-vector (eql t)) *)
1156                  `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
1157   (def bit-and)
1158   (def bit-ior)
1159   (def bit-xor)
1160   (def bit-eqv)
1161   (def bit-nand)
1162   (def bit-nor)
1163   (def bit-andc1)
1164   (def bit-andc2)
1165   (def bit-orc1)
1166   (def bit-orc2))
1167
1168 ;;; Similar for BIT-NOT, but there is only one arg...
1169 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
1170                        (bit-vector &optional null) *
1171                        :policy (>= speed space))
1172   '(bit-not bit-array-1
1173             (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
1174 (deftransform bit-not ((bit-array-1 result-bit-array)
1175                        (bit-vector (eql t)))
1176   '(bit-not bit-array-1 bit-array-1))
1177 \f
1178 ;;; Pick off some constant cases.
1179 (defoptimizer (array-header-p derive-type) ((array))
1180   (let ((type (lvar-type array)))
1181     (cond ((not (array-type-p type))
1182            ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
1183            nil)
1184           (t
1185            (let ((dims (array-type-dimensions type)))
1186              (cond ((csubtypep type (specifier-type '(simple-array * (*))))
1187                     ;; no array header
1188                     (specifier-type 'null))
1189                    ((and (listp dims) (/= (length dims) 1))
1190                     ;; multi-dimensional array, will have a header
1191                     (specifier-type '(eql t)))
1192                    ((eql (array-type-complexp type) t)
1193                     (specifier-type '(eql t)))
1194                    (t
1195                     nil)))))))