1 ;;;; array-specific optimizers and transforms
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;;; utilities for optimizing array operations
16 ;;; Return UPGRADED-ARRAY-ELEMENT-TYPE for LVAR, or do
17 ;;; GIVE-UP-IR1-TRANSFORM if the upgraded element type can't be
19 (defun upgraded-element-type-specifier-or-give-up (lvar)
20 (let* ((element-ctype (extract-upgraded-element-type lvar))
21 (element-type-specifier (type-specifier element-ctype)))
22 (if (eq element-type-specifier '*)
23 (give-up-ir1-transform
24 "upgraded array element type not known at compile time")
25 element-type-specifier)))
27 ;;; Array access functions return an object from the array, hence its
28 ;;; type is going to be the array upgraded element type.
29 (defun extract-upgraded-element-type (array)
30 (let ((type (lvar-type array)))
32 ;; Note that this IF mightn't be satisfied even if the runtime
33 ;; value is known to be a subtype of some specialized ARRAY, because
34 ;; we can have values declared e.g. (AND SIMPLE-VECTOR UNKNOWN-TYPE),
35 ;; which are represented in the compiler as INTERSECTION-TYPE, not
37 ((array-type-p type) (array-type-specialized-element-type type))
38 ;; fix for bug #396. This type logic corresponds to the special
39 ;; case for strings in HAIRY-DATA-VECTOR-REF
40 ;; (generic/vm-tran.lisp)
41 ((csubtypep type (specifier-type 'simple-string))
43 ((csubtypep type (specifier-type '(simple-array character (*))))
44 (specifier-type 'character))
46 ((csubtypep type (specifier-type '(simple-array base-char (*))))
47 (specifier-type 'base-char))
48 ((csubtypep type (specifier-type '(simple-array nil (*))))
53 ;; KLUDGE: there is no good answer here, but at least
54 ;; *wild-type* won't cause HAIRY-DATA-VECTOR-{REF,SET} to be
55 ;; erroneously optimized (see generic/vm-tran.lisp) -- CSR,
59 (defun extract-declared-element-type (array)
60 (let ((type (lvar-type array)))
61 (if (array-type-p type)
62 (array-type-element-type type)
65 ;;; The ``new-value'' for array setters must fit in the array, and the
66 ;;; return type is going to be the same as the new-value for SETF
68 (defun assert-new-value-type (new-value array)
69 (let ((type (lvar-type array)))
70 (when (array-type-p type)
73 (array-type-specialized-element-type type)
74 (lexenv-policy (node-lexenv (lvar-dest new-value))))))
75 (lvar-type new-value))
77 (defun assert-array-complex (array)
80 (make-array-type :complexp t
81 :element-type *wild-type*)
82 (lexenv-policy (node-lexenv (lvar-dest array))))
85 ;;; Return true if ARG is NIL, or is a constant-lvar whose
86 ;;; value is NIL, false otherwise.
87 (defun unsupplied-or-nil (arg)
88 (declare (type (or lvar null) arg))
90 (and (constant-lvar-p arg)
91 (not (lvar-value arg)))))
93 ;;;; DERIVE-TYPE optimizers
95 ;;; Array operations that use a specific number of indices implicitly
96 ;;; assert that the array is of that rank.
97 (defun assert-array-rank (array rank)
100 (specifier-type `(array * ,(make-list rank :initial-element '*)))
101 (lexenv-policy (node-lexenv (lvar-dest array)))))
103 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
104 (assert-array-rank array (length indices))
107 (defoptimizer (aref derive-type) ((array &rest indices) node)
108 (assert-array-rank array (length indices))
109 (extract-upgraded-element-type array))
111 (defoptimizer (%aset derive-type) ((array &rest stuff))
112 (assert-array-rank array (1- (length stuff)))
113 (assert-new-value-type (car (last stuff)) array))
115 (macrolet ((define (name)
116 `(defoptimizer (,name derive-type) ((array index))
117 (extract-upgraded-element-type array))))
118 (define hairy-data-vector-ref)
119 (define hairy-data-vector-ref/check-bounds)
120 (define data-vector-ref))
123 (defoptimizer (data-vector-ref-with-offset derive-type) ((array index offset))
124 (extract-upgraded-element-type array))
126 (macrolet ((define (name)
127 `(defoptimizer (,name derive-type) ((array index new-value))
128 (assert-new-value-type new-value array))))
129 (define hairy-data-vector-set)
130 (define hairy-data-vector-set/check-bounds)
131 (define data-vector-set))
134 (defoptimizer (data-vector-set-with-offset derive-type) ((array index offset new-value))
135 (assert-new-value-type new-value array))
137 ;;; Figure out the type of the data vector if we know the argument
139 (defoptimizer (%with-array-data derive-type) ((array start end))
140 (let ((atype (lvar-type array)))
141 (when (array-type-p atype)
143 `(simple-array ,(type-specifier
144 (array-type-specialized-element-type atype))
147 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
148 (assert-array-rank array (length indices))
151 (defoptimizer (row-major-aref derive-type) ((array index))
152 (extract-upgraded-element-type array))
154 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
155 (assert-new-value-type new-value array))
157 (defoptimizer (make-array derive-type)
158 ((dims &key initial-element element-type initial-contents
159 adjustable fill-pointer displaced-index-offset displaced-to))
160 (let ((simple (and (unsupplied-or-nil adjustable)
161 (unsupplied-or-nil displaced-to)
162 (unsupplied-or-nil fill-pointer))))
163 (or (careful-specifier-type
164 `(,(if simple 'simple-array 'array)
165 ,(cond ((not element-type) t)
166 ((constant-lvar-p element-type)
167 (let ((ctype (careful-specifier-type
168 (lvar-value element-type))))
170 ((or (null ctype) (unknown-type-p ctype)) '*)
171 (t (sb!xc:upgraded-array-element-type
172 (lvar-value element-type))))))
175 ,(cond ((constant-lvar-p dims)
176 (let* ((val (lvar-value dims))
177 (cdims (if (listp val) val (list val))))
181 ((csubtypep (lvar-type dims)
182 (specifier-type 'integer))
186 (specifier-type 'array))))
188 ;;; Complex array operations should assert that their array argument
189 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
190 (defoptimizer (fill-pointer derive-type) ((vector))
191 (assert-array-complex vector))
192 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
193 (declare (ignorable index))
194 (assert-array-complex vector))
196 (defoptimizer (vector-push derive-type) ((object vector))
197 (declare (ignorable object))
198 (assert-array-complex vector))
199 (defoptimizer (vector-push-extend derive-type)
200 ((object vector &optional index))
201 (declare (ignorable object index))
202 (assert-array-complex vector))
203 (defoptimizer (vector-pop derive-type) ((vector))
204 (assert-array-complex vector))
208 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
210 (define-source-transform vector (&rest elements)
211 (let ((len (length elements))
213 (once-only ((n-vec `(make-array ,len)))
215 ,@(mapcar (lambda (el)
216 (once-only ((n-val el))
217 `(locally (declare (optimize (safety 0)))
218 (setf (svref ,n-vec ,(incf n)) ,n-val))))
222 ;;; Just convert it into a MAKE-ARRAY.
223 (deftransform make-string ((length &key
224 (element-type 'character)
226 #.*default-init-char-form*)))
227 `(the simple-string (make-array (the index length)
228 :element-type element-type
229 ,@(when initial-element
230 '(:initial-element initial-element)))))
232 (deftransform make-array ((dims &key initial-element element-type
233 adjustable fill-pointer)
235 (when (null initial-element)
236 (give-up-ir1-transform))
237 (let* ((eltype (cond ((not element-type) t)
238 ((not (constant-lvar-p element-type))
239 (give-up-ir1-transform
240 "ELEMENT-TYPE is not constant."))
242 (lvar-value element-type))))
243 (eltype-type (ir1-transform-specifier-type eltype))
244 (saetp (find-if (lambda (saetp)
245 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
246 sb!vm:*specialized-array-element-type-properties*))
247 (creation-form `(make-array dims
248 :element-type ',(type-specifier (sb!vm:saetp-ctype saetp))
250 '(:fill-pointer fill-pointer))
252 '(:adjustable adjustable)))))
255 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
257 (cond ((and (constant-lvar-p initial-element)
258 (eql (lvar-value initial-element)
259 (sb!vm:saetp-initial-element-default saetp)))
262 ;; error checking for target, disabled on the host because
263 ;; (CTYPE-OF #\Null) is not possible.
265 (when (constant-lvar-p initial-element)
266 (let ((value (lvar-value initial-element)))
268 ((not (ctypep value (sb!vm:saetp-ctype saetp)))
269 ;; this case will cause an error at runtime, so we'd
270 ;; better WARN about it now.
271 (warn 'array-initial-element-mismatch
272 :format-control "~@<~S is not a ~S (which is the ~
277 (type-specifier (sb!vm:saetp-ctype saetp))
278 'upgraded-array-element-type
280 ((not (ctypep value eltype-type))
281 ;; this case will not cause an error at runtime, but
282 ;; it's still worth STYLE-WARNing about.
283 (compiler-style-warn "~S is not a ~S."
285 `(let ((array ,creation-form))
286 (multiple-value-bind (vector)
287 (%data-vector-and-index array 0)
288 (fill vector initial-element))
291 ;;; The integer type restriction on the length ensures that it will be
292 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
293 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
294 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
295 ;;; kind of initialization efficiently.
296 (deftransform make-array ((length &key element-type)
298 (let* ((eltype (cond ((not element-type) t)
299 ((not (constant-lvar-p element-type))
300 (give-up-ir1-transform
301 "ELEMENT-TYPE is not constant."))
303 (lvar-value element-type))))
304 (len (if (constant-lvar-p length)
307 (eltype-type (ir1-transform-specifier-type eltype))
310 ,(if (unknown-type-p eltype-type)
311 (give-up-ir1-transform
312 "ELEMENT-TYPE is an unknown type: ~S" eltype)
313 (sb!xc:upgraded-array-element-type eltype))
315 (saetp (find-if (lambda (saetp)
316 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
317 sb!vm:*specialized-array-element-type-properties*)))
319 (give-up-ir1-transform
320 "cannot open-code creation of ~S" result-type-spec))
322 (unless (ctypep (sb!vm:saetp-initial-element-default saetp) eltype-type)
323 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
324 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
325 ;; INITIAL-ELEMENT is not supplied, the consequences of later
326 ;; reading an uninitialized element of new-array are undefined,"
327 ;; so this could be legal code as long as the user plans to
328 ;; write before he reads, and if he doesn't we're free to do
329 ;; anything we like. But in case the user doesn't know to write
330 ;; elements before he reads elements (or to read manuals before
331 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
332 ;; didn't realize this.
333 (compiler-style-warn "The default initial element ~S is not a ~S."
334 (sb!vm:saetp-initial-element-default saetp)
336 (let* ((n-bits-per-element (sb!vm:saetp-n-bits saetp))
337 (typecode (sb!vm:saetp-typecode saetp))
338 (n-pad-elements (sb!vm:saetp-n-pad-elements saetp))
339 (padded-length-form (if (zerop n-pad-elements)
341 `(+ length ,n-pad-elements)))
344 ((= n-bits-per-element 0) 0)
345 ((>= n-bits-per-element sb!vm:n-word-bits)
346 `(* ,padded-length-form
347 (the fixnum ; i.e., not RATIO
348 ,(/ n-bits-per-element sb!vm:n-word-bits))))
350 (let ((n-elements-per-word (/ sb!vm:n-word-bits
351 n-bits-per-element)))
352 (declare (type index n-elements-per-word)) ; i.e., not RATIO
353 `(ceiling ,padded-length-form ,n-elements-per-word))))))
355 `(truly-the ,result-type-spec
356 (allocate-vector ,typecode length ,n-words-form))
357 '((declare (type index length)))))))
359 ;;; The list type restriction does not ensure that the result will be a
360 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
361 ;;; and displaced-to keywords ensures that it will be simple.
363 ;;; FIXME: should we generalize this transform to non-simple (though
364 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
365 ;;; deal with those? Maybe when the DEFTRANSFORM
366 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
368 (deftransform make-array ((dims &key element-type)
370 (unless (or (null element-type) (constant-lvar-p element-type))
371 (give-up-ir1-transform
372 "The element-type is not constant; cannot open code array creation."))
373 (unless (constant-lvar-p dims)
374 (give-up-ir1-transform
375 "The dimension list is not constant; cannot open code array creation."))
376 (let ((dims (lvar-value dims)))
377 (unless (every #'integerp dims)
378 (give-up-ir1-transform
379 "The dimension list contains something other than an integer: ~S"
381 (if (= (length dims) 1)
382 `(make-array ',(car dims)
384 '(:element-type element-type)))
385 (let* ((total-size (reduce #'* dims))
388 ,(cond ((null element-type) t)
389 ((and (constant-lvar-p element-type)
390 (ir1-transform-specifier-type
391 (lvar-value element-type)))
392 (sb!xc:upgraded-array-element-type
393 (lvar-value element-type)))
395 ,(make-list rank :initial-element '*))))
396 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank)))
397 (setf (%array-fill-pointer header) ,total-size)
398 (setf (%array-fill-pointer-p header) nil)
399 (setf (%array-available-elements header) ,total-size)
400 (setf (%array-data-vector header)
401 (make-array ,total-size
403 '(:element-type element-type))))
404 (setf (%array-displaced-p header) nil)
406 (mapcar (lambda (dim)
407 `(setf (%array-dimension header ,(incf axis))
410 (truly-the ,spec header))))))
412 ;;;; miscellaneous properties of arrays
414 ;;; Transforms for various array properties. If the property is know
415 ;;; at compile time because of a type spec, use that constant value.
417 ;;; Most of this logic may end up belonging in code/late-type.lisp;
418 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
419 ;;; maybe this is just too sloppy for actual type logic. -- CSR,
421 (defun array-type-dimensions-or-give-up (type)
423 (array-type (array-type-dimensions type))
425 (let ((types (union-type-types type)))
426 ;; there are at least two types, right?
427 (aver (> (length types) 1))
428 (let ((result (array-type-dimensions-or-give-up (car types))))
429 (dolist (type (cdr types) result)
430 (unless (equal (array-type-dimensions-or-give-up type) result)
431 (give-up-ir1-transform))))))
432 ;; FIXME: intersection type [e.g. (and (array * (*)) (satisfies foo)) ]
433 (t (give-up-ir1-transform))))
435 (defun conservative-array-type-complexp (type)
437 (array-type (array-type-complexp type))
439 (let ((types (union-type-types type)))
440 (aver (> (length types) 1))
441 (let ((result (conservative-array-type-complexp (car types))))
442 (dolist (type (cdr types) result)
443 (unless (eq (conservative-array-type-complexp type) result)
444 (return-from conservative-array-type-complexp :maybe))))))
445 ;; FIXME: intersection type
448 ;;; If we can tell the rank from the type info, use it instead.
449 (deftransform array-rank ((array))
450 (let ((array-type (lvar-type array)))
451 (let ((dims (array-type-dimensions-or-give-up array-type)))
452 (if (not (listp dims))
453 (give-up-ir1-transform
454 "The array rank is not known at compile time: ~S"
458 ;;; If we know the dimensions at compile time, just use it. Otherwise,
459 ;;; if we can tell that the axis is in bounds, convert to
460 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
461 ;;; (if it's simple and a vector).
462 (deftransform array-dimension ((array axis)
464 (unless (constant-lvar-p axis)
465 (give-up-ir1-transform "The axis is not constant."))
466 (let ((array-type (lvar-type array))
467 (axis (lvar-value axis)))
468 (let ((dims (array-type-dimensions-or-give-up array-type)))
470 (give-up-ir1-transform
471 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
472 (unless (> (length dims) axis)
473 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
476 (let ((dim (nth axis dims)))
477 (cond ((integerp dim)
480 (ecase (conservative-array-type-complexp array-type)
482 '(%array-dimension array 0))
486 (give-up-ir1-transform
487 "can't tell whether array is simple"))))
489 '(%array-dimension array axis)))))))
491 ;;; If the length has been declared and it's simple, just return it.
492 (deftransform length ((vector)
493 ((simple-array * (*))))
494 (let ((type (lvar-type vector)))
495 (let ((dims (array-type-dimensions-or-give-up type)))
496 (unless (and (listp dims) (integerp (car dims)))
497 (give-up-ir1-transform
498 "Vector length is unknown, must call LENGTH at runtime."))
501 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
502 ;;; simple, it will extract the length slot from the vector. It it's
503 ;;; complex, it will extract the fill pointer slot from the array
505 (deftransform length ((vector) (vector))
506 '(vector-length vector))
508 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
509 ;;; compile-time constant.
510 (deftransform vector-length ((vector))
511 (let ((vtype (lvar-type vector)))
512 (let ((dim (first (array-type-dimensions-or-give-up vtype))))
514 (give-up-ir1-transform))
515 (when (conservative-array-type-complexp vtype)
516 (give-up-ir1-transform))
519 ;;; Again, if we can tell the results from the type, just use it.
520 ;;; Otherwise, if we know the rank, convert into a computation based
521 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
522 ;;; multiplications because we know that the total size must be an
524 (deftransform array-total-size ((array)
526 (let ((array-type (lvar-type array)))
527 (let ((dims (array-type-dimensions-or-give-up array-type)))
529 (give-up-ir1-transform "can't tell the rank at compile time"))
531 (do ((form 1 `(truly-the index
532 (* (array-dimension array ,i) ,form)))
534 ((= i (length dims)) form))
535 (reduce #'* dims)))))
537 ;;; Only complex vectors have fill pointers.
538 (deftransform array-has-fill-pointer-p ((array))
539 (let ((array-type (lvar-type array)))
540 (let ((dims (array-type-dimensions-or-give-up array-type)))
541 (if (and (listp dims) (not (= (length dims) 1)))
543 (ecase (conservative-array-type-complexp array-type)
549 (give-up-ir1-transform
550 "The array type is ambiguous; must call ~
551 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
553 ;;; Primitive used to verify indices into arrays. If we can tell at
554 ;;; compile-time or we are generating unsafe code, don't bother with
556 (deftransform %check-bound ((array dimension index) * * :node node)
557 (cond ((policy node (= insert-array-bounds-checks 0))
559 ((not (constant-lvar-p dimension))
560 (give-up-ir1-transform))
562 (let ((dim (lvar-value dimension)))
563 `(the (integer 0 (,dim)) index)))))
567 ;;; This checks to see whether the array is simple and the start and
568 ;;; end are in bounds. If so, it proceeds with those values.
569 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
570 ;;; may be further optimized.
572 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
573 ;;; START-VAR and END-VAR to the start and end of the designated
574 ;;; portion of the data vector. SVALUE and EVALUE are any start and
575 ;;; end specified to the original operation, and are factored into the
576 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
577 ;;; offset of all displacements encountered, and does not include
580 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
581 ;;; forced to be inline, overriding the ordinary judgment of the
582 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
583 ;;; fairly picky about their arguments, figuring that if you haven't
584 ;;; bothered to get all your ducks in a row, you probably don't care
585 ;;; that much about speed anyway! But in some cases it makes sense to
586 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
587 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
588 ;;; sense to use FORCE-INLINE option in that case.
589 (def!macro with-array-data (((data-var array &key offset-var)
590 (start-var &optional (svalue 0))
591 (end-var &optional (evalue nil))
594 (once-only ((n-array array)
595 (n-svalue `(the index ,svalue))
596 (n-evalue `(the (or index null) ,evalue)))
597 `(multiple-value-bind (,data-var
600 ,@(when offset-var `(,offset-var)))
601 (if (not (array-header-p ,n-array))
602 (let ((,n-array ,n-array))
603 (declare (type (simple-array * (*)) ,n-array))
604 ,(once-only ((n-len `(length ,n-array))
605 (n-end `(or ,n-evalue ,n-len)))
606 `(if (<= ,n-svalue ,n-end ,n-len)
608 (values ,n-array ,n-svalue ,n-end 0)
609 (failed-%with-array-data ,n-array
612 (,(if force-inline '%with-array-data-macro '%with-array-data)
613 ,n-array ,n-svalue ,n-evalue))
616 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
617 ;;; DEFTRANSFORMs and DEFUNs.
618 (def!macro %with-array-data-macro (array
625 (with-unique-names (size defaulted-end data cumulative-offset)
626 `(let* ((,size (array-total-size ,array))
629 (unless (or ,unsafe? (<= ,end ,size))
631 `(error 'bounding-indices-bad-error
632 :datum (cons ,start ,end)
633 :expected-type `(cons (integer 0 ,',size)
634 (integer ,',start ,',size))
636 `(failed-%with-array-data ,array ,start ,end)))
639 (unless (or ,unsafe? (<= ,start ,defaulted-end))
641 `(error 'bounding-indices-bad-error
642 :datum (cons ,start ,end)
643 :expected-type `(cons (integer 0 ,',size)
644 (integer ,',start ,',size))
646 `(failed-%with-array-data ,array ,start ,end)))
647 (do ((,data ,array (%array-data-vector ,data))
648 (,cumulative-offset 0
649 (+ ,cumulative-offset
650 (%array-displacement ,data))))
651 ((not (array-header-p ,data))
652 (values (the (simple-array ,element-type 1) ,data)
653 (the index (+ ,cumulative-offset ,start))
654 (the index (+ ,cumulative-offset ,defaulted-end))
655 (the index ,cumulative-offset)))
656 (declare (type index ,cumulative-offset))))))
658 (deftransform %with-array-data ((array start end)
659 ;; It might very well be reasonable to
660 ;; allow general ARRAY here, I just
661 ;; haven't tried to understand the
662 ;; performance issues involved. --
663 ;; WHN, and also CSR 2002-05-26
664 ((or vector simple-array) index (or index null))
667 :policy (> speed space))
668 "inline non-SIMPLE-vector-handling logic"
669 (let ((element-type (upgraded-element-type-specifier-or-give-up array))
670 (type (lvar-type array)))
671 (if (and (array-type-p type)
672 (listp (array-type-dimensions type))
673 (not (null (cdr (array-type-dimensions type)))))
674 ;; If it's a simple multidimensional array, then just return its
675 ;; data vector directly rather than going through
676 ;; %WITH-ARRAY-DATA-MACRO. SBCL doesn't generally generate code
677 ;; that would use this currently, but we have encouraged users
678 ;; to use WITH-ARRAY-DATA and we may use it ourselves at some
679 ;; point in the future for optimized libraries or similar.
680 `(let ((data (truly-the (simple-array ,element-type (*))
681 (%array-data-vector array))))
682 (values data 0 (length data) 0))
683 `(%with-array-data-macro array start end
684 :unsafe? ,(policy node (= safety 0))
685 :element-type ,element-type))))
689 ;;; We convert all typed array accessors into AREF and %ASET with type
690 ;;; assertions on the array.
691 (macrolet ((define-bit-frob (reffer setter simplep)
693 (define-source-transform ,reffer (a &rest i)
694 `(aref (the (,',(if simplep 'simple-array 'array)
696 ,(mapcar (constantly '*) i))
698 (define-source-transform ,setter (a &rest i)
699 `(%aset (the (,',(if simplep 'simple-array 'array)
701 ,(cdr (mapcar (constantly '*) i)))
703 (define-bit-frob sbit %sbitset t)
704 (define-bit-frob bit %bitset nil))
705 (macrolet ((define-frob (reffer setter type)
707 (define-source-transform ,reffer (a i)
708 `(aref (the ,',type ,a) ,i))
709 (define-source-transform ,setter (a i v)
710 `(%aset (the ,',type ,a) ,i ,v)))))
711 (define-frob svref %svset simple-vector)
712 (define-frob schar %scharset simple-string)
713 (define-frob char %charset string))
715 (macrolet (;; This is a handy macro for computing the row-major index
716 ;; given a set of indices. We wrap each index with a call
717 ;; to %CHECK-BOUND to ensure that everything works out
718 ;; correctly. We can wrap all the interior arithmetic with
719 ;; TRULY-THE INDEX because we know the resultant
720 ;; row-major index must be an index.
721 (with-row-major-index ((array indices index &optional new-value)
723 `(let (n-indices dims)
724 (dotimes (i (length ,indices))
725 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
726 (push (make-symbol (format nil "DIM-~D" i)) dims))
727 (setf n-indices (nreverse n-indices))
728 (setf dims (nreverse dims))
729 `(lambda (,',array ,@n-indices
730 ,@',(when new-value (list new-value)))
731 (let* (,@(let ((,index -1))
732 (mapcar (lambda (name)
733 `(,name (array-dimension
740 (do* ((dims dims (cdr dims))
741 (indices n-indices (cdr indices))
742 (last-dim nil (car dims))
743 (form `(%check-bound ,',array
755 ((null (cdr dims)) form)))))
758 ;; Just return the index after computing it.
759 (deftransform array-row-major-index ((array &rest indices))
760 (with-row-major-index (array indices index)
763 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
764 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
765 ;; expression for the row major index.
766 (deftransform aref ((array &rest indices))
767 (with-row-major-index (array indices index)
768 (hairy-data-vector-ref array index)))
770 (deftransform %aset ((array &rest stuff))
771 (let ((indices (butlast stuff)))
772 (with-row-major-index (array indices index new-value)
773 (hairy-data-vector-set array index new-value)))))
775 ;; For AREF of vectors we do the bounds checking in the callee. This
776 ;; lets us do a significantly more efficient check for simple-arrays
777 ;; without bloating the code. If we already know the type of the array
778 ;; with sufficient precision, skip directly to DATA-VECTOR-REF.
779 (deftransform aref ((array index) (t t) * :node node)
780 (let ((type (lvar-type array)))
781 (cond ((and (array-type-p type)
782 (null (array-type-complexp type))
783 (not (eql (extract-upgraded-element-type array)
785 (eql (length (array-type-dimensions type)) 1))
786 `(data-vector-ref array (%check-bound array
787 (array-dimension array 0)
789 ((policy node (zerop insert-array-bounds-checks))
790 `(hairy-data-vector-ref array index))
792 `(hairy-data-vector-ref/check-bounds array index)))))
794 (deftransform %aset ((array index new-value) (t t t) * :node node)
795 (if (policy node (zerop insert-array-bounds-checks))
796 `(hairy-data-vector-set array index new-value)
797 `(hairy-data-vector-set/check-bounds array index new-value)))
799 ;;; But if we find out later that there's some useful type information
800 ;;; available, switch back to the normal one to give other transforms
802 (macrolet ((define (name transform-to extra extra-type)
803 `(deftransform ,name ((array index ,@extra))
804 (let ((type (lvar-type array))
805 (element-type (extract-upgraded-element-type array)))
806 ;; If an element type has been declared, we want to
807 ;; use that information it for type checking (even
808 ;; if the access can't be optimized due to the array
809 ;; not being simple).
810 (when (and (eql element-type *wild-type*)
811 ;; This type logic corresponds to the special
812 ;; case for strings in HAIRY-DATA-VECTOR-REF
813 ;; (generic/vm-tran.lisp)
814 (not (csubtypep type (specifier-type 'simple-string))))
815 (when (or (not (array-type-p type))
816 ;; If it's a simple array, we might be able
817 ;; to inline the access completely.
818 (not (null (array-type-complexp type))))
819 (give-up-ir1-transform
820 "Upgraded element type of array is not known at compile time."))))
821 `(,',transform-to array
823 (array-dimension array 0)
826 (define hairy-data-vector-ref/check-bounds
827 hairy-data-vector-ref nil nil)
828 (define hairy-data-vector-set/check-bounds
829 hairy-data-vector-set (new-value) (*)))
831 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
832 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
833 ;;; array total size.
834 (deftransform row-major-aref ((array index))
835 `(hairy-data-vector-ref array
836 (%check-bound array (array-total-size array) index)))
837 (deftransform %set-row-major-aref ((array index new-value))
838 `(hairy-data-vector-set array
839 (%check-bound array (array-total-size array) index)
842 ;;;; bit-vector array operation canonicalization
844 ;;;; We convert all bit-vector operations to have the result array
845 ;;;; specified. This allows any result allocation to be open-coded,
846 ;;;; and eliminates the need for any VM-dependent transforms to handle
849 (macrolet ((def (fun)
851 (deftransform ,fun ((bit-array-1 bit-array-2
852 &optional result-bit-array)
853 (bit-vector bit-vector &optional null) *
854 :policy (>= speed space))
855 `(,',fun bit-array-1 bit-array-2
856 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
857 ;; If result is T, make it the first arg.
858 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
859 (bit-vector bit-vector (eql t)) *)
860 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
872 ;;; Similar for BIT-NOT, but there is only one arg...
873 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
874 (bit-vector &optional null) *
875 :policy (>= speed space))
876 '(bit-not bit-array-1
877 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
878 (deftransform bit-not ((bit-array-1 result-bit-array)
879 (bit-vector (eql t)))
880 '(bit-not bit-array-1 bit-array-1))
882 ;;; Pick off some constant cases.
883 (defoptimizer (array-header-p derive-type) ((array))
884 (let ((type (lvar-type array)))
885 (cond ((not (array-type-p type))
886 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
889 (let ((dims (array-type-dimensions type)))
890 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
892 (specifier-type 'null))
893 ((and (listp dims) (/= (length dims) 1))
894 ;; multi-dimensional array, will have a header
895 (specifier-type '(eql t)))
896 ((eql (array-type-complexp type) t)
897 (specifier-type '(eql t)))