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 (defun extract-declared-element-type (array)
45 (let ((type (continuation-type array)))
46 (if (array-type-p type)
47 (array-type-element-type type)
50 ;;; The ``new-value'' for array setters must fit in the array, and the
51 ;;; return type is going to be the same as the new-value for SETF
53 (defun assert-new-value-type (new-value array)
54 (let ((type (continuation-type array)))
55 (when (array-type-p type)
56 (assert-continuation-type
58 (array-type-specialized-element-type type)
59 (lexenv-policy (node-lexenv (continuation-dest new-value))))))
60 (continuation-type new-value))
62 (defun assert-array-complex (array)
63 (assert-continuation-type
65 (make-array-type :complexp t
66 :element-type *wild-type*)
67 (lexenv-policy (node-lexenv (continuation-dest array))))
70 ;;; Return true if ARG is NIL, or is a constant-continuation whose
71 ;;; value is NIL, false otherwise.
72 (defun unsupplied-or-nil (arg)
73 (declare (type (or continuation null) arg))
75 (and (constant-continuation-p arg)
76 (not (continuation-value arg)))))
78 ;;;; DERIVE-TYPE optimizers
80 ;;; Array operations that use a specific number of indices implicitly
81 ;;; assert that the array is of that rank.
82 (defun assert-array-rank (array rank)
83 (assert-continuation-type
85 (specifier-type `(array * ,(make-list rank :initial-element '*)))
86 (lexenv-policy (node-lexenv (continuation-dest array)))))
88 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
89 (assert-array-rank array (length indices))
92 (defoptimizer (aref derive-type) ((array &rest indices) node)
93 (assert-array-rank array (length indices))
94 ;; If the node continuation has a single use then assert its type.
95 (let ((cont (node-cont node)))
96 (when (= (length (find-uses cont)) 1)
97 (assert-continuation-type cont (extract-upgraded-element-type array)
98 (lexenv-policy (node-lexenv node)))))
99 (extract-upgraded-element-type array))
101 (defoptimizer (%aset derive-type) ((array &rest stuff))
102 (assert-array-rank array (1- (length stuff)))
103 (assert-new-value-type (car (last stuff)) array))
105 (defoptimizer (hairy-data-vector-ref derive-type) ((array index))
106 (extract-upgraded-element-type array))
107 (defoptimizer (data-vector-ref derive-type) ((array index))
108 (extract-upgraded-element-type array))
110 (defoptimizer (data-vector-set derive-type) ((array index new-value))
111 (assert-new-value-type new-value array))
112 (defoptimizer (hairy-data-vector-set derive-type) ((array index new-value))
113 (assert-new-value-type new-value array))
115 ;;; Figure out the type of the data vector if we know the argument
117 (defoptimizer (%with-array-data derive-type) ((array start end))
118 (let ((atype (continuation-type array)))
119 (when (array-type-p atype)
121 `(simple-array ,(type-specifier
122 (array-type-specialized-element-type atype))
125 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
126 (assert-array-rank array (length indices))
129 (defoptimizer (row-major-aref derive-type) ((array index))
130 (extract-upgraded-element-type array))
132 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
133 (assert-new-value-type new-value array))
135 (defoptimizer (make-array derive-type)
136 ((dims &key initial-element element-type initial-contents
137 adjustable fill-pointer displaced-index-offset displaced-to))
138 (let ((simple (and (unsupplied-or-nil adjustable)
139 (unsupplied-or-nil displaced-to)
140 (unsupplied-or-nil fill-pointer))))
141 (or (careful-specifier-type
142 `(,(if simple 'simple-array 'array)
143 ,(cond ((not element-type) t)
144 ((constant-continuation-p element-type)
145 (let ((ctype (careful-specifier-type
146 (continuation-value element-type))))
148 ((or (null ctype) (unknown-type-p ctype)) '*)
149 (t (sb!xc:upgraded-array-element-type
150 (continuation-value element-type))))))
153 ,(cond ((constant-continuation-p dims)
154 (let* ((val (continuation-value dims))
155 (cdims (if (listp val) val (list val))))
159 ((csubtypep (continuation-type dims)
160 (specifier-type 'integer))
164 (specifier-type 'array))))
166 ;;; Complex array operations should assert that their array argument
167 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
168 (defoptimizer (fill-pointer derive-type) ((vector))
169 (assert-array-complex vector))
170 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
171 (declare (ignorable index))
172 (assert-array-complex vector))
174 (defoptimizer (vector-push derive-type) ((object vector))
175 (declare (ignorable object))
176 (assert-array-complex vector))
177 (defoptimizer (vector-push-extend derive-type)
178 ((object vector &optional index))
179 (declare (ignorable object index))
180 (assert-array-complex vector))
181 (defoptimizer (vector-pop derive-type) ((vector))
182 (assert-array-complex vector))
186 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
188 (define-source-transform vector (&rest elements)
189 (let ((len (length elements))
191 (once-only ((n-vec `(make-array ,len)))
193 ,@(mapcar (lambda (el)
194 (once-only ((n-val el))
195 `(locally (declare (optimize (safety 0)))
196 (setf (svref ,n-vec ,(incf n))
201 ;;; Just convert it into a MAKE-ARRAY.
202 (deftransform make-string ((length &key
203 (element-type 'base-char)
205 #.*default-init-char-form*)))
206 '(make-array (the index length)
207 :element-type element-type
208 :initial-element initial-element))
210 (defstruct (specialized-array-element-type-properties
212 (:constructor !make-saetp (ctype
213 initial-element-default
219 ;; the element type, e.g. #<BUILT-IN-CLASS BASE-CHAR (sealed)> or
220 ;; #<SB-KERNEL:NUMERIC-TYPE (UNSIGNED-BYTE 4)>
221 (ctype (missing-arg) :type ctype :read-only t)
222 ;; what we get when the low-level vector-creation logic zeroes all
223 ;; the bits (which also serves as the default value of MAKE-ARRAY's
224 ;; :INITIAL-ELEMENT keyword)
225 (initial-element-default (missing-arg) :read-only t)
226 ;; how many bits per element
227 (n-bits (missing-arg) :type index :read-only t)
228 ;; the low-level type code
229 (typecode (missing-arg) :type index :read-only t)
230 ;; the number of extra elements we use at the end of the array for
231 ;; low level hackery (e.g., one element for arrays of BASE-CHAR,
232 ;; which is used for a fixed #\NULL so that when we call out to C
233 ;; we don't need to cons a new copy)
234 (n-pad-elements (missing-arg) :type index :read-only t))
236 (defparameter *specialized-array-element-type-properties*
239 (destructuring-bind (type-spec &rest rest) args
240 (let ((ctype (specifier-type type-spec)))
241 (apply #'!make-saetp ctype rest))))
242 `(;; Erm. Yeah. There aren't a lot of things that make sense
243 ;; for an initial element for (ARRAY NIL). -- CSR, 2002-03-07
244 (nil '#:mu 0 ,sb!vm:simple-array-nil-widetag)
245 (base-char ,(code-char 0) 8 ,sb!vm:simple-string-widetag
246 ;; (SIMPLE-STRINGs are stored with an extra trailing
247 ;; #\NULL for convenience in calling out to C.)
249 (single-float 0.0f0 32 ,sb!vm:simple-array-single-float-widetag)
250 (double-float 0.0d0 64 ,sb!vm:simple-array-double-float-widetag)
251 #!+long-float (long-float 0.0L0 #!+x86 96 #!+sparc 128
252 ,sb!vm:simple-array-long-float-widetag)
253 (bit 0 1 ,sb!vm:simple-bit-vector-widetag)
254 ;; KLUDGE: The fact that these UNSIGNED-BYTE entries come
255 ;; before their SIGNED-BYTE partners is significant in the
256 ;; implementation of the compiler; some of the cross-compiler
257 ;; code (see e.g. COERCE-TO-SMALLEST-ELTYPE in
258 ;; src/compiler/debug-dump.lisp) attempts to create an array
259 ;; specialized on (UNSIGNED-BYTE FOO), where FOO could be 7;
260 ;; (UNSIGNED-BYTE 7) is SUBTYPEP (SIGNED-BYTE 8), so if we're
261 ;; not careful we could get the wrong specialized array when
262 ;; we try to FIND-IF, below. -- CSR, 2002-07-08
263 ((unsigned-byte 2) 0 2 ,sb!vm:simple-array-unsigned-byte-2-widetag)
264 ((unsigned-byte 4) 0 4 ,sb!vm:simple-array-unsigned-byte-4-widetag)
265 ((unsigned-byte 8) 0 8 ,sb!vm:simple-array-unsigned-byte-8-widetag)
266 ((unsigned-byte 16) 0 16 ,sb!vm:simple-array-unsigned-byte-16-widetag)
267 ((unsigned-byte 32) 0 32 ,sb!vm:simple-array-unsigned-byte-32-widetag)
268 ((signed-byte 8) 0 8 ,sb!vm:simple-array-signed-byte-8-widetag)
269 ((signed-byte 16) 0 16 ,sb!vm:simple-array-signed-byte-16-widetag)
270 ((signed-byte 30) 0 32 ,sb!vm:simple-array-signed-byte-30-widetag)
271 ((signed-byte 32) 0 32 ,sb!vm:simple-array-signed-byte-32-widetag)
272 ((complex single-float) #C(0.0f0 0.0f0) 64
273 ,sb!vm:simple-array-complex-single-float-widetag)
274 ((complex double-float) #C(0.0d0 0.0d0) 128
275 ,sb!vm:simple-array-complex-double-float-widetag)
276 #!+long-float ((complex long-float) #C(0.0L0 0.0L0)
277 #!+x86 192 #!+sparc 256
278 ,sb!vm:simple-array-complex-long-float-widetag)
279 (t 0 32 ,sb!vm:simple-vector-widetag))))
281 (deftransform make-array ((dims &key initial-element element-type
282 adjustable fill-pointer)
284 (when (null initial-element)
285 (give-up-ir1-transform))
286 (let* ((eltype (cond ((not element-type) t)
287 ((not (constant-continuation-p element-type))
288 (give-up-ir1-transform
289 "ELEMENT-TYPE is not constant."))
291 (continuation-value element-type))))
292 (eltype-type (ir1-transform-specifier-type eltype))
293 (saetp (find-if (lambda (saetp)
294 (csubtypep eltype-type (saetp-ctype saetp)))
295 *specialized-array-element-type-properties*))
296 (creation-form `(make-array dims
297 :element-type ',(type-specifier (saetp-ctype saetp))
299 '(:fill-pointer fill-pointer))
301 '(:adjustable adjustable)))))
304 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
306 (cond ((and (constant-continuation-p initial-element)
307 (eql (continuation-value initial-element)
308 (saetp-initial-element-default saetp)))
311 ;; error checking for target, disabled on the host because
312 ;; (CTYPE-OF #\Null) is not possible.
314 (when (constant-continuation-p initial-element)
315 (let ((value (continuation-value initial-element)))
317 ((not (ctypep value (saetp-ctype saetp)))
318 ;; this case will cause an error at runtime, so we'd
319 ;; better WARN about it now.
320 (compiler-warn "~@<~S is not a ~S (which is the ~
321 UPGRADED-ARRAY-ELEMENT-TYPE of ~S).~@:>"
323 (type-specifier (saetp-ctype saetp))
325 ((not (ctypep value eltype-type))
326 ;; this case will not cause an error at runtime, but
327 ;; it's still worth STYLE-WARNing about.
328 (compiler-style-warn "~S is not a ~S."
330 `(let ((array ,creation-form))
331 (multiple-value-bind (vector)
332 (%data-vector-and-index array 0)
333 (fill vector initial-element))
336 ;;; The integer type restriction on the length ensures that it will be
337 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
338 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
339 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
340 ;;; kind of initialization efficiently.
341 (deftransform make-array ((length &key element-type)
343 (let* ((eltype (cond ((not element-type) t)
344 ((not (constant-continuation-p element-type))
345 (give-up-ir1-transform
346 "ELEMENT-TYPE is not constant."))
348 (continuation-value element-type))))
349 (len (if (constant-continuation-p length)
350 (continuation-value length)
352 (eltype-type (ir1-transform-specifier-type eltype))
355 ,(if (unknown-type-p eltype-type)
356 (give-up-ir1-transform
357 "ELEMENT-TYPE is an unknown type: ~S" eltype)
358 (sb!xc:upgraded-array-element-type eltype))
360 (saetp (find-if (lambda (saetp)
361 (csubtypep eltype-type (saetp-ctype saetp)))
362 *specialized-array-element-type-properties*)))
364 (give-up-ir1-transform
365 "cannot open-code creation of ~S" result-type-spec))
367 (unless (csubtypep (ctype-of (saetp-initial-element-default saetp))
369 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
370 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
371 ;; INITIAL-ELEMENT is not supplied, the consequences of later
372 ;; reading an uninitialized element of new-array are undefined,"
373 ;; so this could be legal code as long as the user plans to
374 ;; write before he reads, and if he doesn't we're free to do
375 ;; anything we like. But in case the user doesn't know to write
376 ;; elements before he reads elements (or to read manuals before
377 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
378 ;; didn't realize this.
379 (compiler-style-warn "The default initial element ~S is not a ~S."
380 (saetp-initial-element-default saetp)
382 (let* ((n-bits-per-element (saetp-n-bits saetp))
383 (typecode (saetp-typecode saetp))
384 (n-pad-elements (saetp-n-pad-elements saetp))
385 (padded-length-form (if (zerop n-pad-elements)
387 `(+ length ,n-pad-elements)))
390 ((= n-bits-per-element 0) 0)
391 ((>= n-bits-per-element sb!vm:n-word-bits)
392 `(* ,padded-length-form
393 (the fixnum ; i.e., not RATIO
394 ,(/ n-bits-per-element sb!vm:n-word-bits))))
396 (let ((n-elements-per-word (/ sb!vm:n-word-bits
397 n-bits-per-element)))
398 (declare (type index n-elements-per-word)) ; i.e., not RATIO
399 `(ceiling ,padded-length-form ,n-elements-per-word))))))
401 `(truly-the ,result-type-spec
402 (allocate-vector ,typecode length ,n-words-form))
403 '((declare (type index length)))))))
405 ;;; The list type restriction does not ensure that the result will be a
406 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
407 ;;; and displaced-to keywords ensures that it will be simple.
409 ;;; FIXME: should we generalize this transform to non-simple (though
410 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
411 ;;; deal with those? Maybe when the DEFTRANSFORM
412 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
414 (deftransform make-array ((dims &key element-type)
416 (unless (or (null element-type) (constant-continuation-p element-type))
417 (give-up-ir1-transform
418 "The element-type is not constant; cannot open code array creation."))
419 (unless (constant-continuation-p dims)
420 (give-up-ir1-transform
421 "The dimension list is not constant; cannot open code array creation."))
422 (let ((dims (continuation-value dims)))
423 (unless (every #'integerp dims)
424 (give-up-ir1-transform
425 "The dimension list contains something other than an integer: ~S"
427 (if (= (length dims) 1)
428 `(make-array ',(car dims)
430 '(:element-type element-type)))
431 (let* ((total-size (reduce #'* dims))
434 ,(cond ((null element-type) t)
435 ((and (constant-continuation-p element-type)
436 (ir1-transform-specifier-type
437 (continuation-value element-type)))
438 (sb!xc:upgraded-array-element-type
439 (continuation-value element-type)))
441 ,(make-list rank :initial-element '*))))
442 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank)))
443 (setf (%array-fill-pointer header) ,total-size)
444 (setf (%array-fill-pointer-p header) nil)
445 (setf (%array-available-elements header) ,total-size)
446 (setf (%array-data-vector header)
447 (make-array ,total-size
449 '(:element-type element-type))))
450 (setf (%array-displaced-p header) nil)
452 (mapcar (lambda (dim)
453 `(setf (%array-dimension header ,(incf axis))
456 (truly-the ,spec header))))))
458 ;;;; miscellaneous properties of arrays
460 ;;; Transforms for various array properties. If the property is know
461 ;;; at compile time because of a type spec, use that constant value.
463 ;;; If we can tell the rank from the type info, use it instead.
464 (deftransform array-rank ((array))
465 (let ((array-type (continuation-type array)))
466 (unless (array-type-p array-type)
467 (give-up-ir1-transform))
468 (let ((dims (array-type-dimensions array-type)))
469 (if (not (listp dims))
470 (give-up-ir1-transform
471 "The array rank is not known at compile time: ~S"
475 ;;; If we know the dimensions at compile time, just use it. Otherwise,
476 ;;; if we can tell that the axis is in bounds, convert to
477 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
478 ;;; (if it's simple and a vector).
479 (deftransform array-dimension ((array axis)
481 (unless (constant-continuation-p axis)
482 (give-up-ir1-transform "The axis is not constant."))
483 (let ((array-type (continuation-type array))
484 (axis (continuation-value axis)))
485 (unless (array-type-p array-type)
486 (give-up-ir1-transform))
487 (let ((dims (array-type-dimensions array-type)))
489 (give-up-ir1-transform
490 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
491 (unless (> (length dims) axis)
492 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
495 (let ((dim (nth axis dims)))
496 (cond ((integerp dim)
499 (ecase (array-type-complexp array-type)
501 '(%array-dimension array 0))
505 (give-up-ir1-transform
506 "can't tell whether array is simple"))))
508 '(%array-dimension array axis)))))))
510 ;;; If the length has been declared and it's simple, just return it.
511 (deftransform length ((vector)
512 ((simple-array * (*))))
513 (let ((type (continuation-type vector)))
514 (unless (array-type-p type)
515 (give-up-ir1-transform))
516 (let ((dims (array-type-dimensions type)))
517 (unless (and (listp dims) (integerp (car dims)))
518 (give-up-ir1-transform
519 "Vector length is unknown, must call LENGTH at runtime."))
522 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
523 ;;; simple, it will extract the length slot from the vector. It it's
524 ;;; complex, it will extract the fill pointer slot from the array
526 (deftransform length ((vector) (vector))
527 '(vector-length vector))
529 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
530 ;;; compile-time constant.
531 (deftransform vector-length ((vector))
532 (let ((vtype (continuation-type vector)))
533 (if (and (array-type-p vtype)
534 (not (array-type-complexp vtype)))
535 (let ((dim (first (array-type-dimensions vtype))))
536 (when (eq dim '*) (give-up-ir1-transform))
538 (give-up-ir1-transform))))
540 ;;; Again, if we can tell the results from the type, just use it.
541 ;;; Otherwise, if we know the rank, convert into a computation based
542 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
543 ;;; multiplications because we know that the total size must be an
545 (deftransform array-total-size ((array)
547 (let ((array-type (continuation-type array)))
548 (unless (array-type-p array-type)
549 (give-up-ir1-transform))
550 (let ((dims (array-type-dimensions array-type)))
552 (give-up-ir1-transform "can't tell the rank at compile time"))
554 (do ((form 1 `(truly-the index
555 (* (array-dimension array ,i) ,form)))
557 ((= i (length dims)) form))
558 (reduce #'* dims)))))
560 ;;; Only complex vectors have fill pointers.
561 (deftransform array-has-fill-pointer-p ((array))
562 (let ((array-type (continuation-type array)))
563 (unless (array-type-p array-type)
564 (give-up-ir1-transform))
565 (let ((dims (array-type-dimensions array-type)))
566 (if (and (listp dims) (not (= (length dims) 1)))
568 (ecase (array-type-complexp array-type)
574 (give-up-ir1-transform
575 "The array type is ambiguous; must call ~
576 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
578 ;;; Primitive used to verify indices into arrays. If we can tell at
579 ;;; compile-time or we are generating unsafe code, don't bother with
581 (deftransform %check-bound ((array dimension index) * * :node node)
582 (cond ((policy node (and (> speed safety) (= safety 0)))
584 ((not (constant-continuation-p dimension))
585 (give-up-ir1-transform))
587 (let ((dim (continuation-value dimension)))
588 `(the (integer 0 (,dim)) index)))))
592 ;;; This checks to see whether the array is simple and the start and
593 ;;; end are in bounds. If so, it proceeds with those values.
594 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
595 ;;; may be further optimized.
597 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
598 ;;; START-VAR and END-VAR to the start and end of the designated
599 ;;; portion of the data vector. SVALUE and EVALUE are any start and
600 ;;; end specified to the original operation, and are factored into the
601 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
602 ;;; offset of all displacements encountered, and does not include
605 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
606 ;;; forced to be inline, overriding the ordinary judgment of the
607 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
608 ;;; fairly picky about their arguments, figuring that if you haven't
609 ;;; bothered to get all your ducks in a row, you probably don't care
610 ;;; that much about speed anyway! But in some cases it makes sense to
611 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
612 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
613 ;;; sense to use FORCE-INLINE option in that case.
614 (def!macro with-array-data (((data-var array &key offset-var)
615 (start-var &optional (svalue 0))
616 (end-var &optional (evalue nil))
619 (once-only ((n-array array)
620 (n-svalue `(the index ,svalue))
621 (n-evalue `(the (or index null) ,evalue)))
622 `(multiple-value-bind (,data-var
625 ,@(when offset-var `(,offset-var)))
626 (if (not (array-header-p ,n-array))
627 (let ((,n-array ,n-array))
628 (declare (type (simple-array * (*)) ,n-array))
629 ,(once-only ((n-len `(length ,n-array))
630 (n-end `(or ,n-evalue ,n-len)))
631 `(if (<= ,n-svalue ,n-end ,n-len)
633 (values ,n-array ,n-svalue ,n-end 0)
634 (failed-%with-array-data ,n-array
637 (,(if force-inline '%with-array-data-macro '%with-array-data)
638 ,n-array ,n-svalue ,n-evalue))
641 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
642 ;;; DEFTRANSFORMs and DEFUNs.
643 (def!macro %with-array-data-macro (array
650 (with-unique-names (size defaulted-end data cumulative-offset)
651 `(let* ((,size (array-total-size ,array))
654 (unless (or ,unsafe? (<= ,end ,size))
656 `(error 'bounding-indices-bad-error
657 :datum (cons ,start ,end)
658 :expected-type `(cons (integer 0 ,',size)
659 (integer ,',start ,',size))
661 `(failed-%with-array-data ,array ,start ,end)))
664 (unless (or ,unsafe? (<= ,start ,defaulted-end))
666 `(error 'bounding-indices-bad-error
667 :datum (cons ,start ,end)
668 :expected-type `(cons (integer 0 ,',size)
669 (integer ,',start ,',size))
671 `(failed-%with-array-data ,array ,start ,end)))
672 (do ((,data ,array (%array-data-vector ,data))
673 (,cumulative-offset 0
674 (+ ,cumulative-offset
675 (%array-displacement ,data))))
676 ((not (array-header-p ,data))
677 (values (the (simple-array ,element-type 1) ,data)
678 (the index (+ ,cumulative-offset ,start))
679 (the index (+ ,cumulative-offset ,defaulted-end))
680 (the index ,cumulative-offset)))
681 (declare (type index ,cumulative-offset))))))
683 (deftransform %with-array-data ((array start end)
684 ;; It might very well be reasonable to
685 ;; allow general ARRAY here, I just
686 ;; haven't tried to understand the
687 ;; performance issues involved. --
688 ;; WHN, and also CSR 2002-05-26
689 ((or vector simple-array) index (or index null))
693 :policy (> speed space))
694 "inline non-SIMPLE-vector-handling logic"
695 (let ((element-type (upgraded-element-type-specifier-or-give-up array)))
696 `(%with-array-data-macro array start end
697 :unsafe? ,(policy node (= safety 0))
698 :element-type ,element-type)))
702 ;;; We convert all typed array accessors into AREF and %ASET with type
703 ;;; assertions on the array.
704 (macrolet ((define-frob (reffer setter type)
706 (define-source-transform ,reffer (a &rest i)
707 `(aref (the ,',type ,a) ,@i))
708 (define-source-transform ,setter (a &rest i)
709 `(%aset (the ,',type ,a) ,@i)))))
710 (define-frob sbit %sbitset (simple-array bit))
711 (define-frob bit %bitset (array bit)))
712 (macrolet ((define-frob (reffer setter type)
714 (define-source-transform ,reffer (a i)
715 `(aref (the ,',type ,a) ,i))
716 (define-source-transform ,setter (a i v)
717 `(%aset (the ,',type ,a) ,i ,v)))))
718 (define-frob svref %svset simple-vector)
719 (define-frob schar %scharset simple-string)
720 (define-frob char %charset string))
722 (macrolet (;; This is a handy macro for computing the row-major index
723 ;; given a set of indices. We wrap each index with a call
724 ;; to %CHECK-BOUND to ensure that everything works out
725 ;; correctly. We can wrap all the interior arithmetic with
726 ;; TRULY-THE INDEX because we know the the resultant
727 ;; row-major index must be an index.
728 (with-row-major-index ((array indices index &optional new-value)
730 `(let (n-indices dims)
731 (dotimes (i (length ,indices))
732 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
733 (push (make-symbol (format nil "DIM-~D" i)) dims))
734 (setf n-indices (nreverse n-indices))
735 (setf dims (nreverse dims))
736 `(lambda (,',array ,@n-indices
737 ,@',(when new-value (list new-value)))
738 (let* (,@(let ((,index -1))
739 (mapcar (lambda (name)
740 `(,name (array-dimension
747 (do* ((dims dims (cdr dims))
748 (indices n-indices (cdr indices))
749 (last-dim nil (car dims))
750 (form `(%check-bound ,',array
762 ((null (cdr dims)) form)))))
765 ;; Just return the index after computing it.
766 (deftransform array-row-major-index ((array &rest indices))
767 (with-row-major-index (array indices index)
770 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
771 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
772 ;; expression for the row major index.
773 (deftransform aref ((array &rest indices))
774 (with-row-major-index (array indices index)
775 (hairy-data-vector-ref array index)))
776 (deftransform %aset ((array &rest stuff))
777 (let ((indices (butlast stuff)))
778 (with-row-major-index (array indices index new-value)
779 (hairy-data-vector-set array index new-value)))))
781 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
782 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
783 ;;; array total size.
784 (deftransform row-major-aref ((array index))
785 `(hairy-data-vector-ref array
786 (%check-bound array (array-total-size array) index)))
787 (deftransform %set-row-major-aref ((array index new-value))
788 `(hairy-data-vector-set array
789 (%check-bound array (array-total-size array) index)
792 ;;;; bit-vector array operation canonicalization
794 ;;;; We convert all bit-vector operations to have the result array
795 ;;;; specified. This allows any result allocation to be open-coded,
796 ;;;; and eliminates the need for any VM-dependent transforms to handle
799 (macrolet ((def (fun)
801 (deftransform ,fun ((bit-array-1 bit-array-2
802 &optional result-bit-array)
803 (bit-vector bit-vector &optional null) *
804 :policy (>= speed space))
805 `(,',fun bit-array-1 bit-array-2
806 (make-array (length bit-array-1) :element-type 'bit)))
807 ;; If result is T, make it the first arg.
808 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
809 (bit-vector bit-vector (member t)) *)
810 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
822 ;;; Similar for BIT-NOT, but there is only one arg...
823 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
824 (bit-vector &optional null) *
825 :policy (>= speed space))
826 '(bit-not bit-array-1
827 (make-array (length bit-array-1) :element-type 'bit)))
828 (deftransform bit-not ((bit-array-1 result-bit-array)
829 (bit-vector (constant-arg t)))
830 '(bit-not bit-array-1 bit-array-1))
831 ;;; FIXME: What does (CONSTANT-ARG T) mean? Is it the same thing
832 ;;; as (CONSTANT-ARG (MEMBER T)), or does it mean any constant
835 ;;; Pick off some constant cases.
836 (deftransform array-header-p ((array) (array))
837 (let ((type (continuation-type array)))
838 (unless (array-type-p type)
839 (give-up-ir1-transform))
840 (let ((dims (array-type-dimensions type)))
841 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
844 ((and (listp dims) (/= (length dims) 1))
845 ;; multi-dimensional array, will have a header
848 (give-up-ir1-transform))))))