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)))
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 (lvar-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 (lvar-type array)))
55 (when (array-type-p type)
58 (array-type-specialized-element-type type)
59 (lexenv-policy (node-lexenv (lvar-dest new-value))))))
60 (lvar-type new-value))
62 (defun assert-array-complex (array)
65 (make-array-type :complexp t
66 :element-type *wild-type*)
67 (lexenv-policy (node-lexenv (lvar-dest array))))
70 ;;; Return true if ARG is NIL, or is a constant-lvar whose
71 ;;; value is NIL, false otherwise.
72 (defun unsupplied-or-nil (arg)
73 (declare (type (or lvar null) arg))
75 (and (constant-lvar-p arg)
76 (not (lvar-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)
85 (specifier-type `(array * ,(make-list rank :initial-element '*)))
86 (lexenv-policy (node-lexenv (lvar-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 (extract-upgraded-element-type array))
96 (defoptimizer (%aset derive-type) ((array &rest stuff))
97 (assert-array-rank array (1- (length stuff)))
98 (assert-new-value-type (car (last stuff)) array))
100 (defoptimizer (hairy-data-vector-ref derive-type) ((array index))
101 (extract-upgraded-element-type array))
102 (defoptimizer (data-vector-ref derive-type) ((array index))
103 (extract-upgraded-element-type array))
105 (defoptimizer (data-vector-set derive-type) ((array index new-value))
106 (assert-new-value-type new-value array))
107 (defoptimizer (hairy-data-vector-set derive-type) ((array index new-value))
108 (assert-new-value-type new-value array))
110 ;;; Figure out the type of the data vector if we know the argument
112 (defoptimizer (%with-array-data derive-type) ((array start end))
113 (let ((atype (lvar-type array)))
114 (when (array-type-p atype)
116 `(simple-array ,(type-specifier
117 (array-type-specialized-element-type atype))
120 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
121 (assert-array-rank array (length indices))
124 (defoptimizer (row-major-aref derive-type) ((array index))
125 (extract-upgraded-element-type array))
127 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
128 (assert-new-value-type new-value array))
130 (defoptimizer (make-array derive-type)
131 ((dims &key initial-element element-type initial-contents
132 adjustable fill-pointer displaced-index-offset displaced-to))
133 (let ((simple (and (unsupplied-or-nil adjustable)
134 (unsupplied-or-nil displaced-to)
135 (unsupplied-or-nil fill-pointer))))
136 (or (careful-specifier-type
137 `(,(if simple 'simple-array 'array)
138 ,(cond ((not element-type) t)
139 ((constant-lvar-p element-type)
140 (let ((ctype (careful-specifier-type
141 (lvar-value element-type))))
143 ((or (null ctype) (unknown-type-p ctype)) '*)
144 (t (sb!xc:upgraded-array-element-type
145 (lvar-value element-type))))))
148 ,(cond ((constant-lvar-p dims)
149 (let* ((val (lvar-value dims))
150 (cdims (if (listp val) val (list val))))
154 ((csubtypep (lvar-type dims)
155 (specifier-type 'integer))
159 (specifier-type 'array))))
161 ;;; Complex array operations should assert that their array argument
162 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
163 (defoptimizer (fill-pointer derive-type) ((vector))
164 (assert-array-complex vector))
165 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
166 (declare (ignorable index))
167 (assert-array-complex vector))
169 (defoptimizer (vector-push derive-type) ((object vector))
170 (declare (ignorable object))
171 (assert-array-complex vector))
172 (defoptimizer (vector-push-extend derive-type)
173 ((object vector &optional index))
174 (declare (ignorable object index))
175 (assert-array-complex vector))
176 (defoptimizer (vector-pop derive-type) ((vector))
177 (assert-array-complex vector))
181 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
183 (define-source-transform vector (&rest elements)
184 (let ((len (length elements))
186 (once-only ((n-vec `(make-array ,len)))
188 ,@(mapcar (lambda (el)
189 (once-only ((n-val el))
190 `(locally (declare (optimize (safety 0)))
191 (setf (svref ,n-vec ,(incf n))
196 ;;; Just convert it into a MAKE-ARRAY.
197 (deftransform make-string ((length &key
198 (element-type 'character)
200 #.*default-init-char-form*)))
201 `(the simple-string (make-array (the index length)
202 :element-type element-type
203 ,@(when initial-element
204 '(:initial-element initial-element)))))
206 (deftransform make-array ((dims &key initial-element element-type
207 adjustable fill-pointer)
209 (when (null initial-element)
210 (give-up-ir1-transform))
211 (let* ((eltype (cond ((not element-type) t)
212 ((not (constant-lvar-p element-type))
213 (give-up-ir1-transform
214 "ELEMENT-TYPE is not constant."))
216 (lvar-value element-type))))
217 (eltype-type (ir1-transform-specifier-type eltype))
218 (saetp (find-if (lambda (saetp)
219 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
220 sb!vm:*specialized-array-element-type-properties*))
221 (creation-form `(make-array dims
222 :element-type ',(type-specifier (sb!vm:saetp-ctype saetp))
224 '(:fill-pointer fill-pointer))
226 '(:adjustable adjustable)))))
229 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
231 (cond ((and (constant-lvar-p initial-element)
232 (eql (lvar-value initial-element)
233 (sb!vm:saetp-initial-element-default saetp)))
236 ;; error checking for target, disabled on the host because
237 ;; (CTYPE-OF #\Null) is not possible.
239 (when (constant-lvar-p initial-element)
240 (let ((value (lvar-value initial-element)))
242 ((not (ctypep value (sb!vm:saetp-ctype saetp)))
243 ;; this case will cause an error at runtime, so we'd
244 ;; better WARN about it now.
245 (warn 'array-initial-element-mismatch
246 :format-control "~@<~S is not a ~S (which is the ~
251 (type-specifier (sb!vm:saetp-ctype saetp))
252 'upgraded-array-element-type
254 ((not (ctypep value eltype-type))
255 ;; this case will not cause an error at runtime, but
256 ;; it's still worth STYLE-WARNing about.
257 (compiler-style-warn "~S is not a ~S."
259 `(let ((array ,creation-form))
260 (multiple-value-bind (vector)
261 (%data-vector-and-index array 0)
262 (fill vector initial-element))
265 ;;; The integer type restriction on the length ensures that it will be
266 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
267 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
268 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
269 ;;; kind of initialization efficiently.
270 (deftransform make-array ((length &key element-type)
272 (let* ((eltype (cond ((not element-type) t)
273 ((not (constant-lvar-p element-type))
274 (give-up-ir1-transform
275 "ELEMENT-TYPE is not constant."))
277 (lvar-value element-type))))
278 (len (if (constant-lvar-p length)
281 (eltype-type (ir1-transform-specifier-type eltype))
284 ,(if (unknown-type-p eltype-type)
285 (give-up-ir1-transform
286 "ELEMENT-TYPE is an unknown type: ~S" eltype)
287 (sb!xc:upgraded-array-element-type eltype))
289 (saetp (find-if (lambda (saetp)
290 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
291 sb!vm:*specialized-array-element-type-properties*)))
293 (give-up-ir1-transform
294 "cannot open-code creation of ~S" result-type-spec))
296 (unless (csubtypep (ctype-of (sb!vm:saetp-initial-element-default saetp))
298 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
299 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
300 ;; INITIAL-ELEMENT is not supplied, the consequences of later
301 ;; reading an uninitialized element of new-array are undefined,"
302 ;; so this could be legal code as long as the user plans to
303 ;; write before he reads, and if he doesn't we're free to do
304 ;; anything we like. But in case the user doesn't know to write
305 ;; elements before he reads elements (or to read manuals before
306 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
307 ;; didn't realize this.
308 (compiler-style-warn "The default initial element ~S is not a ~S."
309 (sb!vm:saetp-initial-element-default saetp)
311 (let* ((n-bits-per-element (sb!vm:saetp-n-bits saetp))
312 (typecode (sb!vm:saetp-typecode saetp))
313 (n-pad-elements (sb!vm:saetp-n-pad-elements saetp))
314 (padded-length-form (if (zerop n-pad-elements)
316 `(+ length ,n-pad-elements)))
319 ((= n-bits-per-element 0) 0)
320 ((>= n-bits-per-element sb!vm:n-word-bits)
321 `(* ,padded-length-form
322 (the fixnum ; i.e., not RATIO
323 ,(/ n-bits-per-element sb!vm:n-word-bits))))
325 (let ((n-elements-per-word (/ sb!vm:n-word-bits
326 n-bits-per-element)))
327 (declare (type index n-elements-per-word)) ; i.e., not RATIO
328 `(ceiling ,padded-length-form ,n-elements-per-word))))))
330 `(truly-the ,result-type-spec
331 (allocate-vector ,typecode length ,n-words-form))
332 '((declare (type index length)))))))
334 ;;; The list type restriction does not ensure that the result will be a
335 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
336 ;;; and displaced-to keywords ensures that it will be simple.
338 ;;; FIXME: should we generalize this transform to non-simple (though
339 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
340 ;;; deal with those? Maybe when the DEFTRANSFORM
341 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
343 (deftransform make-array ((dims &key element-type)
345 (unless (or (null element-type) (constant-lvar-p element-type))
346 (give-up-ir1-transform
347 "The element-type is not constant; cannot open code array creation."))
348 (unless (constant-lvar-p dims)
349 (give-up-ir1-transform
350 "The dimension list is not constant; cannot open code array creation."))
351 (let ((dims (lvar-value dims)))
352 (unless (every #'integerp dims)
353 (give-up-ir1-transform
354 "The dimension list contains something other than an integer: ~S"
356 (if (= (length dims) 1)
357 `(make-array ',(car dims)
359 '(:element-type element-type)))
360 (let* ((total-size (reduce #'* dims))
363 ,(cond ((null element-type) t)
364 ((and (constant-lvar-p element-type)
365 (ir1-transform-specifier-type
366 (lvar-value element-type)))
367 (sb!xc:upgraded-array-element-type
368 (lvar-value element-type)))
370 ,(make-list rank :initial-element '*))))
371 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank)))
372 (setf (%array-fill-pointer header) ,total-size)
373 (setf (%array-fill-pointer-p header) nil)
374 (setf (%array-available-elements header) ,total-size)
375 (setf (%array-data-vector header)
376 (make-array ,total-size
378 '(:element-type element-type))))
379 (setf (%array-displaced-p header) nil)
381 (mapcar (lambda (dim)
382 `(setf (%array-dimension header ,(incf axis))
385 (truly-the ,spec header))))))
387 ;;;; miscellaneous properties of arrays
389 ;;; Transforms for various array properties. If the property is know
390 ;;; at compile time because of a type spec, use that constant value.
392 ;;; Most of this logic may end up belonging in code/late-type.lisp;
393 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
394 ;;; maybe this is just too sloppy for actual type logic. -- CSR,
396 (defun array-type-dimensions-or-give-up (type)
398 (array-type (array-type-dimensions type))
400 (let ((types (union-type-types type)))
401 ;; there are at least two types, right?
402 (aver (> (length types) 1))
403 (let ((result (array-type-dimensions-or-give-up (car types))))
404 (dolist (type (cdr types) result)
405 (unless (equal (array-type-dimensions-or-give-up type) result)
406 (give-up-ir1-transform))))))
407 ;; FIXME: intersection type [e.g. (and (array * (*)) (satisfies foo)) ]
408 (t (give-up-ir1-transform))))
410 (defun conservative-array-type-complexp (type)
412 (array-type (array-type-complexp type))
414 (let ((types (union-type-types type)))
415 (aver (> (length types) 1))
416 (let ((result (conservative-array-type-complexp (car types))))
417 (dolist (type (cdr types) result)
418 (unless (eq (conservative-array-type-complexp type) result)
419 (return-from conservative-array-type-complexp :maybe))))))
420 ;; FIXME: intersection type
423 ;;; If we can tell the rank from the type info, use it instead.
424 (deftransform array-rank ((array))
425 (let ((array-type (lvar-type array)))
426 (let ((dims (array-type-dimensions-or-give-up array-type)))
427 (if (not (listp dims))
428 (give-up-ir1-transform
429 "The array rank is not known at compile time: ~S"
433 ;;; If we know the dimensions at compile time, just use it. Otherwise,
434 ;;; if we can tell that the axis is in bounds, convert to
435 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
436 ;;; (if it's simple and a vector).
437 (deftransform array-dimension ((array axis)
439 (unless (constant-lvar-p axis)
440 (give-up-ir1-transform "The axis is not constant."))
441 (let ((array-type (lvar-type array))
442 (axis (lvar-value axis)))
443 (let ((dims (array-type-dimensions-or-give-up array-type)))
445 (give-up-ir1-transform
446 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
447 (unless (> (length dims) axis)
448 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
451 (let ((dim (nth axis dims)))
452 (cond ((integerp dim)
455 (ecase (conservative-array-type-complexp array-type)
457 '(%array-dimension array 0))
461 (give-up-ir1-transform
462 "can't tell whether array is simple"))))
464 '(%array-dimension array axis)))))))
466 ;;; If the length has been declared and it's simple, just return it.
467 (deftransform length ((vector)
468 ((simple-array * (*))))
469 (let ((type (lvar-type vector)))
470 (let ((dims (array-type-dimensions-or-give-up type)))
471 (unless (and (listp dims) (integerp (car dims)))
472 (give-up-ir1-transform
473 "Vector length is unknown, must call LENGTH at runtime."))
476 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
477 ;;; simple, it will extract the length slot from the vector. It it's
478 ;;; complex, it will extract the fill pointer slot from the array
480 (deftransform length ((vector) (vector))
481 '(vector-length vector))
483 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
484 ;;; compile-time constant.
485 (deftransform vector-length ((vector))
486 (let ((vtype (lvar-type vector)))
487 (let ((dim (first (array-type-dimensions-or-give-up vtype))))
489 (give-up-ir1-transform))
490 (when (conservative-array-type-complexp vtype)
491 (give-up-ir1-transform))
494 ;;; Again, if we can tell the results from the type, just use it.
495 ;;; Otherwise, if we know the rank, convert into a computation based
496 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
497 ;;; multiplications because we know that the total size must be an
499 (deftransform array-total-size ((array)
501 (let ((array-type (lvar-type array)))
502 (let ((dims (array-type-dimensions-or-give-up array-type)))
504 (give-up-ir1-transform "can't tell the rank at compile time"))
506 (do ((form 1 `(truly-the index
507 (* (array-dimension array ,i) ,form)))
509 ((= i (length dims)) form))
510 (reduce #'* dims)))))
512 ;;; Only complex vectors have fill pointers.
513 (deftransform array-has-fill-pointer-p ((array))
514 (let ((array-type (lvar-type array)))
515 (let ((dims (array-type-dimensions-or-give-up array-type)))
516 (if (and (listp dims) (not (= (length dims) 1)))
518 (ecase (conservative-array-type-complexp array-type)
524 (give-up-ir1-transform
525 "The array type is ambiguous; must call ~
526 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
528 ;;; Primitive used to verify indices into arrays. If we can tell at
529 ;;; compile-time or we are generating unsafe code, don't bother with
531 (deftransform %check-bound ((array dimension index) * * :node node)
532 (cond ((policy node (and (> speed safety) (= safety 0)))
534 ((not (constant-lvar-p dimension))
535 (give-up-ir1-transform))
537 (let ((dim (lvar-value dimension)))
538 `(the (integer 0 (,dim)) index)))))
542 ;;; This checks to see whether the array is simple and the start and
543 ;;; end are in bounds. If so, it proceeds with those values.
544 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
545 ;;; may be further optimized.
547 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
548 ;;; START-VAR and END-VAR to the start and end of the designated
549 ;;; portion of the data vector. SVALUE and EVALUE are any start and
550 ;;; end specified to the original operation, and are factored into the
551 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
552 ;;; offset of all displacements encountered, and does not include
555 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
556 ;;; forced to be inline, overriding the ordinary judgment of the
557 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
558 ;;; fairly picky about their arguments, figuring that if you haven't
559 ;;; bothered to get all your ducks in a row, you probably don't care
560 ;;; that much about speed anyway! But in some cases it makes sense to
561 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
562 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
563 ;;; sense to use FORCE-INLINE option in that case.
564 (def!macro with-array-data (((data-var array &key offset-var)
565 (start-var &optional (svalue 0))
566 (end-var &optional (evalue nil))
569 (once-only ((n-array array)
570 (n-svalue `(the index ,svalue))
571 (n-evalue `(the (or index null) ,evalue)))
572 `(multiple-value-bind (,data-var
575 ,@(when offset-var `(,offset-var)))
576 (if (not (array-header-p ,n-array))
577 (let ((,n-array ,n-array))
578 (declare (type (simple-array * (*)) ,n-array))
579 ,(once-only ((n-len `(length ,n-array))
580 (n-end `(or ,n-evalue ,n-len)))
581 `(if (<= ,n-svalue ,n-end ,n-len)
583 (values ,n-array ,n-svalue ,n-end 0)
584 (failed-%with-array-data ,n-array
587 (,(if force-inline '%with-array-data-macro '%with-array-data)
588 ,n-array ,n-svalue ,n-evalue))
591 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
592 ;;; DEFTRANSFORMs and DEFUNs.
593 (def!macro %with-array-data-macro (array
600 (with-unique-names (size defaulted-end data cumulative-offset)
601 `(let* ((,size (array-total-size ,array))
604 (unless (or ,unsafe? (<= ,end ,size))
606 `(error 'bounding-indices-bad-error
607 :datum (cons ,start ,end)
608 :expected-type `(cons (integer 0 ,',size)
609 (integer ,',start ,',size))
611 `(failed-%with-array-data ,array ,start ,end)))
614 (unless (or ,unsafe? (<= ,start ,defaulted-end))
616 `(error 'bounding-indices-bad-error
617 :datum (cons ,start ,end)
618 :expected-type `(cons (integer 0 ,',size)
619 (integer ,',start ,',size))
621 `(failed-%with-array-data ,array ,start ,end)))
622 (do ((,data ,array (%array-data-vector ,data))
623 (,cumulative-offset 0
624 (+ ,cumulative-offset
625 (%array-displacement ,data))))
626 ((not (array-header-p ,data))
627 (values (the (simple-array ,element-type 1) ,data)
628 (the index (+ ,cumulative-offset ,start))
629 (the index (+ ,cumulative-offset ,defaulted-end))
630 (the index ,cumulative-offset)))
631 (declare (type index ,cumulative-offset))))))
633 (deftransform %with-array-data ((array start end)
634 ;; It might very well be reasonable to
635 ;; allow general ARRAY here, I just
636 ;; haven't tried to understand the
637 ;; performance issues involved. --
638 ;; WHN, and also CSR 2002-05-26
639 ((or vector simple-array) index (or index null))
642 :policy (> speed space))
643 "inline non-SIMPLE-vector-handling logic"
644 (let ((element-type (upgraded-element-type-specifier-or-give-up array)))
645 `(%with-array-data-macro array start end
646 :unsafe? ,(policy node (= safety 0))
647 :element-type ,element-type)))
651 ;;; We convert all typed array accessors into AREF and %ASET with type
652 ;;; assertions on the array.
653 (macrolet ((define-frob (reffer setter type)
655 (define-source-transform ,reffer (a &rest i)
656 `(aref (the ,',type ,a) ,@i))
657 (define-source-transform ,setter (a &rest i)
658 `(%aset (the ,',type ,a) ,@i)))))
659 (define-frob sbit %sbitset (simple-array bit))
660 (define-frob bit %bitset (array bit)))
661 (macrolet ((define-frob (reffer setter type)
663 (define-source-transform ,reffer (a i)
664 `(aref (the ,',type ,a) ,i))
665 (define-source-transform ,setter (a i v)
666 `(%aset (the ,',type ,a) ,i ,v)))))
667 (define-frob svref %svset simple-vector)
668 (define-frob schar %scharset simple-string)
669 (define-frob char %charset string))
671 (macrolet (;; This is a handy macro for computing the row-major index
672 ;; given a set of indices. We wrap each index with a call
673 ;; to %CHECK-BOUND to ensure that everything works out
674 ;; correctly. We can wrap all the interior arithmetic with
675 ;; TRULY-THE INDEX because we know the the resultant
676 ;; row-major index must be an index.
677 (with-row-major-index ((array indices index &optional new-value)
679 `(let (n-indices dims)
680 (dotimes (i (length ,indices))
681 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
682 (push (make-symbol (format nil "DIM-~D" i)) dims))
683 (setf n-indices (nreverse n-indices))
684 (setf dims (nreverse dims))
685 `(lambda (,',array ,@n-indices
686 ,@',(when new-value (list new-value)))
687 (let* (,@(let ((,index -1))
688 (mapcar (lambda (name)
689 `(,name (array-dimension
696 (do* ((dims dims (cdr dims))
697 (indices n-indices (cdr indices))
698 (last-dim nil (car dims))
699 (form `(%check-bound ,',array
711 ((null (cdr dims)) form)))))
714 ;; Just return the index after computing it.
715 (deftransform array-row-major-index ((array &rest indices))
716 (with-row-major-index (array indices index)
719 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
720 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
721 ;; expression for the row major index.
722 (deftransform aref ((array &rest indices))
723 (with-row-major-index (array indices index)
724 (hairy-data-vector-ref array index)))
725 (deftransform %aset ((array &rest stuff))
726 (let ((indices (butlast stuff)))
727 (with-row-major-index (array indices index new-value)
728 (hairy-data-vector-set array index new-value)))))
730 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
731 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
732 ;;; array total size.
733 (deftransform row-major-aref ((array index))
734 `(hairy-data-vector-ref array
735 (%check-bound array (array-total-size array) index)))
736 (deftransform %set-row-major-aref ((array index new-value))
737 `(hairy-data-vector-set array
738 (%check-bound array (array-total-size array) index)
741 ;;;; bit-vector array operation canonicalization
743 ;;;; We convert all bit-vector operations to have the result array
744 ;;;; specified. This allows any result allocation to be open-coded,
745 ;;;; and eliminates the need for any VM-dependent transforms to handle
748 (macrolet ((def (fun)
750 (deftransform ,fun ((bit-array-1 bit-array-2
751 &optional result-bit-array)
752 (bit-vector bit-vector &optional null) *
753 :policy (>= speed space))
754 `(,',fun bit-array-1 bit-array-2
755 (make-array (length bit-array-1) :element-type 'bit)))
756 ;; If result is T, make it the first arg.
757 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
758 (bit-vector bit-vector (eql t)) *)
759 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
771 ;;; Similar for BIT-NOT, but there is only one arg...
772 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
773 (bit-vector &optional null) *
774 :policy (>= speed space))
775 '(bit-not bit-array-1
776 (make-array (length bit-array-1) :element-type 'bit)))
777 (deftransform bit-not ((bit-array-1 result-bit-array)
778 (bit-vector (eql t)))
779 '(bit-not bit-array-1 bit-array-1))
781 ;;; Pick off some constant cases.
782 (defoptimizer (array-header-p derive-type) ((array))
783 (let ((type (lvar-type array)))
784 (cond ((not (array-type-p type))
785 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
788 (let ((dims (array-type-dimensions type)))
789 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
791 (specifier-type 'null))
792 ((and (listp dims) (/= (length dims) 1))
793 ;; multi-dimensional array, will have a header
794 (specifier-type '(eql t)))