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 type is
28 ;;; going to be the array upgraded element type. Secondary return value is the
29 ;;; known supertype of the upgraded-array-element-type, if if the exact
30 ;;; U-A-E-T is not known. (If it is NIL, the primary return value is as good
32 (defun extract-upgraded-element-type (array)
33 (let ((type (lvar-type array)))
35 ;; Note that this IF mightn't be satisfied even if the runtime
36 ;; value is known to be a subtype of some specialized ARRAY, because
37 ;; we can have values declared e.g. (AND SIMPLE-VECTOR UNKNOWN-TYPE),
38 ;; which are represented in the compiler as INTERSECTION-TYPE, not
41 (values (array-type-specialized-element-type type) nil))
42 ;; fix for bug #396. This type logic corresponds to the special case for
43 ;; strings in HAIRY-DATA-VECTOR-REF (generic/vm-tran.lisp)
44 ((csubtypep type (specifier-type 'string))
46 ((csubtypep type (specifier-type '(array character (*))))
47 (values (specifier-type 'character) nil))
49 ((csubtypep type (specifier-type '(array base-char (*))))
50 (values (specifier-type 'base-char) nil))
51 ((csubtypep type (specifier-type '(array nil (*))))
52 (values *empty-type* nil))
55 (values *wild-type* (specifier-type 'character)))))
57 ;; KLUDGE: there is no good answer here, but at least
58 ;; *wild-type* won't cause HAIRY-DATA-VECTOR-{REF,SET} to be
59 ;; erroneously optimized (see generic/vm-tran.lisp) -- CSR,
61 (values *wild-type* nil)))))
63 (defun extract-declared-element-type (array)
64 (let ((type (lvar-type array)))
65 (if (array-type-p type)
66 (array-type-element-type type)
69 ;;; The ``new-value'' for array setters must fit in the array, and the
70 ;;; return type is going to be the same as the new-value for SETF
72 (defun assert-new-value-type (new-value array)
73 (let ((type (lvar-type array)))
74 (when (array-type-p type)
77 (array-type-specialized-element-type type)
78 (lexenv-policy (node-lexenv (lvar-dest new-value))))))
79 (lvar-type new-value))
81 (defun assert-array-complex (array)
84 (make-array-type :complexp t
85 :element-type *wild-type*)
86 (lexenv-policy (node-lexenv (lvar-dest array))))
89 ;;; Return true if ARG is NIL, or is a constant-lvar whose
90 ;;; value is NIL, false otherwise.
91 (defun unsupplied-or-nil (arg)
92 (declare (type (or lvar null) arg))
94 (and (constant-lvar-p arg)
95 (not (lvar-value arg)))))
97 ;;;; DERIVE-TYPE optimizers
99 ;;; Array operations that use a specific number of indices implicitly
100 ;;; assert that the array is of that rank.
101 (defun assert-array-rank (array rank)
104 (specifier-type `(array * ,(make-list rank :initial-element '*)))
105 (lexenv-policy (node-lexenv (lvar-dest array)))))
107 (defun derive-aref-type (array)
108 (multiple-value-bind (uaet other) (extract-upgraded-element-type array)
111 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
112 (assert-array-rank array (length indices))
115 (defoptimizer (aref derive-type) ((array &rest indices) node)
116 (assert-array-rank array (length indices))
117 (derive-aref-type array))
119 (defoptimizer (%aset derive-type) ((array &rest stuff))
120 (assert-array-rank array (1- (length stuff)))
121 (assert-new-value-type (car (last stuff)) array))
123 (macrolet ((define (name)
124 `(defoptimizer (,name derive-type) ((array index))
125 (derive-aref-type array))))
126 (define hairy-data-vector-ref)
127 (define hairy-data-vector-ref/check-bounds)
128 (define data-vector-ref))
131 (defoptimizer (data-vector-ref-with-offset derive-type) ((array index offset))
132 (derive-aref-type array))
134 (macrolet ((define (name)
135 `(defoptimizer (,name derive-type) ((array index new-value))
136 (assert-new-value-type new-value array))))
137 (define hairy-data-vector-set)
138 (define hairy-data-vector-set/check-bounds)
139 (define data-vector-set))
142 (defoptimizer (data-vector-set-with-offset derive-type) ((array index offset new-value))
143 (assert-new-value-type new-value array))
145 ;;; Figure out the type of the data vector if we know the argument
147 (defun derive-%with-array-data/mumble-type (array)
148 (let ((atype (lvar-type array)))
149 (when (array-type-p atype)
151 `(simple-array ,(type-specifier
152 (array-type-specialized-element-type atype))
154 (defoptimizer (%with-array-data derive-type) ((array start end))
155 (derive-%with-array-data/mumble-type array))
156 (defoptimizer (%with-array-data/fp derive-type) ((array start end))
157 (derive-%with-array-data/mumble-type array))
159 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
160 (assert-array-rank array (length indices))
163 (defoptimizer (row-major-aref derive-type) ((array index))
164 (derive-aref-type array))
166 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
167 (assert-new-value-type new-value array))
169 (defoptimizer (make-array derive-type)
170 ((dims &key initial-element element-type initial-contents
171 adjustable fill-pointer displaced-index-offset displaced-to))
172 (let ((simple (and (unsupplied-or-nil adjustable)
173 (unsupplied-or-nil displaced-to)
174 (unsupplied-or-nil fill-pointer))))
175 (or (careful-specifier-type
176 `(,(if simple 'simple-array 'array)
177 ,(cond ((not element-type) t)
178 ((constant-lvar-p element-type)
179 (let ((ctype (careful-specifier-type
180 (lvar-value element-type))))
182 ((or (null ctype) (unknown-type-p ctype)) '*)
183 (t (sb!xc:upgraded-array-element-type
184 (lvar-value element-type))))))
187 ,(cond ((constant-lvar-p dims)
188 (let* ((val (lvar-value dims))
189 (cdims (if (listp val) val (list val))))
193 ((csubtypep (lvar-type dims)
194 (specifier-type 'integer))
198 (specifier-type 'array))))
200 ;;; Complex array operations should assert that their array argument
201 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
202 (defoptimizer (fill-pointer derive-type) ((vector))
203 (assert-array-complex vector))
204 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
205 (declare (ignorable index))
206 (assert-array-complex vector))
208 (defoptimizer (vector-push derive-type) ((object vector))
209 (declare (ignorable object))
210 (assert-array-complex vector))
211 (defoptimizer (vector-push-extend derive-type)
212 ((object vector &optional index))
213 (declare (ignorable object index))
214 (assert-array-complex vector))
215 (defoptimizer (vector-pop derive-type) ((vector))
216 (assert-array-complex vector))
220 ;;; Convert VECTOR into a MAKE-ARRAY.
221 (define-source-transform vector (&rest elements)
222 `(make-array ,(length elements) :initial-contents (list ,@elements)))
224 ;;; Just convert it into a MAKE-ARRAY.
225 (deftransform make-string ((length &key
226 (element-type 'character)
228 #.*default-init-char-form*)))
229 `(the simple-string (make-array (the index length)
230 :element-type element-type
231 ,@(when initial-element
232 '(:initial-element initial-element)))))
234 ;;; Prevent open coding DIMENSION and :INITIAL-CONTENTS arguments,
235 ;;; so that we can pick them apart.
236 (define-source-transform make-array (&whole form dimensions &rest keyargs
238 (if (and (fun-lexically-notinline-p 'list)
239 (fun-lexically-notinline-p 'vector))
241 `(locally (declare (notinline list vector))
242 ;; Transform '(3) style dimensions to integer args directly.
243 ,(if (sb!xc:constantp dimensions env)
244 (let ((dims (constant-form-value dimensions env)))
245 (if (and (listp dims) (= 1 (length dims)))
246 `(make-array ',(car dims) ,@keyargs)
250 ;;; This baby is a bit of a monster, but it takes care of any MAKE-ARRAY
251 ;;; call which creates a vector with a known element type -- and tries
252 ;;; to do a good job with all the different ways it can happen.
253 (defun transform-make-array-vector (length element-type initial-element
254 initial-contents call)
255 (aver (or (not element-type) (constant-lvar-p element-type)))
256 (let* ((c-length (when (constant-lvar-p length)
257 (lvar-value length)))
258 (elt-spec (if element-type
259 (lvar-value element-type)
261 (elt-ctype (ir1-transform-specifier-type elt-spec))
262 (saetp (if (unknown-type-p elt-ctype)
263 (give-up-ir1-transform "~S is an unknown type: ~S"
264 :element-type elt-spec)
265 (find-saetp-by-ctype elt-ctype)))
266 (default-initial-element (sb!vm:saetp-initial-element-default saetp))
267 (n-bits (sb!vm:saetp-n-bits saetp))
268 (typecode (sb!vm:saetp-typecode saetp))
269 (n-pad-elements (sb!vm:saetp-n-pad-elements saetp))
272 (ceiling (* (+ c-length n-pad-elements) n-bits)
274 (let ((padded-length-form (if (zerop n-pad-elements)
276 `(+ length ,n-pad-elements))))
279 ((>= n-bits sb!vm:n-word-bits)
280 `(* ,padded-length-form
282 ,(the fixnum (/ n-bits sb!vm:n-word-bits))))
284 (let ((n-elements-per-word (/ sb!vm:n-word-bits n-bits)))
285 (declare (type index n-elements-per-word)) ; i.e., not RATIO
286 `(ceiling ,padded-length-form ,n-elements-per-word)))))))
288 `(simple-array ,(sb!vm:saetp-specifier saetp) (,(or c-length '*))))
290 `(truly-the ,result-spec
291 (allocate-vector ,typecode (the index length) ,n-words-form))))
292 (cond ((and initial-element initial-contents)
293 (abort-ir1-transform "Both ~S and ~S specified."
294 :initial-contents :initial-element))
295 ;; :INITIAL-CONTENTS (LIST ...), (VECTOR ...) and `(1 1 ,x) with a
297 ((and initial-contents c-length
298 (lvar-matches initial-contents
299 :fun-names '(list vector sb!impl::backq-list)
300 :arg-count c-length))
301 (let ((parameters (eliminate-keyword-args
302 call 1 '((:element-type element-type)
303 (:initial-contents initial-contents))))
304 (elt-vars (make-gensym-list c-length))
305 (lambda-list '(length)))
306 (splice-fun-args initial-contents :any c-length)
307 (dolist (p parameters)
310 (if (eq p 'initial-contents)
313 `(lambda ,lambda-list
314 (declare (type ,elt-spec ,@elt-vars)
315 (ignorable ,@lambda-list))
316 (truly-the ,result-spec
317 (initialize-vector ,alloc-form ,@elt-vars)))))
318 ;; constant :INITIAL-CONTENTS and LENGTH
319 ((and initial-contents c-length (constant-lvar-p initial-contents))
320 (let ((contents (lvar-value initial-contents)))
321 (unless (= c-length (length contents))
322 (abort-ir1-transform "~S has ~S elements, vector length is ~S."
323 :initial-contents (length contents) c-length))
324 (let ((parameters (eliminate-keyword-args
325 call 1 '((:element-type element-type)
326 (:initial-contents initial-contents)))))
327 `(lambda (length ,@parameters)
328 (declare (ignorable ,@parameters))
329 (truly-the ,result-spec
330 (initialize-vector ,alloc-form
331 ,@(map 'list (lambda (elt)
332 `(the ,elt-spec ',elt))
334 ;; any other :INITIAL-CONTENTS
336 (let ((parameters (eliminate-keyword-args
337 call 1 '((:element-type element-type)
338 (:initial-contents initial-contents)))))
339 `(lambda (length ,@parameters)
340 (declare (ignorable ,@parameters))
341 (unless (= length (length initial-contents))
342 (error "~S has ~S elements, vector length is ~S."
343 :initial-contents (length initial-contents) length))
344 (truly-the ,result-spec
345 (replace ,alloc-form initial-contents)))))
346 ;; :INITIAL-ELEMENT, not EQL to the default
347 ((and initial-element
348 (or (not (constant-lvar-p initial-element))
349 (not (eql default-initial-element (lvar-value initial-element)))))
350 (let ((parameters (eliminate-keyword-args
351 call 1 '((:element-type element-type)
352 (:initial-element initial-element))))
353 (init (if (constant-lvar-p initial-element)
354 (list 'quote (lvar-value initial-element))
356 `(lambda (length ,@parameters)
357 (declare (ignorable ,@parameters))
358 (truly-the ,result-spec
359 (fill ,alloc-form (the ,elt-spec ,init))))))
360 ;; just :ELEMENT-TYPE, or maybe with :INITIAL-ELEMENT EQL to the
364 (unless (ctypep default-initial-element elt-ctype)
365 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
366 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
367 ;; INITIAL-ELEMENT is not supplied, the consequences of later
368 ;; reading an uninitialized element of new-array are undefined,"
369 ;; so this could be legal code as long as the user plans to
370 ;; write before he reads, and if he doesn't we're free to do
371 ;; anything we like. But in case the user doesn't know to write
372 ;; elements before he reads elements (or to read manuals before
373 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
374 ;; didn't realize this.
376 (compiler-warn "~S ~S is not a ~S"
377 :initial-element default-initial-element
379 (compiler-style-warn "The default initial element ~S is not a ~S."
380 default-initial-element
382 (let ((parameters (eliminate-keyword-args
383 call 1 '((:element-type element-type)
384 (:initial-element initial-element)))))
385 `(lambda (length ,@parameters)
386 (declare (ignorable ,@parameters))
389 ;;; IMPORTANT: The order of these three MAKE-ARRAY forms matters: the least
390 ;;; specific must come first, otherwise suboptimal transforms will result for
393 (deftransform make-array ((dims &key initial-element element-type
394 adjustable fill-pointer)
396 (when (null initial-element)
397 (give-up-ir1-transform))
398 (let* ((eltype (cond ((not element-type) t)
399 ((not (constant-lvar-p element-type))
400 (give-up-ir1-transform
401 "ELEMENT-TYPE is not constant."))
403 (lvar-value element-type))))
404 (eltype-type (ir1-transform-specifier-type eltype))
405 (saetp (find-if (lambda (saetp)
406 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
407 sb!vm:*specialized-array-element-type-properties*))
408 (creation-form `(make-array dims
409 :element-type ',(type-specifier (sb!vm:saetp-ctype saetp))
411 '(:fill-pointer fill-pointer))
413 '(:adjustable adjustable)))))
416 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
418 (cond ((and (constant-lvar-p initial-element)
419 (eql (lvar-value initial-element)
420 (sb!vm:saetp-initial-element-default saetp)))
423 ;; error checking for target, disabled on the host because
424 ;; (CTYPE-OF #\Null) is not possible.
426 (when (constant-lvar-p initial-element)
427 (let ((value (lvar-value initial-element)))
429 ((not (ctypep value (sb!vm:saetp-ctype saetp)))
430 ;; this case will cause an error at runtime, so we'd
431 ;; better WARN about it now.
432 (warn 'array-initial-element-mismatch
433 :format-control "~@<~S is not a ~S (which is the ~
438 (type-specifier (sb!vm:saetp-ctype saetp))
439 'upgraded-array-element-type
441 ((not (ctypep value eltype-type))
442 ;; this case will not cause an error at runtime, but
443 ;; it's still worth STYLE-WARNing about.
444 (compiler-style-warn "~S is not a ~S."
446 `(let ((array ,creation-form))
447 (multiple-value-bind (vector)
448 (%data-vector-and-index array 0)
449 (fill vector (the ,(sb!vm:saetp-specifier saetp) initial-element)))
452 ;;; The list type restriction does not ensure that the result will be a
453 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
454 ;;; and displaced-to keywords ensures that it will be simple.
456 ;;; FIXME: should we generalize this transform to non-simple (though
457 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
458 ;;; deal with those? Maybe when the DEFTRANSFORM
459 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
461 (deftransform make-array ((dims &key
462 element-type initial-element initial-contents)
464 (:element-type (constant-arg *))
466 (:initial-contents *))
470 (when (lvar-matches dims :fun-names '(list) :arg-count 1)
471 (let ((length (car (splice-fun-args dims :any 1))))
472 (return-from make-array
473 (transform-make-array-vector length
478 (unless (constant-lvar-p dims)
479 (give-up-ir1-transform
480 "The dimension list is not constant; cannot open code array creation."))
481 (let ((dims (lvar-value dims)))
482 (unless (every #'integerp dims)
483 (give-up-ir1-transform
484 "The dimension list contains something other than an integer: ~S"
486 (if (= (length dims) 1)
487 `(make-array ',(car dims)
489 '(:element-type element-type))
490 ,@(when initial-element
491 '(:initial-element initial-element))
492 ,@(when initial-contents
493 '(:initial-contents initial-contents)))
494 (let* ((total-size (reduce #'* dims))
497 ,(cond ((null element-type) t)
498 ((and (constant-lvar-p element-type)
499 (ir1-transform-specifier-type
500 (lvar-value element-type)))
501 (sb!xc:upgraded-array-element-type
502 (lvar-value element-type)))
504 ,(make-list rank :initial-element '*))))
505 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank))
506 (data (make-array ,total-size
508 '(:element-type element-type))
509 ,@(when initial-element
510 '(:initial-element initial-element)))))
511 ,@(when initial-contents
512 ;; FIXME: This is could be open coded at least a bit too
513 `((sb!impl::fill-data-vector data ',dims initial-contents)))
514 (setf (%array-fill-pointer header) ,total-size)
515 (setf (%array-fill-pointer-p header) nil)
516 (setf (%array-available-elements header) ,total-size)
517 (setf (%array-data-vector header) data)
518 (setf (%array-displaced-p header) nil)
519 (setf (%array-displaced-from header) nil)
521 (mapcar (lambda (dim)
522 `(setf (%array-dimension header ,(incf axis))
525 (truly-the ,spec header)))))))
527 (deftransform make-array ((dims &key element-type initial-element initial-contents)
529 (:element-type (constant-arg *))
531 (:initial-contents *))
534 (transform-make-array-vector dims
540 ;;;; miscellaneous properties of arrays
542 ;;; Transforms for various array properties. If the property is know
543 ;;; at compile time because of a type spec, use that constant value.
545 ;;; Most of this logic may end up belonging in code/late-type.lisp;
546 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
547 ;;; maybe this is just too sloppy for actual type logic. -- CSR,
549 (defun array-type-dimensions-or-give-up (type)
551 (array-type (array-type-dimensions type))
553 (let ((types (union-type-types type)))
554 ;; there are at least two types, right?
555 (aver (> (length types) 1))
556 (let ((result (array-type-dimensions-or-give-up (car types))))
557 (dolist (type (cdr types) result)
558 (unless (equal (array-type-dimensions-or-give-up type) result)
559 (give-up-ir1-transform))))))
560 ;; FIXME: intersection type [e.g. (and (array * (*)) (satisfies foo)) ]
561 (t (give-up-ir1-transform))))
563 (defun conservative-array-type-complexp (type)
565 (array-type (array-type-complexp type))
567 (let ((types (union-type-types type)))
568 (aver (> (length types) 1))
569 (let ((result (conservative-array-type-complexp (car types))))
570 (dolist (type (cdr types) result)
571 (unless (eq (conservative-array-type-complexp type) result)
572 (return-from conservative-array-type-complexp :maybe))))))
573 ;; FIXME: intersection type
576 ;;; If we can tell the rank from the type info, use it instead.
577 (deftransform array-rank ((array))
578 (let ((array-type (lvar-type array)))
579 (let ((dims (array-type-dimensions-or-give-up array-type)))
582 ((eq t (array-type-complexp array-type))
583 '(%array-rank array))
585 `(if (array-header-p array)
589 ;;; If we know the dimensions at compile time, just use it. Otherwise,
590 ;;; if we can tell that the axis is in bounds, convert to
591 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
592 ;;; (if it's simple and a vector).
593 (deftransform array-dimension ((array axis)
595 (unless (constant-lvar-p axis)
596 (give-up-ir1-transform "The axis is not constant."))
597 ;; Dimensions may change thanks to ADJUST-ARRAY, so we need the
598 ;; conservative type.
599 (let ((array-type (lvar-conservative-type array))
600 (axis (lvar-value axis)))
601 (let ((dims (array-type-dimensions-or-give-up array-type)))
603 (give-up-ir1-transform
604 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
605 (unless (> (length dims) axis)
606 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
609 (let ((dim (nth axis dims)))
610 (cond ((integerp dim)
613 (ecase (conservative-array-type-complexp array-type)
615 '(%array-dimension array 0))
617 '(vector-length array))
619 `(if (array-header-p array)
620 (%array-dimension array axis)
621 (vector-length array)))))
623 '(%array-dimension array axis)))))))
625 ;;; If the length has been declared and it's simple, just return it.
626 (deftransform length ((vector)
627 ((simple-array * (*))))
628 (let ((type (lvar-type vector)))
629 (let ((dims (array-type-dimensions-or-give-up type)))
630 (unless (and (listp dims) (integerp (car dims)))
631 (give-up-ir1-transform
632 "Vector length is unknown, must call LENGTH at runtime."))
635 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
636 ;;; simple, it will extract the length slot from the vector. It it's
637 ;;; complex, it will extract the fill pointer slot from the array
639 (deftransform length ((vector) (vector))
640 '(vector-length vector))
642 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
643 ;;; compile-time constant.
644 (deftransform vector-length ((vector))
645 (let ((vtype (lvar-type vector)))
646 (let ((dim (first (array-type-dimensions-or-give-up vtype))))
648 (give-up-ir1-transform))
649 (when (conservative-array-type-complexp vtype)
650 (give-up-ir1-transform))
653 ;;; Again, if we can tell the results from the type, just use it.
654 ;;; Otherwise, if we know the rank, convert into a computation based
655 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
656 ;;; multiplications because we know that the total size must be an
658 (deftransform array-total-size ((array)
660 (let ((array-type (lvar-type array)))
661 (let ((dims (array-type-dimensions-or-give-up array-type)))
663 (give-up-ir1-transform "can't tell the rank at compile time"))
665 (do ((form 1 `(truly-the index
666 (* (array-dimension array ,i) ,form)))
668 ((= i (length dims)) form))
669 (reduce #'* dims)))))
671 ;;; Only complex vectors have fill pointers.
672 (deftransform array-has-fill-pointer-p ((array))
673 (let ((array-type (lvar-type array)))
674 (let ((dims (array-type-dimensions-or-give-up array-type)))
675 (if (and (listp dims) (not (= (length dims) 1)))
677 (ecase (conservative-array-type-complexp array-type)
683 (give-up-ir1-transform
684 "The array type is ambiguous; must call ~
685 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
687 ;;; Primitive used to verify indices into arrays. If we can tell at
688 ;;; compile-time or we are generating unsafe code, don't bother with
690 (deftransform %check-bound ((array dimension index) * * :node node)
691 (cond ((policy node (= insert-array-bounds-checks 0))
693 ((not (constant-lvar-p dimension))
694 (give-up-ir1-transform))
696 (let ((dim (lvar-value dimension)))
697 ;; FIXME: Can SPEED > SAFETY weaken this check to INTEGER?
698 `(the (integer 0 (,dim)) index)))))
702 ;;; This checks to see whether the array is simple and the start and
703 ;;; end are in bounds. If so, it proceeds with those values.
704 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
705 ;;; may be further optimized.
707 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
708 ;;; START-VAR and END-VAR to the start and end of the designated
709 ;;; portion of the data vector. SVALUE and EVALUE are any start and
710 ;;; end specified to the original operation, and are factored into the
711 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
712 ;;; offset of all displacements encountered, and does not include
715 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
716 ;;; forced to be inline, overriding the ordinary judgment of the
717 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
718 ;;; fairly picky about their arguments, figuring that if you haven't
719 ;;; bothered to get all your ducks in a row, you probably don't care
720 ;;; that much about speed anyway! But in some cases it makes sense to
721 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
722 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
723 ;;; sense to use FORCE-INLINE option in that case.
724 (def!macro with-array-data (((data-var array &key offset-var)
725 (start-var &optional (svalue 0))
726 (end-var &optional (evalue nil))
727 &key force-inline check-fill-pointer)
730 (once-only ((n-array array)
731 (n-svalue `(the index ,svalue))
732 (n-evalue `(the (or index null) ,evalue)))
733 (let ((check-bounds (policy env (plusp insert-array-bounds-checks))))
734 `(multiple-value-bind (,data-var
737 ,@(when offset-var `(,offset-var)))
738 (if (not (array-header-p ,n-array))
739 (let ((,n-array ,n-array))
740 (declare (type (simple-array * (*)) ,n-array))
741 ,(once-only ((n-len (if check-fill-pointer
743 `(array-total-size ,n-array)))
744 (n-end `(or ,n-evalue ,n-len)))
746 `(if (<= 0 ,n-svalue ,n-end ,n-len)
747 (values ,n-array ,n-svalue ,n-end 0)
748 ,(if check-fill-pointer
749 `(sequence-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)
750 `(array-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)))
751 `(values ,n-array ,n-svalue ,n-end 0))))
753 `(%with-array-data-macro ,n-array ,n-svalue ,n-evalue
754 :check-bounds ,check-bounds
755 :check-fill-pointer ,check-fill-pointer)
756 (if check-fill-pointer
757 `(%with-array-data/fp ,n-array ,n-svalue ,n-evalue)
758 `(%with-array-data ,n-array ,n-svalue ,n-evalue))))
761 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
762 ;;; DEFTRANSFORMs and DEFUNs.
763 (def!macro %with-array-data-macro (array
770 (with-unique-names (size defaulted-end data cumulative-offset)
771 `(let* ((,size ,(if check-fill-pointer
773 `(array-total-size ,array)))
774 (,defaulted-end (or ,end ,size)))
776 `((unless (<= ,start ,defaulted-end ,size)
777 ,(if check-fill-pointer
778 `(sequence-bounding-indices-bad-error ,array ,start ,end)
779 `(array-bounding-indices-bad-error ,array ,start ,end)))))
780 (do ((,data ,array (%array-data-vector ,data))
781 (,cumulative-offset 0
782 (+ ,cumulative-offset
783 (%array-displacement ,data))))
784 ((not (array-header-p ,data))
785 (values (the (simple-array ,element-type 1) ,data)
786 (the index (+ ,cumulative-offset ,start))
787 (the index (+ ,cumulative-offset ,defaulted-end))
788 (the index ,cumulative-offset)))
789 (declare (type index ,cumulative-offset))))))
791 (defun transform-%with-array-data/muble (array node check-fill-pointer)
792 (let ((element-type (upgraded-element-type-specifier-or-give-up array))
793 (type (lvar-type array))
794 (check-bounds (policy node (plusp insert-array-bounds-checks))))
795 (if (and (array-type-p type)
796 (not (array-type-complexp type))
797 (listp (array-type-dimensions type))
798 (not (null (cdr (array-type-dimensions type)))))
799 ;; If it's a simple multidimensional array, then just return
800 ;; its data vector directly rather than going through
801 ;; %WITH-ARRAY-DATA-MACRO. SBCL doesn't generally generate
802 ;; code that would use this currently, but we have encouraged
803 ;; users to use WITH-ARRAY-DATA and we may use it ourselves at
804 ;; some point in the future for optimized libraries or
807 `(let* ((data (truly-the (simple-array ,element-type (*))
808 (%array-data-vector array)))
810 (real-end (or end len)))
811 (unless (<= 0 start data-end lend)
812 (sequence-bounding-indices-bad-error array start end))
813 (values data 0 real-end 0))
814 `(let ((data (truly-the (simple-array ,element-type (*))
815 (%array-data-vector array))))
816 (values data 0 (or end (length data)) 0)))
817 `(%with-array-data-macro array start end
818 :check-fill-pointer ,check-fill-pointer
819 :check-bounds ,check-bounds
820 :element-type ,element-type))))
822 ;; It might very well be reasonable to allow general ARRAY here, I
823 ;; just haven't tried to understand the performance issues involved.
824 ;; -- WHN, and also CSR 2002-05-26
825 (deftransform %with-array-data ((array start end)
826 ((or vector simple-array) index (or index null) t)
829 :policy (> speed space))
830 "inline non-SIMPLE-vector-handling logic"
831 (transform-%with-array-data/muble array node nil))
832 (deftransform %with-array-data/fp ((array start end)
833 ((or vector simple-array) index (or index null) t)
836 :policy (> speed space))
837 "inline non-SIMPLE-vector-handling logic"
838 (transform-%with-array-data/muble array node t))
842 ;;; We convert all typed array accessors into AREF and %ASET with type
843 ;;; assertions on the array.
844 (macrolet ((define-bit-frob (reffer setter simplep)
846 (define-source-transform ,reffer (a &rest i)
847 `(aref (the (,',(if simplep 'simple-array 'array)
849 ,(mapcar (constantly '*) i))
851 (define-source-transform ,setter (a &rest i)
852 `(%aset (the (,',(if simplep 'simple-array 'array)
854 ,(cdr (mapcar (constantly '*) i)))
856 (define-bit-frob sbit %sbitset t)
857 (define-bit-frob bit %bitset nil))
858 (macrolet ((define-frob (reffer setter type)
860 (define-source-transform ,reffer (a i)
861 `(aref (the ,',type ,a) ,i))
862 (define-source-transform ,setter (a i v)
863 `(%aset (the ,',type ,a) ,i ,v)))))
864 (define-frob svref %svset simple-vector)
865 (define-frob schar %scharset simple-string)
866 (define-frob char %charset string))
868 (macrolet (;; This is a handy macro for computing the row-major index
869 ;; given a set of indices. We wrap each index with a call
870 ;; to %CHECK-BOUND to ensure that everything works out
871 ;; correctly. We can wrap all the interior arithmetic with
872 ;; TRULY-THE INDEX because we know the resultant
873 ;; row-major index must be an index.
874 (with-row-major-index ((array indices index &optional new-value)
876 `(let (n-indices dims)
877 (dotimes (i (length ,indices))
878 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
879 (push (make-symbol (format nil "DIM-~D" i)) dims))
880 (setf n-indices (nreverse n-indices))
881 (setf dims (nreverse dims))
882 `(lambda (,',array ,@n-indices
883 ,@',(when new-value (list new-value)))
884 (let* (,@(let ((,index -1))
885 (mapcar (lambda (name)
886 `(,name (array-dimension
893 (do* ((dims dims (cdr dims))
894 (indices n-indices (cdr indices))
895 (last-dim nil (car dims))
896 (form `(%check-bound ,',array
908 ((null (cdr dims)) form)))))
911 ;; Just return the index after computing it.
912 (deftransform array-row-major-index ((array &rest indices))
913 (with-row-major-index (array indices index)
916 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
917 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
918 ;; expression for the row major index.
919 (deftransform aref ((array &rest indices))
920 (with-row-major-index (array indices index)
921 (hairy-data-vector-ref array index)))
923 (deftransform %aset ((array &rest stuff))
924 (let ((indices (butlast stuff)))
925 (with-row-major-index (array indices index new-value)
926 (hairy-data-vector-set array index new-value)))))
928 ;; For AREF of vectors we do the bounds checking in the callee. This
929 ;; lets us do a significantly more efficient check for simple-arrays
930 ;; without bloating the code. If we already know the type of the array
931 ;; with sufficient precision, skip directly to DATA-VECTOR-REF.
932 (deftransform aref ((array index) (t t) * :node node)
933 (let* ((type (lvar-type array))
934 (element-ctype (extract-upgraded-element-type array)))
936 ((and (array-type-p type)
937 (null (array-type-complexp type))
938 (not (eql element-ctype *wild-type*))
939 (eql (length (array-type-dimensions type)) 1))
940 (let* ((declared-element-ctype (extract-declared-element-type array))
942 `(data-vector-ref array
943 (%check-bound array (array-dimension array 0) index))))
944 (if (type= declared-element-ctype element-ctype)
946 `(the ,(type-specifier declared-element-ctype) ,bare-form))))
947 ((policy node (zerop insert-array-bounds-checks))
948 `(hairy-data-vector-ref array index))
949 (t `(hairy-data-vector-ref/check-bounds array index)))))
951 (deftransform %aset ((array index new-value) (t t t) * :node node)
952 (if (policy node (zerop insert-array-bounds-checks))
953 `(hairy-data-vector-set array index new-value)
954 `(hairy-data-vector-set/check-bounds array index new-value)))
956 ;;; But if we find out later that there's some useful type information
957 ;;; available, switch back to the normal one to give other transforms
959 (macrolet ((define (name transform-to extra extra-type)
960 (declare (ignore extra-type))
961 `(deftransform ,name ((array index ,@extra))
962 (let ((type (lvar-type array))
963 (element-type (extract-upgraded-element-type array))
964 (declared-type (extract-declared-element-type array)))
965 ;; If an element type has been declared, we want to
966 ;; use that information it for type checking (even
967 ;; if the access can't be optimized due to the array
968 ;; not being simple).
969 (when (and (eql element-type *wild-type*)
970 ;; This type logic corresponds to the special
971 ;; case for strings in HAIRY-DATA-VECTOR-REF
972 ;; (generic/vm-tran.lisp)
973 (not (csubtypep type (specifier-type 'simple-string))))
974 (when (or (not (array-type-p type))
975 ;; If it's a simple array, we might be able
976 ;; to inline the access completely.
977 (not (null (array-type-complexp type))))
978 (give-up-ir1-transform
979 "Upgraded element type of array is not known at compile time.")))
981 ``(truly-the ,declared-type
982 (,',transform-to array
984 (array-dimension array 0)
986 (the ,declared-type ,@',extra)))
987 ``(the ,declared-type
988 (,',transform-to array
990 (array-dimension array 0)
992 (define hairy-data-vector-ref/check-bounds
993 hairy-data-vector-ref nil nil)
994 (define hairy-data-vector-set/check-bounds
995 hairy-data-vector-set (new-value) (*)))
997 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
998 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
999 ;;; array total size.
1000 (deftransform row-major-aref ((array index))
1001 `(hairy-data-vector-ref array
1002 (%check-bound array (array-total-size array) index)))
1003 (deftransform %set-row-major-aref ((array index new-value))
1004 `(hairy-data-vector-set array
1005 (%check-bound array (array-total-size array) index)
1008 ;;;; bit-vector array operation canonicalization
1010 ;;;; We convert all bit-vector operations to have the result array
1011 ;;;; specified. This allows any result allocation to be open-coded,
1012 ;;;; and eliminates the need for any VM-dependent transforms to handle
1015 (macrolet ((def (fun)
1017 (deftransform ,fun ((bit-array-1 bit-array-2
1018 &optional result-bit-array)
1019 (bit-vector bit-vector &optional null) *
1020 :policy (>= speed space))
1021 `(,',fun bit-array-1 bit-array-2
1022 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
1023 ;; If result is T, make it the first arg.
1024 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
1025 (bit-vector bit-vector (eql t)) *)
1026 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
1038 ;;; Similar for BIT-NOT, but there is only one arg...
1039 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
1040 (bit-vector &optional null) *
1041 :policy (>= speed space))
1042 '(bit-not bit-array-1
1043 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
1044 (deftransform bit-not ((bit-array-1 result-bit-array)
1045 (bit-vector (eql t)))
1046 '(bit-not bit-array-1 bit-array-1))
1048 ;;; Pick off some constant cases.
1049 (defoptimizer (array-header-p derive-type) ((array))
1050 (let ((type (lvar-type array)))
1051 (cond ((not (array-type-p type))
1052 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
1055 (let ((dims (array-type-dimensions type)))
1056 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
1058 (specifier-type 'null))
1059 ((and (listp dims) (/= (length dims) 1))
1060 ;; multi-dimensional array, will have a header
1061 (specifier-type '(eql t)))
1062 ((eql (array-type-complexp type) t)
1063 (specifier-type '(eql t)))