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 will be asserted to be array element type.
29 (defun extract-element-type (array)
30 (let ((type (continuation-type array)))
31 (if (array-type-p type)
32 (array-type-element-type type)
35 ;;; Array access functions return an object from the array, hence its
36 ;;; type is going to be the array upgraded element type.
37 (defun extract-upgraded-element-type (array)
38 (let ((type (continuation-type array)))
39 (if (array-type-p type)
40 (array-type-specialized-element-type type)
43 ;;; The ``new-value'' for array setters must fit in the array, and the
44 ;;; return type is going to be the same as the new-value for SETF
46 (defun assert-new-value-type (new-value array)
47 (let ((type (continuation-type array)))
48 (when (array-type-p type)
49 (assert-continuation-type new-value (array-type-element-type type))))
50 (continuation-type new-value))
52 ;;; Return true if Arg is NIL, or is a constant-continuation whose
53 ;;; value is NIL, false otherwise.
54 (defun unsupplied-or-nil (arg)
55 (declare (type (or continuation null) arg))
57 (and (constant-continuation-p arg)
58 (not (continuation-value arg)))))
60 ;;;; DERIVE-TYPE optimizers
62 ;;; Array operations that use a specific number of indices implicitly
63 ;;; assert that the array is of that rank.
64 (defun assert-array-rank (array rank)
65 (assert-continuation-type
67 (specifier-type `(array * ,(make-list rank :initial-element '*)))))
69 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
70 (assert-array-rank array (length indices))
73 (defoptimizer (aref derive-type) ((array &rest indices) node)
74 (assert-array-rank array (length indices))
75 ;; If the node continuation has a single use then assert its type.
76 (let ((cont (node-cont node)))
77 (when (= (length (find-uses cont)) 1)
78 (assert-continuation-type cont (extract-element-type array))))
79 (extract-upgraded-element-type array))
81 (defoptimizer (%aset derive-type) ((array &rest stuff))
82 (assert-array-rank array (1- (length stuff)))
83 (assert-new-value-type (car (last stuff)) array))
85 (defoptimizer (hairy-data-vector-ref derive-type) ((array index))
86 (extract-upgraded-element-type array))
87 (defoptimizer (data-vector-ref derive-type) ((array index))
88 (extract-upgraded-element-type array))
90 (defoptimizer (data-vector-set derive-type) ((array index new-value))
91 (assert-new-value-type new-value array))
92 (defoptimizer (hairy-data-vector-set derive-type) ((array index new-value))
93 (assert-new-value-type new-value array))
95 ;;; Figure out the type of the data vector if we know the argument
97 (defoptimizer (%with-array-data derive-type) ((array start end))
98 (let ((atype (continuation-type array)))
99 (when (array-type-p atype)
100 (values-specifier-type
101 `(values (simple-array ,(type-specifier
102 (array-type-element-type atype))
104 index index index)))))
106 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
107 (assert-array-rank array (length indices))
110 (defoptimizer (row-major-aref derive-type) ((array index))
111 (extract-upgraded-element-type array))
113 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
114 (assert-new-value-type new-value array))
116 (defoptimizer (make-array derive-type)
117 ((dims &key initial-element element-type initial-contents
118 adjustable fill-pointer displaced-index-offset displaced-to))
119 (let ((simple (and (unsupplied-or-nil adjustable)
120 (unsupplied-or-nil displaced-to)
121 (unsupplied-or-nil fill-pointer))))
123 `(,(if simple 'simple-array 'array)
124 ,(cond ((not element-type) t)
125 ((constant-continuation-p element-type)
126 (continuation-value element-type))
131 ((constant-continuation-p dims)
132 (let ((val (continuation-value dims)))
133 (if (listp val) val (list val))))
134 ((csubtypep (continuation-type dims)
135 (specifier-type 'integer))
142 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
144 (def-source-transform vector (&rest elements)
147 (let ((len (length elements))
149 (once-only ((n-vec `(make-array ,len)))
151 ,@(mapcar #'(lambda (el)
152 (once-only ((n-val el))
153 `(locally (declare (optimize (safety 0)))
154 (setf (svref ,n-vec ,(incf n))
159 ;;; Just convert it into a MAKE-ARRAY.
160 (def-source-transform make-string (length &key
161 (element-type ''base-char)
162 (initial-element default-init-char))
165 `(make-array (the index ,length)
166 :element-type ,element-type
167 :initial-element ,initial-element)))
169 (defparameter *array-info*
170 #((base-char #.default-init-char 8 sb!vm:simple-string-type)
171 (single-float 0.0s0 32 sb!vm:simple-array-single-float-type)
172 (double-float 0.0d0 64 sb!vm:simple-array-double-float-type)
173 #!+long-float (long-float 0.0l0 #!+x86 96 #!+sparc 128
174 sb!vm:simple-array-long-float-type)
175 (bit 0 1 sb!vm:simple-bit-vector-type)
176 ((unsigned-byte 2) 0 2 sb!vm:simple-array-unsigned-byte-2-type)
177 ((unsigned-byte 4) 0 4 sb!vm:simple-array-unsigned-byte-4-type)
178 ((unsigned-byte 8) 0 8 sb!vm:simple-array-unsigned-byte-8-type)
179 ((unsigned-byte 16) 0 16 sb!vm:simple-array-unsigned-byte-16-type)
180 ((unsigned-byte 32) 0 32 sb!vm:simple-array-unsigned-byte-32-type)
181 ((signed-byte 8) 0 8 sb!vm:simple-array-signed-byte-8-type)
182 ((signed-byte 16) 0 16 sb!vm:simple-array-signed-byte-16-type)
183 ((signed-byte 30) 0 32 sb!vm:simple-array-signed-byte-30-type)
184 ((signed-byte 32) 0 32 sb!vm:simple-array-signed-byte-32-type)
185 ((complex single-float) #C(0.0s0 0.0s0) 64
186 sb!vm:simple-array-complex-single-float-type)
187 ((complex double-float) #C(0.0d0 0.0d0) 128
188 sb!vm:simple-array-complex-double-float-type)
190 ((complex long-float) #C(0.0l0 0.0l0) #!+x86 192 #!+sparc 256
191 sb!vm:simple-array-complex-long-float-type)
192 (t 0 32 sb!vm:simple-vector-type)))
194 ;;; The integer type restriction on the length ensures that it will be
195 ;;; a vector. The lack of adjustable, fill-pointer, and displaced-to
196 ;;; keywords ensures that it will be simple.
197 (deftransform make-array ((length &key initial-element element-type)
199 (let* ((eltype (cond ((not element-type) t)
200 ((not (constant-continuation-p element-type))
201 (give-up-ir1-transform
202 "ELEMENT-TYPE is not constant."))
204 (continuation-value element-type))))
205 (len (if (constant-continuation-p length)
206 (continuation-value length)
208 (spec `(simple-array ,eltype (,len)))
209 (eltype-type (specifier-type eltype)))
210 (multiple-value-bind (default-initial-element element-size typecode)
211 (dovector (info *array-info*
212 (give-up-ir1-transform
213 "cannot open-code creation of ~S" spec))
214 (when (csubtypep eltype-type (specifier-type (car info)))
215 (return (values-list (cdr info)))))
217 (if (>= element-size sb!vm:word-bits)
218 `(* length ,(/ element-size sb!vm:word-bits))
219 (let ((elements-per-word (/ 32 element-size)))
221 ,(if (eq 'sb!vm:simple-string-type typecode)
222 ;; (Simple strings are stored with an
223 ;; extra trailing null for convenience
224 ;; in calling out to C.)
226 (1- elements-per-word)))
227 ,elements-per-word))))
230 (allocate-vector ,typecode length ,nwords-form))))
232 (cond ((and default-initial-element
233 (or (null initial-element)
234 (and (constant-continuation-p initial-element)
235 (eql (continuation-value initial-element)
236 default-initial-element))))
237 (unless (csubtypep (ctype-of default-initial-element)
239 ;; This situation arises e.g. in
240 ;; (MAKE-ARRAY 4 :ELEMENT-TYPE '(INTEGER 1 5))
241 ;; ANSI's definition of MAKE-ARRAY says "If
242 ;; INITIAL-ELEMENT is not supplied, the consequences
243 ;; of later reading an uninitialized element of
244 ;; new-array are undefined," so this could be legal
245 ;; code as long as the user plans to write before he
246 ;; reads, and if he doesn't we're free to do
247 ;; anything we like. But in case the user doesn't
248 ;; know to write before he reads, we'll signal a
249 ;; STYLE-WARNING in case he didn't realize this.
251 ;; FIXME: should be STYLE-WARNING, not note
252 (compiler-note "The default initial element ~S is not a ~S."
253 default-initial-element
257 `(truly-the ,spec (fill ,constructor initial-element))))
258 '((declare (type index length))))))))
260 ;;; The list type restriction does not ensure that the result will be a
261 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
262 ;;; and displaced-to keywords ensures that it will be simple.
263 (deftransform make-array ((dims &key initial-element element-type)
265 (unless (or (null element-type) (constant-continuation-p element-type))
266 (give-up-ir1-transform
267 "The element-type is not constant; cannot open code array creation."))
268 (unless (constant-continuation-p dims)
269 (give-up-ir1-transform
270 "The dimension list is not constant; cannot open code array creation."))
271 (let ((dims (continuation-value dims)))
272 (unless (every #'integerp dims)
273 (give-up-ir1-transform
274 "The dimension list contains something other than an integer: ~S"
276 (if (= (length dims) 1)
277 `(make-array ',(car dims)
278 ,@(when initial-element
279 '(:initial-element initial-element))
281 '(:element-type element-type)))
282 (let* ((total-size (reduce #'* dims))
285 ,(cond ((null element-type) t)
286 ((constant-continuation-p element-type)
287 (continuation-value element-type))
289 ,(make-list rank :initial-element '*))))
290 `(let ((header (make-array-header sb!vm:simple-array-type ,rank)))
291 (setf (%array-fill-pointer header) ,total-size)
292 (setf (%array-fill-pointer-p header) nil)
293 (setf (%array-available-elements header) ,total-size)
294 (setf (%array-data-vector header)
295 (make-array ,total-size
297 '(:element-type element-type))
298 ,@(when initial-element
299 '(:initial-element initial-element))))
300 (setf (%array-displaced-p header) nil)
302 (mapcar #'(lambda (dim)
303 `(setf (%array-dimension header ,(incf axis))
306 (truly-the ,spec header))))))
308 ;;;; miscellaneous properties of arrays
310 ;;; Transforms for various array properties. If the property is know
311 ;;; at compile time because of a type spec, use that constant value.
313 ;;; If we can tell the rank from the type info, use it instead.
314 (deftransform array-rank ((array))
315 (let ((array-type (continuation-type array)))
316 (unless (array-type-p array-type)
317 (give-up-ir1-transform))
318 (let ((dims (array-type-dimensions array-type)))
319 (if (not (listp dims))
320 (give-up-ir1-transform
321 "The array rank is not known at compile time: ~S"
325 ;;; If we know the dimensions at compile time, just use it. Otherwise,
326 ;;; if we can tell that the axis is in bounds, convert to
327 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
328 ;;; (if it's simple and a vector).
329 (deftransform array-dimension ((array axis)
331 (unless (constant-continuation-p axis)
332 (give-up-ir1-transform "The axis is not constant."))
333 (let ((array-type (continuation-type array))
334 (axis (continuation-value axis)))
335 (unless (array-type-p array-type)
336 (give-up-ir1-transform))
337 (let ((dims (array-type-dimensions array-type)))
339 (give-up-ir1-transform
340 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
341 (unless (> (length dims) axis)
342 (abort-ir1-transform "The array has dimensions ~S, ~D is too large."
345 (let ((dim (nth axis dims)))
346 (cond ((integerp dim)
349 (ecase (array-type-complexp array-type)
351 '(%array-dimension array 0))
355 (give-up-ir1-transform
356 "can't tell whether array is simple"))))
358 '(%array-dimension array axis)))))))
360 ;;; If the length has been declared and it's simple, just return it.
361 (deftransform length ((vector)
362 ((simple-array * (*))))
363 (let ((type (continuation-type vector)))
364 (unless (array-type-p type)
365 (give-up-ir1-transform))
366 (let ((dims (array-type-dimensions type)))
367 (unless (and (listp dims) (integerp (car dims)))
368 (give-up-ir1-transform
369 "Vector length is unknown, must call LENGTH at runtime."))
372 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
373 ;;; simple, it will extract the length slot from the vector. It it's
374 ;;; complex, it will extract the fill pointer slot from the array
376 (deftransform length ((vector) (vector))
377 '(vector-length vector))
379 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
380 ;;; compile-time constant.
381 (deftransform vector-length ((vector) ((simple-array * (*))))
382 (let ((vtype (continuation-type vector)))
383 (if (array-type-p vtype)
384 (let ((dim (first (array-type-dimensions vtype))))
385 (when (eq dim '*) (give-up-ir1-transform))
387 (give-up-ir1-transform))))
389 ;;; Again, if we can tell the results from the type, just use it.
390 ;;; Otherwise, if we know the rank, convert into a computation based
391 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
392 ;;; multiplications because we know that the total size must be an
394 (deftransform array-total-size ((array)
396 (let ((array-type (continuation-type array)))
397 (unless (array-type-p array-type)
398 (give-up-ir1-transform))
399 (let ((dims (array-type-dimensions array-type)))
401 (give-up-ir1-transform "can't tell the rank at compile time"))
403 (do ((form 1 `(truly-the index
404 (* (array-dimension array ,i) ,form)))
406 ((= i (length dims)) form))
407 (reduce #'* dims)))))
409 ;;; Only complex vectors have fill pointers.
410 (deftransform array-has-fill-pointer-p ((array))
411 (let ((array-type (continuation-type array)))
412 (unless (array-type-p array-type)
413 (give-up-ir1-transform))
414 (let ((dims (array-type-dimensions array-type)))
415 (if (and (listp dims) (not (= (length dims) 1)))
417 (ecase (array-type-complexp array-type)
423 (give-up-ir1-transform
424 "The array type is ambiguous; must call ~
425 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
427 ;;; Primitive used to verify indices into arrays. If we can tell at
428 ;;; compile-time or we are generating unsafe code, don't bother with
430 (deftransform %check-bound ((array dimension index))
431 (unless (constant-continuation-p dimension)
432 (give-up-ir1-transform))
433 (let ((dim (continuation-value dimension)))
434 `(the (integer 0 ,dim) index)))
435 (deftransform %check-bound ((array dimension index) * *
436 :policy (and (> speed safety) (= safety 0)))
441 ;;; FIXME: This was commented out in sbcl-0.6.9.21 since it was
442 ;;; causing a problem in a CHAR form in HEXSTR. It's still important
443 ;;; to be able to inline this, so something along these lines
444 ;;; will probably be back, but it might be different in detail, e.g.
445 ;;; (DECLAIM (MAYBE-INLINE %WITH-ARRAY-DATA)).
447 ;;; Handle the 1-dimensional case of %WITH-ARRAY-DATA specially. It's
448 ;;; important to do this efficiently if we want people to be able to
449 ;;; use vectors with fill pointers anywhere near inner loops, and
450 ;;; hence it's important to do this efficiently if we want people to
451 ;;; be able to use sequence functions anywhere near inner loops.
452 (deftransform %with-array-data ((array start end)
453 (vector index (or index null))
457 :policy (> speed space))
458 "avoid full call to %WITH-ARRAY-DATA at runtime"
459 (let* ((element-ctype (extract-upgraded-element-type array))
460 (element-type-specifier (type-specifier element-ctype))
461 (simple-array-type `(simple-array ,element-type-specifier 1)))
462 (declare (type ctype element-ctype))
463 `(let* (;; FIXME: Instead of doing this hairy expression for SIZE,
464 ;; it should just be (ARRAY-DIMENSION ARRAY 0), and there
465 ;; should be a DEFTRANSFORM for ARRAY-DIMENSION which
467 (size (if (array-header-p array)
468 (%array-dimension array 0)
469 (length (the ,simple-array-type array))))
471 (if (or ,(policy node (= safety 0))
472 (<= (the index end) size))
474 (vector-data-start-out-of-range))
476 (declare (type index end))
477 (unless (or ,(policy node (= safety 0))
479 (vector-data-end-out-of-range))
480 (do (;; cumulative displacement
481 (d 0 (truly-the index (+ d (%array-displacement array))))
482 ;; eventually becomes bare data vector
483 (v array (%array-data-vector v)))
484 ((not (array-header-p v))
485 (values (the ,simple-array-type v)
486 (truly-the index (+ d start))
487 (truly-the index (+ d end))
489 (declare (type index d))))))
490 (defun vector-data-start-out-of-range ()
491 (error "The start of vector data was out of range."))
492 (defun vector-data-end-out-of-range ()
493 (error "The end of vector data was out of range."))
496 (deftransform %with-array-data ((array start end)
497 ;; Note: This transform is limited to
498 ;; VECTOR only because I happened to
499 ;; create it in order to get sequence
500 ;; function operations to be more
501 ;; efficient. It might very well be
502 ;; reasonable to allow general ARRAY
503 ;; here, I just haven't tried to
504 ;; understand the performance issues
506 (vector index (or index null))
510 :policy (> speed space))
511 "inline non-SIMPLE-vector-handling logic"
512 (let ((element-type (upgraded-element-type-specifier-or-give-up array)))
513 `(%with-array-data-macro array start end
514 :unsafe? ,(policy node (= safety 0))
515 :element-type ,element-type)))
517 ;;; We convert all typed array accessors into AREF and %ASET with type
518 ;;; assertions on the array.
519 (macrolet ((define-frob (reffer setter type)
521 (def-source-transform ,reffer (a &rest i)
524 `(aref (the ,',type ,a) ,@i)))
525 (def-source-transform ,setter (a &rest i)
528 `(%aset (the ,',type ,a) ,@i))))))
529 (define-frob svref %svset simple-vector)
530 (define-frob schar %scharset simple-string)
531 (define-frob char %charset string)
532 (define-frob sbit %sbitset (simple-array bit))
533 (define-frob bit %bitset (array bit)))
535 (macrolet (;; This is a handy macro for computing the row-major index
536 ;; given a set of indices. We wrap each index with a call
537 ;; to %CHECK-BOUND to ensure that everything works out
538 ;; correctly. We can wrap all the interior arithmetic with
539 ;; TRULY-THE INDEX because we know the the resultant
540 ;; row-major index must be an index.
541 (with-row-major-index ((array indices index &optional new-value)
543 `(let (n-indices dims)
544 (dotimes (i (length ,indices))
545 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
546 (push (make-symbol (format nil "DIM-~D" i)) dims))
547 (setf n-indices (nreverse n-indices))
548 (setf dims (nreverse dims))
549 `(lambda (,',array ,@n-indices
550 ,@',(when new-value (list new-value)))
551 (let* (,@(let ((,index -1))
552 (mapcar #'(lambda (name)
553 `(,name (array-dimension
560 (do* ((dims dims (cdr dims))
561 (indices n-indices (cdr indices))
562 (last-dim nil (car dims))
563 (form `(%check-bound ,',array
575 ((null (cdr dims)) form)))))
578 ;; Just return the index after computing it.
579 (deftransform array-row-major-index ((array &rest indices))
580 (with-row-major-index (array indices index)
583 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
584 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
585 ;; expression for the row major index.
586 (deftransform aref ((array &rest indices))
587 (with-row-major-index (array indices index)
588 (hairy-data-vector-ref array index)))
589 (deftransform %aset ((array &rest stuff))
590 (let ((indices (butlast stuff)))
591 (with-row-major-index (array indices index new-value)
592 (hairy-data-vector-set array index new-value)))))
594 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
595 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
596 ;;; array total size.
597 (deftransform row-major-aref ((array index))
598 `(hairy-data-vector-ref array
599 (%check-bound array (array-total-size array) index)))
600 (deftransform %set-row-major-aref ((array index new-value))
601 `(hairy-data-vector-set array
602 (%check-bound array (array-total-size array) index)
605 ;;;; bit-vector array operation canonicalization
607 ;;;; We convert all bit-vector operations to have the result array
608 ;;;; specified. This allows any result allocation to be open-coded,
609 ;;;; and eliminates the need for any VM-dependent transforms to handle
612 (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
613 bit-andc2 bit-orc1 bit-orc2))
614 ;; Make a result array if result is NIL or unsupplied.
615 (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
616 '(bit-vector bit-vector &optional null) '*
618 :policy (>= speed space))
619 `(,fun bit-array-1 bit-array-2
620 (make-array (length bit-array-1) :element-type 'bit)))
621 ;; If result is T, make it the first arg.
622 (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
623 '(bit-vector bit-vector (member t)) '*
625 `(,fun bit-array-1 bit-array-2 bit-array-1)))
627 ;;; Similar for BIT-NOT, but there is only one arg...
628 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
629 (bit-vector &optional null) *
630 :policy (>= speed space))
631 '(bit-not bit-array-1
632 (make-array (length bit-array-1) :element-type 'bit)))
633 (deftransform bit-not ((bit-array-1 result-bit-array)
634 (bit-vector (constant-argument t)))
635 '(bit-not bit-array-1 bit-array-1))
636 ;;; FIXME: What does (CONSTANT-ARGUMENT T) mean? Is it the same thing
637 ;;; as (CONSTANT-ARGUMENT (MEMBER T)), or does it mean any constant
640 ;;; Pick off some constant cases.
641 (deftransform array-header-p ((array) (array))
642 (let ((type (continuation-type array)))
643 (declare (optimize (safety 3)))
644 (unless (array-type-p type)
645 (give-up-ir1-transform))
646 (let ((dims (array-type-dimensions type)))
647 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
650 ((and (listp dims) (> (length dims) 1))
651 ;; Multi-dimensional array, will have a header.
654 (give-up-ir1-transform))))))