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 CONTINUATION, or do
17 ;;; GIVE-UP-IR1-TRANSFORM if the upgraded element type can't be
19 (defun upgraded-element-type-specifier-or-give-up (continuation)
20 (let* ((element-ctype (extract-upgraded-element-type continuation))
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 (continuation-type array)))
31 ;; Note that this IF mightn't be satisfied even if the runtime
32 ;; value is known to be a subtype of some specialized ARRAY, because
33 ;; we can have values declared e.g. (AND SIMPLE-VECTOR UNKNOWN-TYPE),
34 ;; which are represented in the compiler as INTERSECTION-TYPE, not
36 (if (array-type-p type)
37 (array-type-specialized-element-type type)
38 ;; KLUDGE: there is no good answer here, but at least
39 ;; *wild-type* won't cause HAIRY-DATA-VECTOR-{REF,SET} to be
40 ;; erroneously optimized (see generic/vm-tran.lisp) -- CSR,
44 ;;; The ``new-value'' for array setters must fit in the array, and the
45 ;;; return type is going to be the same as the new-value for SETF
47 (defun assert-new-value-type (new-value array)
48 (let ((type (continuation-type array)))
49 (when (array-type-p type)
50 (assert-continuation-type
52 (array-type-specialized-element-type type)
53 (lexenv-policy (node-lexenv (continuation-dest new-value))))))
54 (continuation-type new-value))
56 (defun assert-array-complex (array)
57 (assert-continuation-type
59 (make-array-type :complexp t
60 :element-type *wild-type*)
61 (lexenv-policy (node-lexenv (continuation-dest array)))))
63 ;;; Return true if ARG is NIL, or is a constant-continuation whose
64 ;;; value is NIL, false otherwise.
65 (defun unsupplied-or-nil (arg)
66 (declare (type (or continuation null) arg))
68 (and (constant-continuation-p arg)
69 (not (continuation-value arg)))))
71 ;;;; DERIVE-TYPE optimizers
73 ;;; Array operations that use a specific number of indices implicitly
74 ;;; assert that the array is of that rank.
75 (defun assert-array-rank (array rank)
76 (assert-continuation-type
78 (specifier-type `(array * ,(make-list rank :initial-element '*)))
79 (lexenv-policy (node-lexenv (continuation-dest array)))))
81 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
82 (assert-array-rank array (length indices))
85 (defoptimizer (aref derive-type) ((array &rest indices) node)
86 (assert-array-rank array (length indices))
87 ;; If the node continuation has a single use then assert its type.
88 (let ((cont (node-cont node)))
89 (when (= (length (find-uses cont)) 1)
90 (assert-continuation-type cont (extract-upgraded-element-type array)
91 (lexenv-policy (node-lexenv node)))))
92 (extract-upgraded-element-type array))
94 (defoptimizer (%aset derive-type) ((array &rest stuff))
95 (assert-array-rank array (1- (length stuff)))
96 (assert-new-value-type (car (last stuff)) array))
98 (defoptimizer (hairy-data-vector-ref derive-type) ((array index))
99 (extract-upgraded-element-type array))
100 (defoptimizer (data-vector-ref derive-type) ((array index))
101 (extract-upgraded-element-type array))
103 (defoptimizer (data-vector-set derive-type) ((array index new-value))
104 (assert-new-value-type new-value array))
105 (defoptimizer (hairy-data-vector-set derive-type) ((array index new-value))
106 (assert-new-value-type new-value array))
108 ;;; Figure out the type of the data vector if we know the argument
110 (defoptimizer (%with-array-data derive-type) ((array start end))
111 (let ((atype (continuation-type array)))
112 (when (array-type-p atype)
113 (values-specifier-type
114 `(values (simple-array ,(type-specifier
115 (array-type-specialized-element-type atype))
117 index index index)))))
119 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
120 (assert-array-rank array (length indices))
123 (defoptimizer (row-major-aref derive-type) ((array index))
124 (extract-upgraded-element-type array))
126 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
127 (assert-new-value-type new-value array))
129 (defoptimizer (make-array derive-type)
130 ((dims &key initial-element element-type initial-contents
131 adjustable fill-pointer displaced-index-offset displaced-to))
132 (let ((simple (and (unsupplied-or-nil adjustable)
133 (unsupplied-or-nil displaced-to)
134 (unsupplied-or-nil fill-pointer))))
135 (or (careful-specifier-type
136 `(,(if simple 'simple-array 'array)
137 ,(cond ((not element-type) t)
138 ((constant-continuation-p element-type)
139 (continuation-value element-type))
144 ((constant-continuation-p dims)
145 (let ((val (continuation-value dims)))
146 (if (listp val) val (list val))))
147 ((csubtypep (continuation-type dims)
148 (specifier-type 'integer))
152 (specifier-type 'array))))
154 ;;; Complex array operations should assert that their array argument
155 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
156 (defoptimizer (fill-pointer derive-type) ((vector))
157 (assert-array-complex vector))
158 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
159 (declare (ignorable index))
160 (assert-array-complex vector))
162 (defoptimizer (vector-push derive-type) ((object vector))
163 (declare (ignorable object))
164 (assert-array-complex vector))
165 (defoptimizer (vector-push-extend derive-type)
166 ((object vector &optional index))
167 (declare (ignorable object index))
168 (assert-array-complex vector))
169 (defoptimizer (vector-pop derive-type) ((vector))
170 (assert-array-complex vector))
174 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
176 (define-source-transform vector (&rest elements)
177 (let ((len (length elements))
179 (once-only ((n-vec `(make-array ,len)))
181 ,@(mapcar (lambda (el)
182 (once-only ((n-val el))
183 `(locally (declare (optimize (safety 0)))
184 (setf (svref ,n-vec ,(incf n))
189 ;;; Just convert it into a MAKE-ARRAY.
190 (deftransform make-string ((length &key
191 (element-type 'base-char)
193 #.*default-init-char-form*)))
194 '(make-array (the index length)
195 :element-type element-type
196 :initial-element initial-element))
198 (defstruct (specialized-array-element-type-properties
200 (:constructor !make-saetp (ctype
201 initial-element-default
207 ;; the element type, e.g. #<BUILT-IN-CLASS BASE-CHAR (sealed)> or
208 ;; #<SB-KERNEL:NUMERIC-TYPE (UNSIGNED-BYTE 4)>
209 (ctype (missing-arg) :type ctype :read-only t)
210 ;; what we get when the low-level vector-creation logic zeroes all
211 ;; the bits (which also serves as the default value of MAKE-ARRAY's
212 ;; :INITIAL-ELEMENT keyword)
213 (initial-element-default (missing-arg) :read-only t)
214 ;; how many bits per element
215 (n-bits (missing-arg) :type index :read-only t)
216 ;; the low-level type code
217 (typecode (missing-arg) :type index :read-only t)
218 ;; the number of extra elements we use at the end of the array for
219 ;; low level hackery (e.g., one element for arrays of BASE-CHAR,
220 ;; which is used for a fixed #\NULL so that when we call out to C
221 ;; we don't need to cons a new copy)
222 (n-pad-elements (missing-arg) :type index :read-only t))
224 (defparameter *specialized-array-element-type-properties*
227 (destructuring-bind (type-spec &rest rest) args
228 (let ((ctype (specifier-type type-spec)))
229 (apply #'!make-saetp ctype rest))))
230 `(;; Erm. Yeah. There aren't a lot of things that make sense
231 ;; for an initial element for (ARRAY NIL). -- CSR, 2002-03-07
232 (nil '#:mu 0 ,sb!vm:simple-array-nil-widetag)
233 (base-char ,(code-char 0) 8 ,sb!vm:simple-string-widetag
234 ;; (SIMPLE-STRINGs are stored with an extra trailing
235 ;; #\NULL for convenience in calling out to C.)
237 (single-float 0.0f0 32 ,sb!vm:simple-array-single-float-widetag)
238 (double-float 0.0d0 64 ,sb!vm:simple-array-double-float-widetag)
239 #!+long-float (long-float 0.0L0 #!+x86 96 #!+sparc 128
240 ,sb!vm:simple-array-long-float-widetag)
241 (bit 0 1 ,sb!vm:simple-bit-vector-widetag)
242 ;; KLUDGE: The fact that these UNSIGNED-BYTE entries come
243 ;; before their SIGNED-BYTE partners is significant in the
244 ;; implementation of the compiler; some of the cross-compiler
245 ;; code (see e.g. COERCE-TO-SMALLEST-ELTYPE in
246 ;; src/compiler/debug-dump.lisp) attempts to create an array
247 ;; specialized on (UNSIGNED-BYTE FOO), where FOO could be 7;
248 ;; (UNSIGNED-BYTE 7) is SUBTYPEP (SIGNED-BYTE 8), so if we're
249 ;; not careful we could get the wrong specialized array when
250 ;; we try to FIND-IF, below. -- CSR, 2002-07-08
251 ((unsigned-byte 2) 0 2 ,sb!vm:simple-array-unsigned-byte-2-widetag)
252 ((unsigned-byte 4) 0 4 ,sb!vm:simple-array-unsigned-byte-4-widetag)
253 ((unsigned-byte 8) 0 8 ,sb!vm:simple-array-unsigned-byte-8-widetag)
254 ((unsigned-byte 16) 0 16 ,sb!vm:simple-array-unsigned-byte-16-widetag)
255 ((unsigned-byte 32) 0 32 ,sb!vm:simple-array-unsigned-byte-32-widetag)
256 ((signed-byte 8) 0 8 ,sb!vm:simple-array-signed-byte-8-widetag)
257 ((signed-byte 16) 0 16 ,sb!vm:simple-array-signed-byte-16-widetag)
258 ((signed-byte 30) 0 32 ,sb!vm:simple-array-signed-byte-30-widetag)
259 ((signed-byte 32) 0 32 ,sb!vm:simple-array-signed-byte-32-widetag)
260 ((complex single-float) #C(0.0f0 0.0f0) 64
261 ,sb!vm:simple-array-complex-single-float-widetag)
262 ((complex double-float) #C(0.0d0 0.0d0) 128
263 ,sb!vm:simple-array-complex-double-float-widetag)
264 #!+long-float ((complex long-float) #C(0.0L0 0.0L0)
265 #!+x86 192 #!+sparc 256
266 ,sb!vm:simple-array-complex-long-float-widetag)
267 (t 0 32 ,sb!vm:simple-vector-widetag))))
269 (deftransform make-array ((dims &key initial-element element-type
270 adjustable fill-pointer)
272 (when (null initial-element)
273 (give-up-ir1-transform))
274 (let* ((eltype (cond ((not element-type) t)
275 ((not (constant-continuation-p element-type))
276 (give-up-ir1-transform
277 "ELEMENT-TYPE is not constant."))
279 (continuation-value element-type))))
280 (eltype-type (ir1-transform-specifier-type eltype))
281 (saetp (find-if (lambda (saetp)
282 (csubtypep eltype-type (saetp-ctype saetp)))
283 *specialized-array-element-type-properties*))
284 (creation-form `(make-array dims
285 :element-type ',(type-specifier (saetp-ctype saetp))
287 '(:fill-pointer fill-pointer))
289 '(:adjustable adjustable)))))
292 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
294 (cond ((and (constant-continuation-p initial-element)
295 (eql (continuation-value initial-element)
296 (saetp-initial-element-default saetp)))
299 ;; error checking for target, disabled on the host because
300 ;; (CTYPE-OF #\Null) is not possible.
302 (when (constant-continuation-p initial-element)
303 (let ((value (continuation-value initial-element)))
305 ((not (csubtypep (ctype-of value)
306 (saetp-ctype saetp)))
307 ;; this case will cause an error at runtime, so we'd
308 ;; better WARN about it now.
309 (compiler-warn "~@<~S is not a ~S (which is the ~
310 UPGRADED-ARRAY-ELEMENT-TYPE of ~S).~@:>"
312 (type-specifier (saetp-ctype saetp))
314 ((not (csubtypep (ctype-of value) eltype-type))
315 ;; this case will not cause an error at runtime, but
316 ;; it's still worth STYLE-WARNing about.
317 (compiler-style-warn "~S is not a ~S."
319 `(let ((array ,creation-form))
320 (multiple-value-bind (vector)
321 (%data-vector-and-index array 0)
322 (fill vector initial-element))
325 ;;; The integer type restriction on the length ensures that it will be
326 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
327 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
328 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
329 ;;; kind of initialization efficiently.
330 (deftransform make-array ((length &key element-type)
332 (let* ((eltype (cond ((not element-type) t)
333 ((not (constant-continuation-p element-type))
334 (give-up-ir1-transform
335 "ELEMENT-TYPE is not constant."))
337 (continuation-value element-type))))
338 (len (if (constant-continuation-p length)
339 (continuation-value length)
341 (result-type-spec `(simple-array ,eltype (,len)))
342 (eltype-type (ir1-transform-specifier-type eltype))
343 (saetp (find-if (lambda (saetp)
344 (csubtypep eltype-type (saetp-ctype saetp)))
345 *specialized-array-element-type-properties*)))
347 (give-up-ir1-transform
348 "cannot open-code creation of ~S" result-type-spec))
350 (unless (csubtypep (ctype-of (saetp-initial-element-default saetp))
352 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
353 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
354 ;; INITIAL-ELEMENT is not supplied, the consequences of later
355 ;; reading an uninitialized element of new-array are undefined,"
356 ;; so this could be legal code as long as the user plans to
357 ;; write before he reads, and if he doesn't we're free to do
358 ;; anything we like. But in case the user doesn't know to write
359 ;; elements before he reads elements (or to read manuals before
360 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
361 ;; didn't realize this.
362 (compiler-style-warn "The default initial element ~S is not a ~S."
363 (saetp-initial-element-default saetp)
365 (let* ((n-bits-per-element (saetp-n-bits saetp))
366 (typecode (saetp-typecode saetp))
367 (n-pad-elements (saetp-n-pad-elements saetp))
368 (padded-length-form (if (zerop n-pad-elements)
370 `(+ length ,n-pad-elements)))
373 ((= n-bits-per-element 0) 0)
374 ((>= n-bits-per-element sb!vm:n-word-bits)
375 `(* ,padded-length-form
376 (the fixnum ; i.e., not RATIO
377 ,(/ n-bits-per-element sb!vm:n-word-bits))))
379 (let ((n-elements-per-word (/ sb!vm:n-word-bits
380 n-bits-per-element)))
381 (declare (type index n-elements-per-word)) ; i.e., not RATIO
382 `(ceiling ,padded-length-form ,n-elements-per-word))))))
384 `(truly-the ,result-type-spec
385 (allocate-vector ,typecode length ,n-words-form))
386 '((declare (type index length)))))))
388 ;;; The list type restriction does not ensure that the result will be a
389 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
390 ;;; and displaced-to keywords ensures that it will be simple.
392 ;;; FIXME: should we generalize this transform to non-simple (though
393 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
394 ;;; deal with those? Maybe when the DEFTRANSFORM
395 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
397 (deftransform make-array ((dims &key element-type)
399 (unless (or (null element-type) (constant-continuation-p element-type))
400 (give-up-ir1-transform
401 "The element-type is not constant; cannot open code array creation."))
402 (unless (constant-continuation-p dims)
403 (give-up-ir1-transform
404 "The dimension list is not constant; cannot open code array creation."))
405 (let ((dims (continuation-value dims)))
406 (unless (every #'integerp dims)
407 (give-up-ir1-transform
408 "The dimension list contains something other than an integer: ~S"
410 (if (= (length dims) 1)
411 `(make-array ',(car dims)
413 '(:element-type element-type)))
414 (let* ((total-size (reduce #'* dims))
417 ,(cond ((null element-type) t)
418 ((constant-continuation-p element-type)
419 (continuation-value element-type))
421 ,(make-list rank :initial-element '*))))
422 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank)))
423 (setf (%array-fill-pointer header) ,total-size)
424 (setf (%array-fill-pointer-p header) nil)
425 (setf (%array-available-elements header) ,total-size)
426 (setf (%array-data-vector header)
427 (make-array ,total-size
429 '(:element-type element-type))))
430 (setf (%array-displaced-p header) nil)
432 (mapcar (lambda (dim)
433 `(setf (%array-dimension header ,(incf axis))
436 (truly-the ,spec header))))))
438 ;;;; miscellaneous properties of arrays
440 ;;; Transforms for various array properties. If the property is know
441 ;;; at compile time because of a type spec, use that constant value.
443 ;;; If we can tell the rank from the type info, use it instead.
444 (deftransform array-rank ((array))
445 (let ((array-type (continuation-type array)))
446 (unless (array-type-p array-type)
447 (give-up-ir1-transform))
448 (let ((dims (array-type-dimensions array-type)))
449 (if (not (listp dims))
450 (give-up-ir1-transform
451 "The array rank is not known at compile time: ~S"
455 ;;; If we know the dimensions at compile time, just use it. Otherwise,
456 ;;; if we can tell that the axis is in bounds, convert to
457 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
458 ;;; (if it's simple and a vector).
459 (deftransform array-dimension ((array axis)
461 (unless (constant-continuation-p axis)
462 (give-up-ir1-transform "The axis is not constant."))
463 (let ((array-type (continuation-type array))
464 (axis (continuation-value axis)))
465 (unless (array-type-p array-type)
466 (give-up-ir1-transform))
467 (let ((dims (array-type-dimensions array-type)))
469 (give-up-ir1-transform
470 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
471 (unless (> (length dims) axis)
472 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
475 (let ((dim (nth axis dims)))
476 (cond ((integerp dim)
479 (ecase (array-type-complexp array-type)
481 '(%array-dimension array 0))
485 (give-up-ir1-transform
486 "can't tell whether array is simple"))))
488 '(%array-dimension array axis)))))))
490 ;;; If the length has been declared and it's simple, just return it.
491 (deftransform length ((vector)
492 ((simple-array * (*))))
493 (let ((type (continuation-type vector)))
494 (unless (array-type-p type)
495 (give-up-ir1-transform))
496 (let ((dims (array-type-dimensions type)))
497 (unless (and (listp dims) (integerp (car dims)))
498 (give-up-ir1-transform
499 "Vector length is unknown, must call LENGTH at runtime."))
502 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
503 ;;; simple, it will extract the length slot from the vector. It it's
504 ;;; complex, it will extract the fill pointer slot from the array
506 (deftransform length ((vector) (vector))
507 '(vector-length vector))
509 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
510 ;;; compile-time constant.
511 (deftransform vector-length ((vector) ((simple-array * (*))))
512 (let ((vtype (continuation-type vector)))
513 (if (array-type-p vtype)
514 (let ((dim (first (array-type-dimensions vtype))))
515 (when (eq dim '*) (give-up-ir1-transform))
517 (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 (continuation-type array)))
527 (unless (array-type-p array-type)
528 (give-up-ir1-transform))
529 (let ((dims (array-type-dimensions array-type)))
531 (give-up-ir1-transform "can't tell the rank at compile time"))
533 (do ((form 1 `(truly-the index
534 (* (array-dimension array ,i) ,form)))
536 ((= i (length dims)) form))
537 (reduce #'* dims)))))
539 ;;; Only complex vectors have fill pointers.
540 (deftransform array-has-fill-pointer-p ((array))
541 (let ((array-type (continuation-type array)))
542 (unless (array-type-p array-type)
543 (give-up-ir1-transform))
544 (let ((dims (array-type-dimensions array-type)))
545 (if (and (listp dims) (not (= (length dims) 1)))
547 (ecase (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))
561 (unless (constant-continuation-p dimension)
562 (give-up-ir1-transform))
563 (let ((dim (continuation-value dimension)))
564 `(the (integer 0 ,dim) index)))
565 (deftransform %check-bound ((array dimension index) * *
566 :policy (and (> speed safety) (= safety 0)))
571 ;;; This checks to see whether the array is simple and the start and
572 ;;; end are in bounds. If so, it proceeds with those values.
573 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
574 ;;; may be further optimized.
576 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
577 ;;; START-VAR and END-VAR to the start and end of the designated
578 ;;; portion of the data vector. SVALUE and EVALUE are any start and
579 ;;; end specified to the original operation, and are factored into the
580 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
581 ;;; offset of all displacements encountered, and does not include
584 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
585 ;;; forced to be inline, overriding the ordinary judgment of the
586 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
587 ;;; fairly picky about their arguments, figuring that if you haven't
588 ;;; bothered to get all your ducks in a row, you probably don't care
589 ;;; that much about speed anyway! But in some cases it makes sense to
590 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
591 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
592 ;;; sense to use FORCE-INLINE option in that case.
593 (def!macro with-array-data (((data-var array &key offset-var)
594 (start-var &optional (svalue 0))
595 (end-var &optional (evalue nil))
598 (once-only ((n-array array)
599 (n-svalue `(the index ,svalue))
600 (n-evalue `(the (or index null) ,evalue)))
601 `(multiple-value-bind (,data-var
604 ,@(when offset-var `(,offset-var)))
605 (if (not (array-header-p ,n-array))
606 (let ((,n-array ,n-array))
607 (declare (type (simple-array * (*)) ,n-array))
608 ,(once-only ((n-len `(length ,n-array))
609 (n-end `(or ,n-evalue ,n-len)))
610 `(if (<= ,n-svalue ,n-end ,n-len)
612 (values ,n-array ,n-svalue ,n-end 0)
613 (failed-%with-array-data ,n-array
616 (,(if force-inline '%with-array-data-macro '%with-array-data)
617 ,n-array ,n-svalue ,n-evalue))
620 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
621 ;;; DEFTRANSFORMs and DEFUNs.
622 (def!macro %with-array-data-macro (array
629 (let ((size (gensym "SIZE-"))
630 (defaulted-end (gensym "DEFAULTED-END-"))
631 (data (gensym "DATA-"))
632 (cumulative-offset (gensym "CUMULATIVE-OFFSET-")))
633 `(let* ((,size (array-total-size ,array))
636 (unless (or ,unsafe? (<= ,end ,size))
638 `(error 'bounding-indices-bad-error
639 :datum (cons ,start ,end)
640 :expected-type `(cons (integer 0 ,',size)
641 (integer ,',start ,',size))
643 `(failed-%with-array-data ,array ,start ,end)))
646 (unless (or ,unsafe? (<= ,start ,defaulted-end))
648 `(error 'bounding-indices-bad-error
649 :datum (cons ,start ,end)
650 :expected-type `(cons (integer 0 ,',size)
651 (integer ,',start ,',size))
653 `(failed-%with-array-data ,array ,start ,end)))
654 (do ((,data ,array (%array-data-vector ,data))
655 (,cumulative-offset 0
656 (+ ,cumulative-offset
657 (%array-displacement ,data))))
658 ((not (array-header-p ,data))
659 (values (the (simple-array ,element-type 1) ,data)
660 (the index (+ ,cumulative-offset ,start))
661 (the index (+ ,cumulative-offset ,defaulted-end))
662 (the index ,cumulative-offset)))
663 (declare (type index ,cumulative-offset))))))
665 (deftransform %with-array-data ((array start end)
666 ;; It might very well be reasonable to
667 ;; allow general ARRAY here, I just
668 ;; haven't tried to understand the
669 ;; performance issues involved. --
670 ;; WHN, and also CSR 2002-05-26
671 ((or vector simple-array) index (or index null))
675 :policy (> speed space))
676 "inline non-SIMPLE-vector-handling logic"
677 (let ((element-type (upgraded-element-type-specifier-or-give-up array)))
678 `(%with-array-data-macro array start end
679 :unsafe? ,(policy node (= safety 0))
680 :element-type ,element-type)))
684 ;;; We convert all typed array accessors into AREF and %ASET with type
685 ;;; assertions on the array.
686 (macrolet ((define-frob (reffer setter type)
688 (define-source-transform ,reffer (a &rest i)
689 `(aref (the ,',type ,a) ,@i))
690 (define-source-transform ,setter (a &rest i)
691 `(%aset (the ,',type ,a) ,@i)))))
692 (define-frob svref %svset simple-vector)
693 (define-frob schar %scharset simple-string)
694 (define-frob char %charset string)
695 (define-frob sbit %sbitset (simple-array bit))
696 (define-frob bit %bitset (array bit)))
698 (macrolet (;; This is a handy macro for computing the row-major index
699 ;; given a set of indices. We wrap each index with a call
700 ;; to %CHECK-BOUND to ensure that everything works out
701 ;; correctly. We can wrap all the interior arithmetic with
702 ;; TRULY-THE INDEX because we know the the resultant
703 ;; row-major index must be an index.
704 (with-row-major-index ((array indices index &optional new-value)
706 `(let (n-indices dims)
707 (dotimes (i (length ,indices))
708 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
709 (push (make-symbol (format nil "DIM-~D" i)) dims))
710 (setf n-indices (nreverse n-indices))
711 (setf dims (nreverse dims))
712 `(lambda (,',array ,@n-indices
713 ,@',(when new-value (list new-value)))
714 (let* (,@(let ((,index -1))
715 (mapcar (lambda (name)
716 `(,name (array-dimension
723 (do* ((dims dims (cdr dims))
724 (indices n-indices (cdr indices))
725 (last-dim nil (car dims))
726 (form `(%check-bound ,',array
738 ((null (cdr dims)) form)))))
741 ;; Just return the index after computing it.
742 (deftransform array-row-major-index ((array &rest indices))
743 (with-row-major-index (array indices index)
746 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
747 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
748 ;; expression for the row major index.
749 (deftransform aref ((array &rest indices))
750 (with-row-major-index (array indices index)
751 (hairy-data-vector-ref array index)))
752 (deftransform %aset ((array &rest stuff))
753 (let ((indices (butlast stuff)))
754 (with-row-major-index (array indices index new-value)
755 (hairy-data-vector-set array index new-value)))))
757 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
758 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
759 ;;; array total size.
760 (deftransform row-major-aref ((array index))
761 `(hairy-data-vector-ref array
762 (%check-bound array (array-total-size array) index)))
763 (deftransform %set-row-major-aref ((array index new-value))
764 `(hairy-data-vector-set array
765 (%check-bound array (array-total-size array) index)
768 ;;;; bit-vector array operation canonicalization
770 ;;;; We convert all bit-vector operations to have the result array
771 ;;;; specified. This allows any result allocation to be open-coded,
772 ;;;; and eliminates the need for any VM-dependent transforms to handle
775 (macrolet ((def (fun)
777 (deftransform ,fun ((bit-array-1 bit-array-2
778 &optional result-bit-array)
779 (bit-vector bit-vector &optional null) *
780 :policy (>= speed space))
781 `(,',fun bit-array-1 bit-array-2
782 (make-array (length bit-array-1) :element-type 'bit)))
783 ;; If result is T, make it the first arg.
784 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
785 (bit-vector bit-vector (member t)) *)
786 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
798 ;;; Similar for BIT-NOT, but there is only one arg...
799 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
800 (bit-vector &optional null) *
801 :policy (>= speed space))
802 '(bit-not bit-array-1
803 (make-array (length bit-array-1) :element-type 'bit)))
804 (deftransform bit-not ((bit-array-1 result-bit-array)
805 (bit-vector (constant-arg t)))
806 '(bit-not bit-array-1 bit-array-1))
807 ;;; FIXME: What does (CONSTANT-ARG T) mean? Is it the same thing
808 ;;; as (CONSTANT-ARG (MEMBER T)), or does it mean any constant
811 ;;; Pick off some constant cases.
812 (deftransform array-header-p ((array) (array))
813 (let ((type (continuation-type array)))
814 (unless (array-type-p type)
815 (give-up-ir1-transform))
816 (let ((dims (array-type-dimensions type)))
817 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
820 ((and (listp dims) (> (length dims) 1))
821 ;; multi-dimensional array, will have a header
824 (give-up-ir1-transform))))))