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