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 ;;;; DERIVE-TYPE optimizers
16 ;;; Array operations that use a specific number of indices implicitly
17 ;;; assert that the array is of that rank.
18 (defun assert-array-rank (array rank)
19 (assert-continuation-type
21 (specifier-type `(array * ,(make-list rank :initial-element '*)))))
23 ;;; Array access functions return an object from the array, hence its
24 ;;; type will be asserted to be array element type.
25 (defun extract-element-type (array)
26 (let ((type (continuation-type array)))
27 (if (array-type-p type)
28 (array-type-element-type type)
31 ;;; Array access functions return an object from the array, hence its
32 ;;; type is going to be the array upgraded element type.
33 (defun extract-upgraded-element-type (array)
34 (let ((type (continuation-type array)))
35 (if (array-type-p type)
36 (array-type-specialized-element-type type)
39 ;;; The ``new-value'' for array setters must fit in the array, and the
40 ;;; return type is going to be the same as the new-value for SETF
42 (defun assert-new-value-type (new-value array)
43 (let ((type (continuation-type array)))
44 (when (array-type-p type)
45 (assert-continuation-type new-value (array-type-element-type type))))
46 (continuation-type new-value))
48 ;;; Return true if Arg is NIL, or is a constant-continuation whose
49 ;;; value is NIL, false otherwise.
50 (defun unsupplied-or-nil (arg)
51 (declare (type (or continuation null) arg))
53 (and (constant-continuation-p arg)
54 (not (continuation-value arg)))))
56 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
57 (assert-array-rank array (length indices))
60 (defoptimizer (aref derive-type) ((array &rest indices) node)
61 (assert-array-rank array (length indices))
62 ;; If the node continuation has a single use then assert its type.
63 (let ((cont (node-cont node)))
64 (when (= (length (find-uses cont)) 1)
65 (assert-continuation-type cont (extract-element-type array))))
66 (extract-upgraded-element-type array))
68 (defoptimizer (%aset derive-type) ((array &rest stuff))
69 (assert-array-rank array (1- (length stuff)))
70 (assert-new-value-type (car (last stuff)) array))
72 (defoptimizer (hairy-data-vector-ref derive-type) ((array index))
73 (extract-upgraded-element-type array))
74 (defoptimizer (data-vector-ref derive-type) ((array index))
75 (extract-upgraded-element-type array))
77 (defoptimizer (data-vector-set derive-type) ((array index new-value))
78 (assert-new-value-type new-value array))
79 (defoptimizer (hairy-data-vector-set derive-type) ((array index new-value))
80 (assert-new-value-type new-value array))
82 ;;; Figure out the type of the data vector if we know the argument
84 (defoptimizer (%with-array-data derive-type) ((array start end))
85 (let ((atype (continuation-type array)))
86 (when (array-type-p atype)
87 (values-specifier-type
88 `(values (simple-array ,(type-specifier
89 (array-type-element-type atype))
91 index index index)))))
93 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
94 (assert-array-rank array (length indices))
97 (defoptimizer (row-major-aref derive-type) ((array index))
98 (extract-upgraded-element-type array))
100 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
101 (assert-new-value-type new-value array))
103 (defoptimizer (make-array derive-type)
104 ((dims &key initial-element element-type initial-contents
105 adjustable fill-pointer displaced-index-offset displaced-to))
106 (let ((simple (and (unsupplied-or-nil adjustable)
107 (unsupplied-or-nil displaced-to)
108 (unsupplied-or-nil fill-pointer))))
110 `(,(if simple 'simple-array 'array)
111 ,(cond ((not element-type) t)
112 ((constant-continuation-p element-type)
113 (continuation-value element-type))
118 ((constant-continuation-p dims)
119 (let ((val (continuation-value dims)))
120 (if (listp val) val (list val))))
121 ((csubtypep (continuation-type dims)
122 (specifier-type 'integer))
129 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
131 (def-source-transform vector (&rest elements)
134 (let ((len (length elements))
136 (once-only ((n-vec `(make-array ,len)))
138 ,@(mapcar #'(lambda (el)
139 (once-only ((n-val el))
140 `(locally (declare (optimize (safety 0)))
141 (setf (svref ,n-vec ,(incf n))
146 ;;; Just convert it into a MAKE-ARRAY.
147 (def-source-transform make-string (length &key
148 (element-type ''base-char)
149 (initial-element default-init-char))
152 `(make-array (the index ,length)
153 :element-type ,element-type
154 :initial-element ,initial-element)))
156 (defparameter *array-info*
157 #((base-char #.default-init-char 8 sb!vm:simple-string-type)
158 (single-float 0.0s0 32 sb!vm:simple-array-single-float-type)
159 (double-float 0.0d0 64 sb!vm:simple-array-double-float-type)
160 #!+long-float (long-float 0.0l0 #!+x86 96 #!+sparc 128
161 sb!vm:simple-array-long-float-type)
162 (bit 0 1 sb!vm:simple-bit-vector-type)
163 ((unsigned-byte 2) 0 2 sb!vm:simple-array-unsigned-byte-2-type)
164 ((unsigned-byte 4) 0 4 sb!vm:simple-array-unsigned-byte-4-type)
165 ((unsigned-byte 8) 0 8 sb!vm:simple-array-unsigned-byte-8-type)
166 ((unsigned-byte 16) 0 16 sb!vm:simple-array-unsigned-byte-16-type)
167 ((unsigned-byte 32) 0 32 sb!vm:simple-array-unsigned-byte-32-type)
168 ((signed-byte 8) 0 8 sb!vm:simple-array-signed-byte-8-type)
169 ((signed-byte 16) 0 16 sb!vm:simple-array-signed-byte-16-type)
170 ((signed-byte 30) 0 32 sb!vm:simple-array-signed-byte-30-type)
171 ((signed-byte 32) 0 32 sb!vm:simple-array-signed-byte-32-type)
172 ((complex single-float) #C(0.0s0 0.0s0) 64
173 sb!vm:simple-array-complex-single-float-type)
174 ((complex double-float) #C(0.0d0 0.0d0) 128
175 sb!vm:simple-array-complex-double-float-type)
177 ((complex long-float) #C(0.0l0 0.0l0) #!+x86 192 #!+sparc 256
178 sb!vm:simple-array-complex-long-float-type)
179 (t 0 32 sb!vm:simple-vector-type)))
181 ;;; The integer type restriction on the length ensures that it will be
182 ;;; a vector. The lack of adjustable, fill-pointer, and displaced-to
183 ;;; keywords ensures that it will be simple.
184 (deftransform make-array ((length &key initial-element element-type)
186 (let* ((eltype (cond ((not element-type) t)
187 ((not (constant-continuation-p element-type))
188 (give-up-ir1-transform
189 "ELEMENT-TYPE is not constant."))
191 (continuation-value element-type))))
192 (len (if (constant-continuation-p length)
193 (continuation-value length)
195 (spec `(simple-array ,eltype (,len)))
196 (eltype-type (specifier-type eltype)))
197 (multiple-value-bind (default-initial-element element-size typecode)
198 (dovector (info *array-info*
199 (give-up-ir1-transform
200 "cannot open-code creation of ~S" spec))
201 (when (csubtypep eltype-type (specifier-type (car info)))
202 (return (values-list (cdr info)))))
204 (if (>= element-size sb!vm:word-bits)
205 `(* length ,(/ element-size sb!vm:word-bits))
206 (let ((elements-per-word (/ 32 element-size)))
208 ,(if (eq 'sb!vm:simple-string-type typecode)
209 ;; (Simple strings are stored with an
210 ;; extra trailing null for convenience
211 ;; in calling out to C.)
213 (1- elements-per-word)))
214 ,elements-per-word))))
217 (allocate-vector ,typecode length ,nwords-form))))
219 (cond ((and default-initial-element
220 (or (null initial-element)
221 (and (constant-continuation-p initial-element)
222 (eql (continuation-value initial-element)
223 default-initial-element))))
224 (unless (csubtypep (ctype-of default-initial-element)
226 ;; This situation arises e.g. in
227 ;; (MAKE-ARRAY 4 :ELEMENT-TYPE '(INTEGER 1 5))
228 ;; ANSI's definition of MAKE-ARRAY says "If
229 ;; INITIAL-ELEMENT is not supplied, the consequences
230 ;; of later reading an uninitialized element of
231 ;; new-array are undefined," so this could be legal
232 ;; code as long as the user plans to write before he
233 ;; reads, and if he doesn't we're free to do
234 ;; anything we like. But in case the user doesn't
235 ;; know to write before he reads, we'll signal a
236 ;; STYLE-WARNING in case he didn't realize this.
238 ;; FIXME: should be STYLE-WARNING, not note
239 (compiler-note "The default initial element ~S is not a ~S."
240 default-initial-element
244 `(truly-the ,spec (fill ,constructor initial-element))))
245 '((declare (type index length))))))))
247 ;;; The list type restriction does not ensure that the result will be a
248 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
249 ;;; and displaced-to keywords ensures that it will be simple.
250 (deftransform make-array ((dims &key initial-element element-type)
252 (unless (or (null element-type) (constant-continuation-p element-type))
253 (give-up-ir1-transform
254 "The element-type is not constant; cannot open code array creation."))
255 (unless (constant-continuation-p dims)
256 (give-up-ir1-transform
257 "The dimension list is not constant; cannot open code array creation."))
258 (let ((dims (continuation-value dims)))
259 (unless (every #'integerp dims)
260 (give-up-ir1-transform
261 "The dimension list contains something other than an integer: ~S"
263 (if (= (length dims) 1)
264 `(make-array ',(car dims)
265 ,@(when initial-element
266 '(:initial-element initial-element))
268 '(:element-type element-type)))
269 (let* ((total-size (reduce #'* dims))
272 ,(cond ((null element-type) t)
273 ((constant-continuation-p element-type)
274 (continuation-value element-type))
276 ,(make-list rank :initial-element '*))))
277 `(let ((header (make-array-header sb!vm:simple-array-type ,rank)))
278 (setf (%array-fill-pointer header) ,total-size)
279 (setf (%array-fill-pointer-p header) nil)
280 (setf (%array-available-elements header) ,total-size)
281 (setf (%array-data-vector header)
282 (make-array ,total-size
284 '(:element-type element-type))
285 ,@(when initial-element
286 '(:initial-element initial-element))))
287 (setf (%array-displaced-p header) nil)
289 (mapcar #'(lambda (dim)
290 `(setf (%array-dimension header ,(incf axis))
293 (truly-the ,spec header))))))
295 ;;;; miscellaneous properties of arrays
297 ;;; Transforms for various array properties. If the property is know
298 ;;; at compile time because of a type spec, use that constant value.
300 ;;; If we can tell the rank from the type info, use it instead.
301 (deftransform array-rank ((array))
302 (let ((array-type (continuation-type array)))
303 (unless (array-type-p array-type)
304 (give-up-ir1-transform))
305 (let ((dims (array-type-dimensions array-type)))
306 (if (not (listp dims))
307 (give-up-ir1-transform
308 "The array rank is not known at compile time: ~S"
312 ;;; If we know the dimensions at compile time, just use it. Otherwise,
313 ;;; if we can tell that the axis is in bounds, convert to
314 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
315 ;;; (if it's simple and a vector).
316 (deftransform array-dimension ((array axis)
318 (unless (constant-continuation-p axis)
319 (give-up-ir1-transform "The axis is not constant."))
320 (let ((array-type (continuation-type array))
321 (axis (continuation-value axis)))
322 (unless (array-type-p array-type)
323 (give-up-ir1-transform))
324 (let ((dims (array-type-dimensions array-type)))
326 (give-up-ir1-transform
327 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
328 (unless (> (length dims) axis)
329 (abort-ir1-transform "The array has dimensions ~S, ~D is too large."
332 (let ((dim (nth axis dims)))
333 (cond ((integerp dim)
336 (ecase (array-type-complexp array-type)
338 '(%array-dimension array 0))
342 (give-up-ir1-transform
343 "can't tell whether array is simple"))))
345 '(%array-dimension array axis)))))))
347 ;;; If the length has been declared and it's simple, just return it.
348 (deftransform length ((vector)
349 ((simple-array * (*))))
350 (let ((type (continuation-type vector)))
351 (unless (array-type-p type)
352 (give-up-ir1-transform))
353 (let ((dims (array-type-dimensions type)))
354 (unless (and (listp dims) (integerp (car dims)))
355 (give-up-ir1-transform
356 "Vector length is unknown, must call LENGTH at runtime."))
359 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
360 ;;; simple, it will extract the length slot from the vector. It it's
361 ;;; complex, it will extract the fill pointer slot from the array
363 (deftransform length ((vector) (vector))
364 '(vector-length vector))
366 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
367 ;;; compile-time constant.
368 (deftransform vector-length ((vector) ((simple-array * (*))))
369 (let ((vtype (continuation-type vector)))
370 (if (array-type-p vtype)
371 (let ((dim (first (array-type-dimensions vtype))))
372 (when (eq dim '*) (give-up-ir1-transform))
374 (give-up-ir1-transform))))
376 ;;; Again, if we can tell the results from the type, just use it.
377 ;;; Otherwise, if we know the rank, convert into a computation based
378 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
379 ;;; multiplications because we know that the total size must be an
381 (deftransform array-total-size ((array)
383 (let ((array-type (continuation-type array)))
384 (unless (array-type-p array-type)
385 (give-up-ir1-transform))
386 (let ((dims (array-type-dimensions array-type)))
388 (give-up-ir1-transform "can't tell the rank at compile time"))
390 (do ((form 1 `(truly-the index
391 (* (array-dimension array ,i) ,form)))
393 ((= i (length dims)) form))
394 (reduce #'* dims)))))
396 ;;; Only complex vectors have fill pointers.
397 (deftransform array-has-fill-pointer-p ((array))
398 (let ((array-type (continuation-type array)))
399 (unless (array-type-p array-type)
400 (give-up-ir1-transform))
401 (let ((dims (array-type-dimensions array-type)))
402 (if (and (listp dims) (not (= (length dims) 1)))
404 (ecase (array-type-complexp array-type)
410 (give-up-ir1-transform
411 "The array type is ambiguous; must call ~
412 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
414 ;;; Primitive used to verify indices into arrays. If we can tell at
415 ;;; compile-time or we are generating unsafe code, don't bother with
417 (deftransform %check-bound ((array dimension index))
418 (unless (constant-continuation-p dimension)
419 (give-up-ir1-transform))
420 (let ((dim (continuation-value dimension)))
421 `(the (integer 0 ,dim) index)))
422 (deftransform %check-bound ((array dimension index) * *
423 :policy (and (> speed safety) (= safety 0)))
428 ;;; FIXME: This was commented out in sbcl-0.6.9.21 since it was
429 ;;; causing a problem in a CHAR form in HEXSTR. It's still important
430 ;;; to be able to inline this, so something along these lines
431 ;;; will probably be back, but it might be different in detail, e.g.
432 ;;; (DECLAIM (MAYBE-INLINE %WITH-ARRAY-DATA)).
434 ;;; Handle the 1-dimensional case of %WITH-ARRAY-DATA specially. It's
435 ;;; important to do this efficiently if we want people to be able to
436 ;;; use vectors with fill pointers anywhere near inner loops, and
437 ;;; hence it's important to do this efficiently if we want people to
438 ;;; be able to use sequence functions anywhere near inner loops.
439 (deftransform %with-array-data ((array start end)
440 (vector index (or index null))
444 :policy (> speed space))
445 "avoid full call to %WITH-ARRAY-DATA at runtime"
446 (let* ((element-ctype (extract-upgraded-element-type array))
447 (element-type-specifier (type-specifier element-ctype))
448 (simple-array-type `(simple-array ,element-type-specifier 1)))
449 (declare (type ctype element-ctype))
450 `(let* (;; FIXME: Instead of doing this hairy expression for SIZE,
451 ;; it should just be (ARRAY-DIMENSION ARRAY 0), and there
452 ;; should be a DEFTRANSFORM for ARRAY-DIMENSION which
454 (size (if (array-header-p array)
455 (%array-dimension array 0)
456 (length (the ,simple-array-type array))))
458 (if (or ,(policy node (= safety 0))
459 (<= (the index end) size))
461 (vector-data-start-out-of-range))
463 (declare (type index end))
464 (unless (or ,(policy node (= safety 0))
466 (vector-data-end-out-of-range))
467 (do (;; cumulative displacement
468 (d 0 (truly-the index (+ d (%array-displacement array))))
469 ;; eventually becomes bare data vector
470 (v array (%array-data-vector v)))
471 ((not (array-header-p v))
472 (values (the ,simple-array-type v)
473 (truly-the index (+ d start))
474 (truly-the index (+ d end))
476 (declare (type index d))))))
477 (defun vector-data-start-out-of-range ()
478 (error "The start of vector data was out of range."))
479 (defun vector-data-end-out-of-range ()
480 (error "The end of vector data was out of range."))
483 ;;; We convert all typed array accessors into AREF and %ASET with type
484 ;;; assertions on the array.
485 (macrolet ((define-frob (reffer setter type)
487 (def-source-transform ,reffer (a &rest i)
490 `(aref (the ,',type ,a) ,@i)))
491 (def-source-transform ,setter (a &rest i)
494 `(%aset (the ,',type ,a) ,@i))))))
495 (define-frob svref %svset simple-vector)
496 (define-frob schar %scharset simple-string)
497 (define-frob char %charset string)
498 (define-frob sbit %sbitset (simple-array bit))
499 (define-frob bit %bitset (array bit)))
501 (macrolet (;; This is a handy macro for computing the row-major index
502 ;; given a set of indices. We wrap each index with a call
503 ;; to %CHECK-BOUND to ensure that everything works out
504 ;; correctly. We can wrap all the interior arithmetic with
505 ;; TRULY-THE INDEX because we know the the resultant
506 ;; row-major index must be an index.
507 (with-row-major-index ((array indices index &optional new-value)
509 `(let (n-indices dims)
510 (dotimes (i (length ,indices))
511 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
512 (push (make-symbol (format nil "DIM-~D" i)) dims))
513 (setf n-indices (nreverse n-indices))
514 (setf dims (nreverse dims))
515 `(lambda (,',array ,@n-indices
516 ,@',(when new-value (list new-value)))
517 (let* (,@(let ((,index -1))
518 (mapcar #'(lambda (name)
519 `(,name (array-dimension
526 (do* ((dims dims (cdr dims))
527 (indices n-indices (cdr indices))
528 (last-dim nil (car dims))
529 (form `(%check-bound ,',array
541 ((null (cdr dims)) form)))))
544 ;; Just return the index after computing it.
545 (deftransform array-row-major-index ((array &rest indices))
546 (with-row-major-index (array indices index)
549 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
550 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
551 ;; expression for the row major index.
552 (deftransform aref ((array &rest indices))
553 (with-row-major-index (array indices index)
554 (hairy-data-vector-ref array index)))
555 (deftransform %aset ((array &rest stuff))
556 (let ((indices (butlast stuff)))
557 (with-row-major-index (array indices index new-value)
558 (hairy-data-vector-set array index new-value)))))
560 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
561 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
562 ;;; array total size.
563 (deftransform row-major-aref ((array index))
564 `(hairy-data-vector-ref array
565 (%check-bound array (array-total-size array) index)))
566 (deftransform %set-row-major-aref ((array index new-value))
567 `(hairy-data-vector-set array
568 (%check-bound array (array-total-size array) index)
571 ;;;; bit-vector array operation canonicalization
573 ;;;; We convert all bit-vector operations to have the result array
574 ;;;; specified. This allows any result allocation to be open-coded,
575 ;;;; and eliminates the need for any VM-dependent transforms to handle
578 (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
579 bit-andc2 bit-orc1 bit-orc2))
580 ;; Make a result array if result is NIL or unsupplied.
581 (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
582 '(bit-vector bit-vector &optional null) '*
584 :policy (>= speed space))
585 `(,fun bit-array-1 bit-array-2
586 (make-array (length bit-array-1) :element-type 'bit)))
587 ;; If result is T, make it the first arg.
588 (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
589 '(bit-vector bit-vector (member t)) '*
591 `(,fun bit-array-1 bit-array-2 bit-array-1)))
593 ;;; Similar for BIT-NOT, but there is only one arg...
594 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
595 (bit-vector &optional null) *
596 :policy (>= speed space))
597 '(bit-not bit-array-1
598 (make-array (length bit-array-1) :element-type 'bit)))
599 (deftransform bit-not ((bit-array-1 result-bit-array)
600 (bit-vector (constant-argument t)))
601 '(bit-not bit-array-1 bit-array-1))
602 ;;; FIXME: What does (CONSTANT-ARGUMENT T) mean? Is it the same thing
603 ;;; as (CONSTANT-ARGUMENT (MEMBER T)), or does it mean any constant
606 ;;; Pick off some constant cases.
607 (deftransform array-header-p ((array) (array))
608 (let ((type (continuation-type array)))
609 (declare (optimize (safety 3)))
610 (unless (array-type-p type)
611 (give-up-ir1-transform))
612 (let ((dims (array-type-dimensions type)))
613 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
616 ((and (listp dims) (> (length dims) 1))
617 ;; Multi-dimensional array, will have a header.
620 (give-up-ir1-transform))))))