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 (defun derive-%with-array-data
/mumble-type
(array)
140 (let ((atype (lvar-type array
)))
141 (when (array-type-p atype
)
143 `(simple-array ,(type-specifier
144 (array-type-specialized-element-type atype
))
146 (defoptimizer (%with-array-data derive-type
) ((array start end
))
147 (derive-%with-array-data
/mumble-type array
))
148 (defoptimizer (%with-array-data
/fp derive-type
) ((array start end
))
149 (derive-%with-array-data
/mumble-type array
))
151 (defoptimizer (array-row-major-index derive-type
) ((array &rest indices
))
152 (assert-array-rank array
(length indices
))
155 (defoptimizer (row-major-aref derive-type
) ((array index
))
156 (extract-upgraded-element-type array
))
158 (defoptimizer (%set-row-major-aref derive-type
) ((array index new-value
))
159 (assert-new-value-type new-value array
))
161 (defoptimizer (make-array derive-type
)
162 ((dims &key initial-element element-type initial-contents
163 adjustable fill-pointer displaced-index-offset displaced-to
))
164 (let ((simple (and (unsupplied-or-nil adjustable
)
165 (unsupplied-or-nil displaced-to
)
166 (unsupplied-or-nil fill-pointer
))))
167 (or (careful-specifier-type
168 `(,(if simple
'simple-array
'array
)
169 ,(cond ((not element-type
) t
)
170 ((constant-lvar-p element-type
)
171 (let ((ctype (careful-specifier-type
172 (lvar-value element-type
))))
174 ((or (null ctype
) (unknown-type-p ctype
)) '*)
175 (t (sb!xc
:upgraded-array-element-type
176 (lvar-value element-type
))))))
179 ,(cond ((constant-lvar-p dims
)
180 (let* ((val (lvar-value dims
))
181 (cdims (if (listp val
) val
(list val
))))
185 ((csubtypep (lvar-type dims
)
186 (specifier-type 'integer
))
190 (specifier-type 'array
))))
192 ;;; Complex array operations should assert that their array argument
193 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
194 (defoptimizer (fill-pointer derive-type
) ((vector))
195 (assert-array-complex vector
))
196 (defoptimizer (%set-fill-pointer derive-type
) ((vector index
))
197 (declare (ignorable index
))
198 (assert-array-complex vector
))
200 (defoptimizer (vector-push derive-type
) ((object vector
))
201 (declare (ignorable object
))
202 (assert-array-complex vector
))
203 (defoptimizer (vector-push-extend derive-type
)
204 ((object vector
&optional index
))
205 (declare (ignorable object index
))
206 (assert-array-complex vector
))
207 (defoptimizer (vector-pop derive-type
) ((vector))
208 (assert-array-complex vector
))
212 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
214 (define-source-transform vector
(&rest elements
)
215 (let ((len (length elements
))
217 (once-only ((n-vec `(make-array ,len
)))
219 ,@(mapcar (lambda (el)
220 (once-only ((n-val el
))
221 `(locally (declare (optimize (safety 0)))
222 (setf (svref ,n-vec
,(incf n
)) ,n-val
))))
226 ;;; Just convert it into a MAKE-ARRAY.
227 (deftransform make-string
((length &key
228 (element-type 'character
)
230 #.
*default-init-char-form
*)))
231 `(the simple-string
(make-array (the index length
)
232 :element-type element-type
233 ,@(when initial-element
234 '(:initial-element initial-element
)))))
236 (deftransform make-array
((dims &key initial-element element-type
237 adjustable fill-pointer
)
239 (when (null initial-element
)
240 (give-up-ir1-transform))
241 (let* ((eltype (cond ((not element-type
) t
)
242 ((not (constant-lvar-p element-type
))
243 (give-up-ir1-transform
244 "ELEMENT-TYPE is not constant."))
246 (lvar-value element-type
))))
247 (eltype-type (ir1-transform-specifier-type eltype
))
248 (saetp (find-if (lambda (saetp)
249 (csubtypep eltype-type
(sb!vm
:saetp-ctype saetp
)))
250 sb
!vm
:*specialized-array-element-type-properties
*))
251 (creation-form `(make-array dims
252 :element-type
',(type-specifier (sb!vm
:saetp-ctype saetp
))
254 '(:fill-pointer fill-pointer
))
256 '(:adjustable adjustable
)))))
259 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype
))
261 (cond ((and (constant-lvar-p initial-element
)
262 (eql (lvar-value initial-element
)
263 (sb!vm
:saetp-initial-element-default saetp
)))
266 ;; error checking for target, disabled on the host because
267 ;; (CTYPE-OF #\Null) is not possible.
269 (when (constant-lvar-p initial-element
)
270 (let ((value (lvar-value initial-element
)))
272 ((not (ctypep value
(sb!vm
:saetp-ctype saetp
)))
273 ;; this case will cause an error at runtime, so we'd
274 ;; better WARN about it now.
275 (warn 'array-initial-element-mismatch
276 :format-control
"~@<~S is not a ~S (which is the ~
281 (type-specifier (sb!vm
:saetp-ctype saetp
))
282 'upgraded-array-element-type
284 ((not (ctypep value eltype-type
))
285 ;; this case will not cause an error at runtime, but
286 ;; it's still worth STYLE-WARNing about.
287 (compiler-style-warn "~S is not a ~S."
289 `(let ((array ,creation-form
))
290 (multiple-value-bind (vector)
291 (%data-vector-and-index array
0)
292 (fill vector initial-element
))
295 ;;; The integer type restriction on the length ensures that it will be
296 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
297 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
298 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
299 ;;; kind of initialization efficiently.
300 (deftransform make-array
((length &key element-type
)
302 (let* ((eltype (cond ((not element-type
) t
)
303 ((not (constant-lvar-p element-type
))
304 (give-up-ir1-transform
305 "ELEMENT-TYPE is not constant."))
307 (lvar-value element-type
))))
308 (len (if (constant-lvar-p length
)
311 (eltype-type (ir1-transform-specifier-type eltype
))
314 ,(if (unknown-type-p eltype-type
)
315 (give-up-ir1-transform
316 "ELEMENT-TYPE is an unknown type: ~S" eltype
)
317 (sb!xc
:upgraded-array-element-type eltype
))
319 (saetp (find-if (lambda (saetp)
320 (csubtypep eltype-type
(sb!vm
:saetp-ctype saetp
)))
321 sb
!vm
:*specialized-array-element-type-properties
*)))
323 (give-up-ir1-transform
324 "cannot open-code creation of ~S" result-type-spec
))
326 (unless (ctypep (sb!vm
:saetp-initial-element-default saetp
) eltype-type
)
327 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
328 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
329 ;; INITIAL-ELEMENT is not supplied, the consequences of later
330 ;; reading an uninitialized element of new-array are undefined,"
331 ;; so this could be legal code as long as the user plans to
332 ;; write before he reads, and if he doesn't we're free to do
333 ;; anything we like. But in case the user doesn't know to write
334 ;; elements before he reads elements (or to read manuals before
335 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
336 ;; didn't realize this.
337 (compiler-style-warn "The default initial element ~S is not a ~S."
338 (sb!vm
:saetp-initial-element-default saetp
)
340 (let* ((n-bits-per-element (sb!vm
:saetp-n-bits saetp
))
341 (typecode (sb!vm
:saetp-typecode saetp
))
342 (n-pad-elements (sb!vm
:saetp-n-pad-elements saetp
))
343 (padded-length-form (if (zerop n-pad-elements
)
345 `(+ length
,n-pad-elements
)))
348 ((= n-bits-per-element
0) 0)
349 ((>= n-bits-per-element sb
!vm
:n-word-bits
)
350 `(* ,padded-length-form
351 (the fixnum
; i.e., not RATIO
352 ,(/ n-bits-per-element sb
!vm
:n-word-bits
))))
354 (let ((n-elements-per-word (/ sb
!vm
:n-word-bits
355 n-bits-per-element
)))
356 (declare (type index n-elements-per-word
)) ; i.e., not RATIO
357 `(ceiling ,padded-length-form
,n-elements-per-word
))))))
359 `(truly-the ,result-type-spec
360 (allocate-vector ,typecode length
,n-words-form
))
361 '((declare (type index length
)))))))
363 ;;; The list type restriction does not ensure that the result will be a
364 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
365 ;;; and displaced-to keywords ensures that it will be simple.
367 ;;; FIXME: should we generalize this transform to non-simple (though
368 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
369 ;;; deal with those? Maybe when the DEFTRANSFORM
370 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
372 (deftransform make-array
((dims &key element-type
)
374 (unless (or (null element-type
) (constant-lvar-p element-type
))
375 (give-up-ir1-transform
376 "The element-type is not constant; cannot open code array creation."))
377 (unless (constant-lvar-p dims
)
378 (give-up-ir1-transform
379 "The dimension list is not constant; cannot open code array creation."))
380 (let ((dims (lvar-value dims
)))
381 (unless (every #'integerp dims
)
382 (give-up-ir1-transform
383 "The dimension list contains something other than an integer: ~S"
385 (if (= (length dims
) 1)
386 `(make-array ',(car dims
)
388 '(:element-type element-type
)))
389 (let* ((total-size (reduce #'* dims
))
392 ,(cond ((null element-type
) t
)
393 ((and (constant-lvar-p element-type
)
394 (ir1-transform-specifier-type
395 (lvar-value element-type
)))
396 (sb!xc
:upgraded-array-element-type
397 (lvar-value element-type
)))
399 ,(make-list rank
:initial-element
'*))))
400 `(let ((header (make-array-header sb
!vm
:simple-array-widetag
,rank
)))
401 (setf (%array-fill-pointer header
) ,total-size
)
402 (setf (%array-fill-pointer-p header
) nil
)
403 (setf (%array-available-elements header
) ,total-size
)
404 (setf (%array-data-vector header
)
405 (make-array ,total-size
407 '(:element-type element-type
))))
408 (setf (%array-displaced-p header
) nil
)
410 (mapcar (lambda (dim)
411 `(setf (%array-dimension header
,(incf axis
))
414 (truly-the ,spec header
))))))
416 ;;;; miscellaneous properties of arrays
418 ;;; Transforms for various array properties. If the property is know
419 ;;; at compile time because of a type spec, use that constant value.
421 ;;; Most of this logic may end up belonging in code/late-type.lisp;
422 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
423 ;;; maybe this is just too sloppy for actual type logic. -- CSR,
425 (defun array-type-dimensions-or-give-up (type)
427 (array-type (array-type-dimensions type
))
429 (let ((types (union-type-types type
)))
430 ;; there are at least two types, right?
431 (aver (> (length types
) 1))
432 (let ((result (array-type-dimensions-or-give-up (car types
))))
433 (dolist (type (cdr types
) result
)
434 (unless (equal (array-type-dimensions-or-give-up type
) result
)
435 (give-up-ir1-transform))))))
436 ;; FIXME: intersection type [e.g. (and (array * (*)) (satisfies foo)) ]
437 (t (give-up-ir1-transform))))
439 (defun conservative-array-type-complexp (type)
441 (array-type (array-type-complexp type
))
443 (let ((types (union-type-types type
)))
444 (aver (> (length types
) 1))
445 (let ((result (conservative-array-type-complexp (car types
))))
446 (dolist (type (cdr types
) result
)
447 (unless (eq (conservative-array-type-complexp type
) result
)
448 (return-from conservative-array-type-complexp
:maybe
))))))
449 ;; FIXME: intersection type
452 ;;; If we can tell the rank from the type info, use it instead.
453 (deftransform array-rank
((array))
454 (let ((array-type (lvar-type array
)))
455 (let ((dims (array-type-dimensions-or-give-up array-type
)))
456 (if (not (listp dims
))
457 (give-up-ir1-transform
458 "The array rank is not known at compile time: ~S"
462 ;;; If we know the dimensions at compile time, just use it. Otherwise,
463 ;;; if we can tell that the axis is in bounds, convert to
464 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
465 ;;; (if it's simple and a vector).
466 (deftransform array-dimension
((array axis
)
468 (unless (constant-lvar-p axis
)
469 (give-up-ir1-transform "The axis is not constant."))
470 (let ((array-type (lvar-type array
))
471 (axis (lvar-value axis
)))
472 (let ((dims (array-type-dimensions-or-give-up array-type
)))
474 (give-up-ir1-transform
475 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
476 (unless (> (length dims
) axis
)
477 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
480 (let ((dim (nth axis dims
)))
481 (cond ((integerp dim
)
484 (ecase (conservative-array-type-complexp array-type
)
486 '(%array-dimension array
0))
490 (give-up-ir1-transform
491 "can't tell whether array is simple"))))
493 '(%array-dimension array axis
)))))))
495 ;;; If the length has been declared and it's simple, just return it.
496 (deftransform length
((vector)
497 ((simple-array * (*))))
498 (let ((type (lvar-type vector
)))
499 (let ((dims (array-type-dimensions-or-give-up type
)))
500 (unless (and (listp dims
) (integerp (car dims
)))
501 (give-up-ir1-transform
502 "Vector length is unknown, must call LENGTH at runtime."))
505 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
506 ;;; simple, it will extract the length slot from the vector. It it's
507 ;;; complex, it will extract the fill pointer slot from the array
509 (deftransform length
((vector) (vector))
510 '(vector-length vector
))
512 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
513 ;;; compile-time constant.
514 (deftransform vector-length
((vector))
515 (let ((vtype (lvar-type vector
)))
516 (let ((dim (first (array-type-dimensions-or-give-up vtype
))))
518 (give-up-ir1-transform))
519 (when (conservative-array-type-complexp vtype
)
520 (give-up-ir1-transform))
523 ;;; Again, if we can tell the results from the type, just use it.
524 ;;; Otherwise, if we know the rank, convert into a computation based
525 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
526 ;;; multiplications because we know that the total size must be an
528 (deftransform array-total-size
((array)
530 (let ((array-type (lvar-type array
)))
531 (let ((dims (array-type-dimensions-or-give-up array-type
)))
533 (give-up-ir1-transform "can't tell the rank at compile time"))
535 (do ((form 1 `(truly-the index
536 (* (array-dimension array
,i
) ,form
)))
538 ((= i
(length dims
)) form
))
539 (reduce #'* dims
)))))
541 ;;; Only complex vectors have fill pointers.
542 (deftransform array-has-fill-pointer-p
((array))
543 (let ((array-type (lvar-type array
)))
544 (let ((dims (array-type-dimensions-or-give-up array-type
)))
545 (if (and (listp dims
) (not (= (length dims
) 1)))
547 (ecase (conservative-array-type-complexp array-type
)
553 (give-up-ir1-transform
554 "The array type is ambiguous; must call ~
555 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
557 ;;; Primitive used to verify indices into arrays. If we can tell at
558 ;;; compile-time or we are generating unsafe code, don't bother with
560 (deftransform %check-bound
((array dimension index
) * * :node node
)
561 (cond ((policy node
(= insert-array-bounds-checks
0))
563 ((not (constant-lvar-p dimension
))
564 (give-up-ir1-transform))
566 (let ((dim (lvar-value dimension
)))
567 ;; FIXME: Can SPEED > SAFETY weaken this check to INTEGER?
568 `(the (integer 0 (,dim
)) index
)))))
572 ;;; This checks to see whether the array is simple and the start and
573 ;;; end are in bounds. If so, it proceeds with those values.
574 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
575 ;;; may be further optimized.
577 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
578 ;;; START-VAR and END-VAR to the start and end of the designated
579 ;;; portion of the data vector. SVALUE and EVALUE are any start and
580 ;;; end specified to the original operation, and are factored into the
581 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
582 ;;; offset of all displacements encountered, and does not include
585 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
586 ;;; forced to be inline, overriding the ordinary judgment of the
587 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
588 ;;; fairly picky about their arguments, figuring that if you haven't
589 ;;; bothered to get all your ducks in a row, you probably don't care
590 ;;; that much about speed anyway! But in some cases it makes sense to
591 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
592 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
593 ;;; sense to use FORCE-INLINE option in that case.
594 (def!macro with-array-data
(((data-var array
&key offset-var
)
595 (start-var &optional
(svalue 0))
596 (end-var &optional
(evalue nil
))
597 &key force-inline check-fill-pointer
)
600 (once-only ((n-array array
)
601 (n-svalue `(the index
,svalue
))
602 (n-evalue `(the (or index null
) ,evalue
)))
603 (let ((check-bounds (policy env
(plusp insert-array-bounds-checks
))))
604 `(multiple-value-bind (,data-var
607 ,@(when offset-var
`(,offset-var
)))
608 (if (not (array-header-p ,n-array
))
609 (let ((,n-array
,n-array
))
610 (declare (type (simple-array * (*)) ,n-array
))
611 ,(once-only ((n-len (if check-fill-pointer
613 `(array-total-size ,n-array
)))
614 (n-end `(or ,n-evalue
,n-len
)))
616 `(if (<= 0 ,n-svalue
,n-end
,n-len
)
617 (values ,n-array
,n-svalue
,n-end
0)
618 ,(if check-fill-pointer
619 `(sequence-bounding-indices-bad-error ,n-array
,n-svalue
,n-evalue
)
620 `(array-bounding-indices-bad-error ,n-array
,n-svalue
,n-evalue
)))
621 `(values ,n-array
,n-svalue
,n-end
0))))
623 `(%with-array-data-macro
,n-array
,n-svalue
,n-evalue
624 :check-bounds
,check-bounds
625 :check-fill-pointer
,check-fill-pointer
)
626 (if check-fill-pointer
627 `(%with-array-data
/fp
,n-array
,n-svalue
,n-evalue
)
628 `(%with-array-data
,n-array
,n-svalue
,n-evalue
))))
631 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
632 ;;; DEFTRANSFORMs and DEFUNs.
633 (def!macro %with-array-data-macro
(array
640 (with-unique-names (size defaulted-end data cumulative-offset
)
641 `(let* ((,size
,(if check-fill-pointer
643 `(array-total-size ,array
)))
644 (,defaulted-end
(or ,end
,size
)))
646 `((unless (<= ,start
,defaulted-end
,size
)
647 ,(if check-fill-pointer
648 `(sequence-bounding-indices-bad-error ,array
,start
,end
)
649 `(array-bounding-indices-bad-error ,array
,start
,end
)))))
650 (do ((,data
,array
(%array-data-vector
,data
))
651 (,cumulative-offset
0
652 (+ ,cumulative-offset
653 (%array-displacement
,data
))))
654 ((not (array-header-p ,data
))
655 (values (the (simple-array ,element-type
1) ,data
)
656 (the index
(+ ,cumulative-offset
,start
))
657 (the index
(+ ,cumulative-offset
,defaulted-end
))
658 (the index
,cumulative-offset
)))
659 (declare (type index
,cumulative-offset
))))))
661 (defun transform-%with-array-data
/muble
(array node check-fill-pointer
)
662 (let ((element-type (upgraded-element-type-specifier-or-give-up array
))
663 (type (lvar-type array
))
664 (check-bounds (policy node
(plusp insert-array-bounds-checks
))))
665 (if (and (array-type-p type
)
666 (not (array-type-complexp type
))
667 (listp (array-type-dimensions type
))
668 (not (null (cdr (array-type-dimensions type
)))))
669 ;; If it's a simple multidimensional array, then just return
670 ;; its data vector directly rather than going through
671 ;; %WITH-ARRAY-DATA-MACRO. SBCL doesn't generally generate
672 ;; code that would use this currently, but we have encouraged
673 ;; users to use WITH-ARRAY-DATA and we may use it ourselves at
674 ;; some point in the future for optimized libraries or
677 `(let* ((data (truly-the (simple-array ,element-type
(*))
678 (%array-data-vector array
)))
680 (real-end (or end len
)))
681 (unless (<= 0 start data-end lend
)
682 (sequence-bounding-indices-bad-error array start end
))
683 (values data
0 real-end
0))
684 `(let ((data (truly-the (simple-array ,element-type
(*))
685 (%array-data-vector array
))))
686 (values data
0 (or end
(length data
)) 0)))
687 `(%with-array-data-macro array start end
688 :check-fill-pointer
,check-fill-pointer
689 :check-bounds
,check-bounds
690 :element-type
,element-type
))))
692 ;; It might very well be reasonable to allow general ARRAY here, I
693 ;; just haven't tried to understand the performance issues involved.
694 ;; -- WHN, and also CSR 2002-05-26
695 (deftransform %with-array-data
((array start end
)
696 ((or vector simple-array
) index
(or index null
) t
)
699 :policy
(> speed space
))
700 "inline non-SIMPLE-vector-handling logic"
701 (transform-%with-array-data
/muble array node nil
))
702 (deftransform %with-array-data
/fp
((array start end
)
703 ((or vector simple-array
) index
(or index null
) t
)
706 :policy
(> speed space
))
707 "inline non-SIMPLE-vector-handling logic"
708 (transform-%with-array-data
/muble array node t
))
712 ;;; We convert all typed array accessors into AREF and %ASET with type
713 ;;; assertions on the array.
714 (macrolet ((define-bit-frob (reffer setter simplep
)
716 (define-source-transform ,reffer
(a &rest i
)
717 `(aref (the (,',(if simplep
'simple-array
'array
)
719 ,(mapcar (constantly '*) i
))
721 (define-source-transform ,setter
(a &rest i
)
722 `(%aset
(the (,',(if simplep
'simple-array
'array
)
724 ,(cdr (mapcar (constantly '*) i
)))
726 (define-bit-frob sbit %sbitset t
)
727 (define-bit-frob bit %bitset nil
))
728 (macrolet ((define-frob (reffer setter type
)
730 (define-source-transform ,reffer
(a i
)
731 `(aref (the ,',type
,a
) ,i
))
732 (define-source-transform ,setter
(a i v
)
733 `(%aset
(the ,',type
,a
) ,i
,v
)))))
734 (define-frob svref %svset simple-vector
)
735 (define-frob schar %scharset simple-string
)
736 (define-frob char %charset string
))
738 (macrolet (;; This is a handy macro for computing the row-major index
739 ;; given a set of indices. We wrap each index with a call
740 ;; to %CHECK-BOUND to ensure that everything works out
741 ;; correctly. We can wrap all the interior arithmetic with
742 ;; TRULY-THE INDEX because we know the resultant
743 ;; row-major index must be an index.
744 (with-row-major-index ((array indices index
&optional new-value
)
746 `(let (n-indices dims
)
747 (dotimes (i (length ,indices
))
748 (push (make-symbol (format nil
"INDEX-~D" i
)) n-indices
)
749 (push (make-symbol (format nil
"DIM-~D" i
)) dims
))
750 (setf n-indices
(nreverse n-indices
))
751 (setf dims
(nreverse dims
))
752 `(lambda (,',array
,@n-indices
753 ,@',(when new-value
(list new-value
)))
754 (let* (,@(let ((,index -
1))
755 (mapcar (lambda (name)
756 `(,name
(array-dimension
763 (do* ((dims dims
(cdr dims
))
764 (indices n-indices
(cdr indices
))
765 (last-dim nil
(car dims
))
766 (form `(%check-bound
,',array
778 ((null (cdr dims
)) form
)))))
781 ;; Just return the index after computing it.
782 (deftransform array-row-major-index
((array &rest indices
))
783 (with-row-major-index (array indices index
)
786 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
787 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
788 ;; expression for the row major index.
789 (deftransform aref
((array &rest indices
))
790 (with-row-major-index (array indices index
)
791 (hairy-data-vector-ref array index
)))
793 (deftransform %aset
((array &rest stuff
))
794 (let ((indices (butlast stuff
)))
795 (with-row-major-index (array indices index new-value
)
796 (hairy-data-vector-set array index new-value
)))))
798 ;; For AREF of vectors we do the bounds checking in the callee. This
799 ;; lets us do a significantly more efficient check for simple-arrays
800 ;; without bloating the code. If we already know the type of the array
801 ;; with sufficient precision, skip directly to DATA-VECTOR-REF.
802 (deftransform aref
((array index
) (t t
) * :node node
)
803 (let* ((type (lvar-type array
))
804 (element-ctype (extract-upgraded-element-type array
)))
806 ((and (array-type-p type
)
807 (null (array-type-complexp type
))
808 (not (eql element-ctype
*wild-type
*))
809 (eql (length (array-type-dimensions type
)) 1))
810 (let* ((declared-element-ctype (extract-declared-element-type array
))
812 `(data-vector-ref array
813 (%check-bound array
(array-dimension array
0) index
))))
814 (if (type= declared-element-ctype element-ctype
)
816 `(the ,(type-specifier declared-element-ctype
) ,bare-form
))))
817 ((policy node
(zerop insert-array-bounds-checks
))
818 `(hairy-data-vector-ref array index
))
819 (t `(hairy-data-vector-ref/check-bounds array index
)))))
821 (deftransform %aset
((array index new-value
) (t t t
) * :node node
)
822 (if (policy node
(zerop insert-array-bounds-checks
))
823 `(hairy-data-vector-set array index new-value
)
824 `(hairy-data-vector-set/check-bounds array index new-value
)))
826 ;;; But if we find out later that there's some useful type information
827 ;;; available, switch back to the normal one to give other transforms
829 (macrolet ((define (name transform-to extra extra-type
)
830 (declare (ignore extra-type
))
831 `(deftransform ,name
((array index
,@extra
))
832 (let ((type (lvar-type array
))
833 (element-type (extract-upgraded-element-type array
)))
834 ;; If an element type has been declared, we want to
835 ;; use that information it for type checking (even
836 ;; if the access can't be optimized due to the array
837 ;; not being simple).
838 (when (and (eql element-type
*wild-type
*)
839 ;; This type logic corresponds to the special
840 ;; case for strings in HAIRY-DATA-VECTOR-REF
841 ;; (generic/vm-tran.lisp)
842 (not (csubtypep type
(specifier-type 'simple-string
))))
843 (when (or (not (array-type-p type
))
844 ;; If it's a simple array, we might be able
845 ;; to inline the access completely.
846 (not (null (array-type-complexp type
))))
847 (give-up-ir1-transform
848 "Upgraded element type of array is not known at compile time."))))
849 `(,',transform-to array
851 (array-dimension array
0)
854 (define hairy-data-vector-ref
/check-bounds
855 hairy-data-vector-ref nil nil
)
856 (define hairy-data-vector-set
/check-bounds
857 hairy-data-vector-set
(new-value) (*)))
859 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
860 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
861 ;;; array total size.
862 (deftransform row-major-aref
((array index
))
863 `(hairy-data-vector-ref array
864 (%check-bound array
(array-total-size array
) index
)))
865 (deftransform %set-row-major-aref
((array index new-value
))
866 `(hairy-data-vector-set array
867 (%check-bound array
(array-total-size array
) index
)
870 ;;;; bit-vector array operation canonicalization
872 ;;;; We convert all bit-vector operations to have the result array
873 ;;;; specified. This allows any result allocation to be open-coded,
874 ;;;; and eliminates the need for any VM-dependent transforms to handle
877 (macrolet ((def (fun)
879 (deftransform ,fun
((bit-array-1 bit-array-2
880 &optional result-bit-array
)
881 (bit-vector bit-vector
&optional null
) *
882 :policy
(>= speed space
))
883 `(,',fun bit-array-1 bit-array-2
884 (make-array (array-dimension bit-array-1
0) :element-type
'bit
)))
885 ;; If result is T, make it the first arg.
886 (deftransform ,fun
((bit-array-1 bit-array-2 result-bit-array
)
887 (bit-vector bit-vector
(eql t
)) *)
888 `(,',fun bit-array-1 bit-array-2 bit-array-1
)))))
900 ;;; Similar for BIT-NOT, but there is only one arg...
901 (deftransform bit-not
((bit-array-1 &optional result-bit-array
)
902 (bit-vector &optional null
) *
903 :policy
(>= speed space
))
904 '(bit-not bit-array-1
905 (make-array (array-dimension bit-array-1
0) :element-type
'bit
)))
906 (deftransform bit-not
((bit-array-1 result-bit-array
)
907 (bit-vector (eql t
)))
908 '(bit-not bit-array-1 bit-array-1
))
910 ;;; Pick off some constant cases.
911 (defoptimizer (array-header-p derive-type
) ((array))
912 (let ((type (lvar-type array
)))
913 (cond ((not (array-type-p type
))
914 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
917 (let ((dims (array-type-dimensions type
)))
918 (cond ((csubtypep type
(specifier-type '(simple-array * (*))))
920 (specifier-type 'null
))
921 ((and (listp dims
) (/= (length dims
) 1))
922 ;; multi-dimensional array, will have a header
923 (specifier-type '(eql t
)))
924 ((eql (array-type-complexp type
) t
)
925 (specifier-type '(eql t
)))