1.0.12.11: WITH-ARRAY-DATA bugfixes
[sbcl/simd.git] / src / compiler / array-tran.lisp
blob14eb3de5f28aed3bbf2c3db373a69efb8de7d08c
1 ;;;; array-specific optimizers and transforms
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!C")
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
18 ;;; determined.
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 (cond
32 ;; Note that this IF mightn't be satisfied even if the runtime
33 ;; value is known to be a subtype of some specialized ARRAY, because
34 ;; we can have values declared e.g. (AND SIMPLE-VECTOR UNKNOWN-TYPE),
35 ;; which are represented in the compiler as INTERSECTION-TYPE, not
36 ;; array type.
37 ((array-type-p type) (array-type-specialized-element-type type))
38 ;; fix for bug #396. This type logic corresponds to the special
39 ;; case for strings in HAIRY-DATA-VECTOR-REF
40 ;; (generic/vm-tran.lisp)
41 ((csubtypep type (specifier-type 'simple-string))
42 (cond
43 ((csubtypep type (specifier-type '(simple-array character (*))))
44 (specifier-type 'character))
45 #!+sb-unicode
46 ((csubtypep type (specifier-type '(simple-array base-char (*))))
47 (specifier-type 'base-char))
48 ((csubtypep type (specifier-type '(simple-array nil (*))))
49 *empty-type*)
50 ;; see KLUDGE below.
51 (t *wild-type*)))
53 ;; KLUDGE: there is no good answer here, but at least
54 ;; *wild-type* won't cause HAIRY-DATA-VECTOR-{REF,SET} to be
55 ;; erroneously optimized (see generic/vm-tran.lisp) -- CSR,
56 ;; 2002-08-21
57 *wild-type*))))
59 (defun extract-declared-element-type (array)
60 (let ((type (lvar-type array)))
61 (if (array-type-p type)
62 (array-type-element-type type)
63 *wild-type*)))
65 ;;; The ``new-value'' for array setters must fit in the array, and the
66 ;;; return type is going to be the same as the new-value for SETF
67 ;;; functions.
68 (defun assert-new-value-type (new-value array)
69 (let ((type (lvar-type array)))
70 (when (array-type-p type)
71 (assert-lvar-type
72 new-value
73 (array-type-specialized-element-type type)
74 (lexenv-policy (node-lexenv (lvar-dest new-value))))))
75 (lvar-type new-value))
77 (defun assert-array-complex (array)
78 (assert-lvar-type
79 array
80 (make-array-type :complexp t
81 :element-type *wild-type*)
82 (lexenv-policy (node-lexenv (lvar-dest array))))
83 nil)
85 ;;; Return true if ARG is NIL, or is a constant-lvar whose
86 ;;; value is NIL, false otherwise.
87 (defun unsupplied-or-nil (arg)
88 (declare (type (or lvar null) arg))
89 (or (not arg)
90 (and (constant-lvar-p arg)
91 (not (lvar-value arg)))))
93 ;;;; DERIVE-TYPE optimizers
95 ;;; Array operations that use a specific number of indices implicitly
96 ;;; assert that the array is of that rank.
97 (defun assert-array-rank (array rank)
98 (assert-lvar-type
99 array
100 (specifier-type `(array * ,(make-list rank :initial-element '*)))
101 (lexenv-policy (node-lexenv (lvar-dest array)))))
103 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
104 (assert-array-rank array (length indices))
105 *universal-type*)
107 (defoptimizer (aref derive-type) ((array &rest indices) node)
108 (assert-array-rank array (length indices))
109 (extract-upgraded-element-type array))
111 (defoptimizer (%aset derive-type) ((array &rest stuff))
112 (assert-array-rank array (1- (length stuff)))
113 (assert-new-value-type (car (last stuff)) array))
115 (macrolet ((define (name)
116 `(defoptimizer (,name derive-type) ((array index))
117 (extract-upgraded-element-type array))))
118 (define hairy-data-vector-ref)
119 (define hairy-data-vector-ref/check-bounds)
120 (define data-vector-ref))
122 #!+(or x86 x86-64)
123 (defoptimizer (data-vector-ref-with-offset derive-type) ((array index offset))
124 (extract-upgraded-element-type array))
126 (macrolet ((define (name)
127 `(defoptimizer (,name derive-type) ((array index new-value))
128 (assert-new-value-type new-value array))))
129 (define hairy-data-vector-set)
130 (define hairy-data-vector-set/check-bounds)
131 (define data-vector-set))
133 #!+(or x86 x86-64)
134 (defoptimizer (data-vector-set-with-offset derive-type) ((array index offset new-value))
135 (assert-new-value-type new-value array))
137 ;;; Figure out the type of the data vector if we know the argument
138 ;;; element type.
139 (defun derive-%with-array-data/mumble-type (array)
140 (let ((atype (lvar-type array)))
141 (when (array-type-p atype)
142 (specifier-type
143 `(simple-array ,(type-specifier
144 (array-type-specialized-element-type atype))
145 (*))))))
146 (defoptimizer (%with-array-data derive-type) ((array start end))
147 (derive-%with-array-data/mumble-type array))
148 (defoptimizer (%with-array-data/fp derive-type) ((array start end))
149 (derive-%with-array-data/mumble-type array))
151 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
152 (assert-array-rank array (length indices))
153 *universal-type*)
155 (defoptimizer (row-major-aref derive-type) ((array index))
156 (extract-upgraded-element-type array))
158 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
159 (assert-new-value-type new-value array))
161 (defoptimizer (make-array derive-type)
162 ((dims &key initial-element element-type initial-contents
163 adjustable fill-pointer displaced-index-offset displaced-to))
164 (let ((simple (and (unsupplied-or-nil adjustable)
165 (unsupplied-or-nil displaced-to)
166 (unsupplied-or-nil fill-pointer))))
167 (or (careful-specifier-type
168 `(,(if simple 'simple-array 'array)
169 ,(cond ((not element-type) t)
170 ((constant-lvar-p element-type)
171 (let ((ctype (careful-specifier-type
172 (lvar-value element-type))))
173 (cond
174 ((or (null ctype) (unknown-type-p ctype)) '*)
175 (t (sb!xc:upgraded-array-element-type
176 (lvar-value element-type))))))
178 '*))
179 ,(cond ((constant-lvar-p dims)
180 (let* ((val (lvar-value dims))
181 (cdims (if (listp val) val (list val))))
182 (if simple
183 cdims
184 (length cdims))))
185 ((csubtypep (lvar-type dims)
186 (specifier-type 'integer))
187 '(*))
189 '*))))
190 (specifier-type 'array))))
192 ;;; Complex array operations should assert that their array argument
193 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
194 (defoptimizer (fill-pointer derive-type) ((vector))
195 (assert-array-complex vector))
196 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
197 (declare (ignorable index))
198 (assert-array-complex vector))
200 (defoptimizer (vector-push derive-type) ((object vector))
201 (declare (ignorable object))
202 (assert-array-complex vector))
203 (defoptimizer (vector-push-extend derive-type)
204 ((object vector &optional index))
205 (declare (ignorable object index))
206 (assert-array-complex vector))
207 (defoptimizer (vector-pop derive-type) ((vector))
208 (assert-array-complex vector))
210 ;;;; constructors
212 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
213 ;;; elements.
214 (define-source-transform vector (&rest elements)
215 (let ((len (length elements))
216 (n -1))
217 (once-only ((n-vec `(make-array ,len)))
218 `(progn
219 ,@(mapcar (lambda (el)
220 (once-only ((n-val el))
221 `(locally (declare (optimize (safety 0)))
222 (setf (svref ,n-vec ,(incf n)) ,n-val))))
223 elements)
224 ,n-vec))))
226 ;;; Just convert it into a MAKE-ARRAY.
227 (deftransform make-string ((length &key
228 (element-type 'character)
229 (initial-element
230 #.*default-init-char-form*)))
231 `(the simple-string (make-array (the index length)
232 :element-type element-type
233 ,@(when initial-element
234 '(:initial-element initial-element)))))
236 (deftransform make-array ((dims &key initial-element element-type
237 adjustable fill-pointer)
238 (t &rest *))
239 (when (null initial-element)
240 (give-up-ir1-transform))
241 (let* ((eltype (cond ((not element-type) t)
242 ((not (constant-lvar-p element-type))
243 (give-up-ir1-transform
244 "ELEMENT-TYPE is not constant."))
246 (lvar-value element-type))))
247 (eltype-type (ir1-transform-specifier-type eltype))
248 (saetp (find-if (lambda (saetp)
249 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
250 sb!vm:*specialized-array-element-type-properties*))
251 (creation-form `(make-array dims
252 :element-type ',(type-specifier (sb!vm:saetp-ctype saetp))
253 ,@(when fill-pointer
254 '(:fill-pointer fill-pointer))
255 ,@(when adjustable
256 '(:adjustable adjustable)))))
258 (unless saetp
259 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
261 (cond ((and (constant-lvar-p initial-element)
262 (eql (lvar-value initial-element)
263 (sb!vm:saetp-initial-element-default saetp)))
264 creation-form)
266 ;; error checking for target, disabled on the host because
267 ;; (CTYPE-OF #\Null) is not possible.
268 #-sb-xc-host
269 (when (constant-lvar-p initial-element)
270 (let ((value (lvar-value initial-element)))
271 (cond
272 ((not (ctypep value (sb!vm:saetp-ctype saetp)))
273 ;; this case will cause an error at runtime, so we'd
274 ;; better WARN about it now.
275 (warn 'array-initial-element-mismatch
276 :format-control "~@<~S is not a ~S (which is the ~
277 ~S of ~S).~@:>"
278 :format-arguments
279 (list
280 value
281 (type-specifier (sb!vm:saetp-ctype saetp))
282 'upgraded-array-element-type
283 eltype)))
284 ((not (ctypep value eltype-type))
285 ;; this case will not cause an error at runtime, but
286 ;; it's still worth STYLE-WARNing about.
287 (compiler-style-warn "~S is not a ~S."
288 value eltype)))))
289 `(let ((array ,creation-form))
290 (multiple-value-bind (vector)
291 (%data-vector-and-index array 0)
292 (fill vector initial-element))
293 array)))))
295 ;;; The integer type restriction on the length ensures that it will be
296 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
297 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
298 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
299 ;;; kind of initialization efficiently.
300 (deftransform make-array ((length &key element-type)
301 (integer &rest *))
302 (let* ((eltype (cond ((not element-type) t)
303 ((not (constant-lvar-p element-type))
304 (give-up-ir1-transform
305 "ELEMENT-TYPE is not constant."))
307 (lvar-value element-type))))
308 (len (if (constant-lvar-p length)
309 (lvar-value length)
310 '*))
311 (eltype-type (ir1-transform-specifier-type eltype))
312 (result-type-spec
313 `(simple-array
314 ,(if (unknown-type-p eltype-type)
315 (give-up-ir1-transform
316 "ELEMENT-TYPE is an unknown type: ~S" eltype)
317 (sb!xc:upgraded-array-element-type eltype))
318 (,len)))
319 (saetp (find-if (lambda (saetp)
320 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
321 sb!vm:*specialized-array-element-type-properties*)))
322 (unless saetp
323 (give-up-ir1-transform
324 "cannot open-code creation of ~S" result-type-spec))
325 #-sb-xc-host
326 (unless (ctypep (sb!vm:saetp-initial-element-default saetp) eltype-type)
327 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
328 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
329 ;; INITIAL-ELEMENT is not supplied, the consequences of later
330 ;; reading an uninitialized element of new-array are undefined,"
331 ;; so this could be legal code as long as the user plans to
332 ;; write before he reads, and if he doesn't we're free to do
333 ;; anything we like. But in case the user doesn't know to write
334 ;; elements before he reads elements (or to read manuals before
335 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
336 ;; didn't realize this.
337 (compiler-style-warn "The default initial element ~S is not a ~S."
338 (sb!vm:saetp-initial-element-default saetp)
339 eltype))
340 (let* ((n-bits-per-element (sb!vm:saetp-n-bits saetp))
341 (typecode (sb!vm:saetp-typecode saetp))
342 (n-pad-elements (sb!vm:saetp-n-pad-elements saetp))
343 (padded-length-form (if (zerop n-pad-elements)
344 'length
345 `(+ length ,n-pad-elements)))
346 (n-words-form
347 (cond
348 ((= n-bits-per-element 0) 0)
349 ((>= n-bits-per-element sb!vm:n-word-bits)
350 `(* ,padded-length-form
351 (the fixnum ; i.e., not RATIO
352 ,(/ n-bits-per-element sb!vm:n-word-bits))))
354 (let ((n-elements-per-word (/ sb!vm:n-word-bits
355 n-bits-per-element)))
356 (declare (type index n-elements-per-word)) ; i.e., not RATIO
357 `(ceiling ,padded-length-form ,n-elements-per-word))))))
358 (values
359 `(truly-the ,result-type-spec
360 (allocate-vector ,typecode length ,n-words-form))
361 '((declare (type index length)))))))
363 ;;; The list type restriction does not ensure that the result will be a
364 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
365 ;;; and displaced-to keywords ensures that it will be simple.
367 ;;; FIXME: should we generalize this transform to non-simple (though
368 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
369 ;;; deal with those? Maybe when the DEFTRANSFORM
370 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
371 ;;; CSR, 2002-07-01
372 (deftransform make-array ((dims &key element-type)
373 (list &rest *))
374 (unless (or (null element-type) (constant-lvar-p element-type))
375 (give-up-ir1-transform
376 "The element-type is not constant; cannot open code array creation."))
377 (unless (constant-lvar-p dims)
378 (give-up-ir1-transform
379 "The dimension list is not constant; cannot open code array creation."))
380 (let ((dims (lvar-value dims)))
381 (unless (every #'integerp dims)
382 (give-up-ir1-transform
383 "The dimension list contains something other than an integer: ~S"
384 dims))
385 (if (= (length dims) 1)
386 `(make-array ',(car dims)
387 ,@(when element-type
388 '(:element-type element-type)))
389 (let* ((total-size (reduce #'* dims))
390 (rank (length dims))
391 (spec `(simple-array
392 ,(cond ((null element-type) t)
393 ((and (constant-lvar-p element-type)
394 (ir1-transform-specifier-type
395 (lvar-value element-type)))
396 (sb!xc:upgraded-array-element-type
397 (lvar-value element-type)))
398 (t '*))
399 ,(make-list rank :initial-element '*))))
400 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank)))
401 (setf (%array-fill-pointer header) ,total-size)
402 (setf (%array-fill-pointer-p header) nil)
403 (setf (%array-available-elements header) ,total-size)
404 (setf (%array-data-vector header)
405 (make-array ,total-size
406 ,@(when element-type
407 '(:element-type element-type))))
408 (setf (%array-displaced-p header) nil)
409 ,@(let ((axis -1))
410 (mapcar (lambda (dim)
411 `(setf (%array-dimension header ,(incf axis))
412 ,dim))
413 dims))
414 (truly-the ,spec header))))))
416 ;;;; miscellaneous properties of arrays
418 ;;; Transforms for various array properties. If the property is know
419 ;;; at compile time because of a type spec, use that constant value.
421 ;;; Most of this logic may end up belonging in code/late-type.lisp;
422 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
423 ;;; maybe this is just too sloppy for actual type logic. -- CSR,
424 ;;; 2004-02-18
425 (defun array-type-dimensions-or-give-up (type)
426 (typecase type
427 (array-type (array-type-dimensions type))
428 (union-type
429 (let ((types (union-type-types type)))
430 ;; there are at least two types, right?
431 (aver (> (length types) 1))
432 (let ((result (array-type-dimensions-or-give-up (car types))))
433 (dolist (type (cdr types) result)
434 (unless (equal (array-type-dimensions-or-give-up type) result)
435 (give-up-ir1-transform))))))
436 ;; FIXME: intersection type [e.g. (and (array * (*)) (satisfies foo)) ]
437 (t (give-up-ir1-transform))))
439 (defun conservative-array-type-complexp (type)
440 (typecase type
441 (array-type (array-type-complexp type))
442 (union-type
443 (let ((types (union-type-types type)))
444 (aver (> (length types) 1))
445 (let ((result (conservative-array-type-complexp (car types))))
446 (dolist (type (cdr types) result)
447 (unless (eq (conservative-array-type-complexp type) result)
448 (return-from conservative-array-type-complexp :maybe))))))
449 ;; FIXME: intersection type
450 (t :maybe)))
452 ;;; If we can tell the rank from the type info, use it instead.
453 (deftransform array-rank ((array))
454 (let ((array-type (lvar-type array)))
455 (let ((dims (array-type-dimensions-or-give-up array-type)))
456 (if (not (listp dims))
457 (give-up-ir1-transform
458 "The array rank is not known at compile time: ~S"
459 dims)
460 (length dims)))))
462 ;;; If we know the dimensions at compile time, just use it. Otherwise,
463 ;;; if we can tell that the axis is in bounds, convert to
464 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
465 ;;; (if it's simple and a vector).
466 (deftransform array-dimension ((array axis)
467 (array index))
468 (unless (constant-lvar-p axis)
469 (give-up-ir1-transform "The axis is not constant."))
470 (let ((array-type (lvar-type array))
471 (axis (lvar-value axis)))
472 (let ((dims (array-type-dimensions-or-give-up array-type)))
473 (unless (listp dims)
474 (give-up-ir1-transform
475 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
476 (unless (> (length dims) axis)
477 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
478 dims
479 axis))
480 (let ((dim (nth axis dims)))
481 (cond ((integerp dim)
482 dim)
483 ((= (length dims) 1)
484 (ecase (conservative-array-type-complexp array-type)
485 ((t)
486 '(%array-dimension array 0))
487 ((nil)
488 '(length array))
489 ((:maybe)
490 (give-up-ir1-transform
491 "can't tell whether array is simple"))))
493 '(%array-dimension array axis)))))))
495 ;;; If the length has been declared and it's simple, just return it.
496 (deftransform length ((vector)
497 ((simple-array * (*))))
498 (let ((type (lvar-type vector)))
499 (let ((dims (array-type-dimensions-or-give-up type)))
500 (unless (and (listp dims) (integerp (car dims)))
501 (give-up-ir1-transform
502 "Vector length is unknown, must call LENGTH at runtime."))
503 (car dims))))
505 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
506 ;;; simple, it will extract the length slot from the vector. It it's
507 ;;; complex, it will extract the fill pointer slot from the array
508 ;;; header.
509 (deftransform length ((vector) (vector))
510 '(vector-length vector))
512 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
513 ;;; compile-time constant.
514 (deftransform vector-length ((vector))
515 (let ((vtype (lvar-type vector)))
516 (let ((dim (first (array-type-dimensions-or-give-up vtype))))
517 (when (eq dim '*)
518 (give-up-ir1-transform))
519 (when (conservative-array-type-complexp vtype)
520 (give-up-ir1-transform))
521 dim)))
523 ;;; Again, if we can tell the results from the type, just use it.
524 ;;; Otherwise, if we know the rank, convert into a computation based
525 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
526 ;;; multiplications because we know that the total size must be an
527 ;;; INDEX.
528 (deftransform array-total-size ((array)
529 (array))
530 (let ((array-type (lvar-type array)))
531 (let ((dims (array-type-dimensions-or-give-up array-type)))
532 (unless (listp dims)
533 (give-up-ir1-transform "can't tell the rank at compile time"))
534 (if (member '* dims)
535 (do ((form 1 `(truly-the index
536 (* (array-dimension array ,i) ,form)))
537 (i 0 (1+ i)))
538 ((= i (length dims)) form))
539 (reduce #'* dims)))))
541 ;;; Only complex vectors have fill pointers.
542 (deftransform array-has-fill-pointer-p ((array))
543 (let ((array-type (lvar-type array)))
544 (let ((dims (array-type-dimensions-or-give-up array-type)))
545 (if (and (listp dims) (not (= (length dims) 1)))
547 (ecase (conservative-array-type-complexp array-type)
548 ((t)
550 ((nil)
551 nil)
552 ((:maybe)
553 (give-up-ir1-transform
554 "The array type is ambiguous; must call ~
555 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
557 ;;; Primitive used to verify indices into arrays. If we can tell at
558 ;;; compile-time or we are generating unsafe code, don't bother with
559 ;;; the VOP.
560 (deftransform %check-bound ((array dimension index) * * :node node)
561 (cond ((policy node (= insert-array-bounds-checks 0))
562 'index)
563 ((not (constant-lvar-p dimension))
564 (give-up-ir1-transform))
566 (let ((dim (lvar-value dimension)))
567 ;; FIXME: Can SPEED > SAFETY weaken this check to INTEGER?
568 `(the (integer 0 (,dim)) index)))))
570 ;;;; WITH-ARRAY-DATA
572 ;;; This checks to see whether the array is simple and the start and
573 ;;; end are in bounds. If so, it proceeds with those values.
574 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
575 ;;; may be further optimized.
577 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
578 ;;; START-VAR and END-VAR to the start and end of the designated
579 ;;; portion of the data vector. SVALUE and EVALUE are any start and
580 ;;; end specified to the original operation, and are factored into the
581 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
582 ;;; offset of all displacements encountered, and does not include
583 ;;; SVALUE.
585 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
586 ;;; forced to be inline, overriding the ordinary judgment of the
587 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
588 ;;; fairly picky about their arguments, figuring that if you haven't
589 ;;; bothered to get all your ducks in a row, you probably don't care
590 ;;; that much about speed anyway! But in some cases it makes sense to
591 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
592 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
593 ;;; sense to use FORCE-INLINE option in that case.
594 (def!macro with-array-data (((data-var array &key offset-var)
595 (start-var &optional (svalue 0))
596 (end-var &optional (evalue nil))
597 &key force-inline check-fill-pointer)
598 &body forms
599 &environment env)
600 (once-only ((n-array array)
601 (n-svalue `(the index ,svalue))
602 (n-evalue `(the (or index null) ,evalue)))
603 (let ((check-bounds (policy env (plusp insert-array-bounds-checks))))
604 `(multiple-value-bind (,data-var
605 ,start-var
606 ,end-var
607 ,@(when offset-var `(,offset-var)))
608 (if (not (array-header-p ,n-array))
609 (let ((,n-array ,n-array))
610 (declare (type (simple-array * (*)) ,n-array))
611 ,(once-only ((n-len (if check-fill-pointer
612 `(length ,n-array)
613 `(array-total-size ,n-array)))
614 (n-end `(or ,n-evalue ,n-len)))
615 (if check-bounds
616 `(if (<= 0 ,n-svalue ,n-end ,n-len)
617 (values ,n-array ,n-svalue ,n-end 0)
618 ,(if check-fill-pointer
619 `(sequence-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)
620 `(array-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)))
621 `(values ,n-array ,n-svalue ,n-end 0))))
622 ,(if force-inline
623 `(%with-array-data-macro ,n-array ,n-svalue ,n-evalue
624 :check-bounds ,check-bounds
625 :check-fill-pointer ,check-fill-pointer)
626 (if check-fill-pointer
627 `(%with-array-data/fp ,n-array ,n-svalue ,n-evalue)
628 `(%with-array-data ,n-array ,n-svalue ,n-evalue))))
629 ,@forms))))
631 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
632 ;;; DEFTRANSFORMs and DEFUNs.
633 (def!macro %with-array-data-macro (array
634 start
636 &key
637 (element-type '*)
638 check-bounds
639 check-fill-pointer)
640 (with-unique-names (size defaulted-end data cumulative-offset)
641 `(let* ((,size ,(if check-fill-pointer
642 `(length ,array)
643 `(array-total-size ,array)))
644 (,defaulted-end (or ,end ,size)))
645 ,@(when check-bounds
646 `((unless (<= ,start ,defaulted-end ,size)
647 ,(if check-fill-pointer
648 `(sequence-bounding-indices-bad-error ,array ,start ,end)
649 `(array-bounding-indices-bad-error ,array ,start ,end)))))
650 (do ((,data ,array (%array-data-vector ,data))
651 (,cumulative-offset 0
652 (+ ,cumulative-offset
653 (%array-displacement ,data))))
654 ((not (array-header-p ,data))
655 (values (the (simple-array ,element-type 1) ,data)
656 (the index (+ ,cumulative-offset ,start))
657 (the index (+ ,cumulative-offset ,defaulted-end))
658 (the index ,cumulative-offset)))
659 (declare (type index ,cumulative-offset))))))
661 (defun transform-%with-array-data/muble (array node check-fill-pointer)
662 (let ((element-type (upgraded-element-type-specifier-or-give-up array))
663 (type (lvar-type array))
664 (check-bounds (policy node (plusp insert-array-bounds-checks))))
665 (if (and (array-type-p type)
666 (not (array-type-complexp type))
667 (listp (array-type-dimensions type))
668 (not (null (cdr (array-type-dimensions type)))))
669 ;; If it's a simple multidimensional array, then just return
670 ;; its data vector directly rather than going through
671 ;; %WITH-ARRAY-DATA-MACRO. SBCL doesn't generally generate
672 ;; code that would use this currently, but we have encouraged
673 ;; users to use WITH-ARRAY-DATA and we may use it ourselves at
674 ;; some point in the future for optimized libraries or
675 ;; similar.
676 (if check-bounds
677 `(let* ((data (truly-the (simple-array ,element-type (*))
678 (%array-data-vector array)))
679 (len (length data))
680 (real-end (or end len)))
681 (unless (<= 0 start data-end lend)
682 (sequence-bounding-indices-bad-error array start end))
683 (values data 0 real-end 0))
684 `(let ((data (truly-the (simple-array ,element-type (*))
685 (%array-data-vector array))))
686 (values data 0 (or end (length data)) 0)))
687 `(%with-array-data-macro array start end
688 :check-fill-pointer ,check-fill-pointer
689 :check-bounds ,check-bounds
690 :element-type ,element-type))))
692 ;; It might very well be reasonable to allow general ARRAY here, I
693 ;; just haven't tried to understand the performance issues involved.
694 ;; -- WHN, and also CSR 2002-05-26
695 (deftransform %with-array-data ((array start end)
696 ((or vector simple-array) index (or index null) t)
698 :node node
699 :policy (> speed space))
700 "inline non-SIMPLE-vector-handling logic"
701 (transform-%with-array-data/muble array node nil))
702 (deftransform %with-array-data/fp ((array start end)
703 ((or vector simple-array) index (or index null) t)
705 :node node
706 :policy (> speed space))
707 "inline non-SIMPLE-vector-handling logic"
708 (transform-%with-array-data/muble array node t))
710 ;;;; array accessors
712 ;;; We convert all typed array accessors into AREF and %ASET with type
713 ;;; assertions on the array.
714 (macrolet ((define-bit-frob (reffer setter simplep)
715 `(progn
716 (define-source-transform ,reffer (a &rest i)
717 `(aref (the (,',(if simplep 'simple-array 'array)
719 ,(mapcar (constantly '*) i))
720 ,a) ,@i))
721 (define-source-transform ,setter (a &rest i)
722 `(%aset (the (,',(if simplep 'simple-array 'array)
724 ,(cdr (mapcar (constantly '*) i)))
725 ,a) ,@i)))))
726 (define-bit-frob sbit %sbitset t)
727 (define-bit-frob bit %bitset nil))
728 (macrolet ((define-frob (reffer setter type)
729 `(progn
730 (define-source-transform ,reffer (a i)
731 `(aref (the ,',type ,a) ,i))
732 (define-source-transform ,setter (a i v)
733 `(%aset (the ,',type ,a) ,i ,v)))))
734 (define-frob svref %svset simple-vector)
735 (define-frob schar %scharset simple-string)
736 (define-frob char %charset string))
738 (macrolet (;; This is a handy macro for computing the row-major index
739 ;; given a set of indices. We wrap each index with a call
740 ;; to %CHECK-BOUND to ensure that everything works out
741 ;; correctly. We can wrap all the interior arithmetic with
742 ;; TRULY-THE INDEX because we know the resultant
743 ;; row-major index must be an index.
744 (with-row-major-index ((array indices index &optional new-value)
745 &rest body)
746 `(let (n-indices dims)
747 (dotimes (i (length ,indices))
748 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
749 (push (make-symbol (format nil "DIM-~D" i)) dims))
750 (setf n-indices (nreverse n-indices))
751 (setf dims (nreverse dims))
752 `(lambda (,',array ,@n-indices
753 ,@',(when new-value (list new-value)))
754 (let* (,@(let ((,index -1))
755 (mapcar (lambda (name)
756 `(,name (array-dimension
757 ,',array
758 ,(incf ,index))))
759 dims))
760 (,',index
761 ,(if (null dims)
763 (do* ((dims dims (cdr dims))
764 (indices n-indices (cdr indices))
765 (last-dim nil (car dims))
766 (form `(%check-bound ,',array
767 ,(car dims)
768 ,(car indices))
769 `(truly-the
770 index
771 (+ (truly-the index
772 (* ,form
773 ,last-dim))
774 (%check-bound
775 ,',array
776 ,(car dims)
777 ,(car indices))))))
778 ((null (cdr dims)) form)))))
779 ,',@body)))))
781 ;; Just return the index after computing it.
782 (deftransform array-row-major-index ((array &rest indices))
783 (with-row-major-index (array indices index)
784 index))
786 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
787 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
788 ;; expression for the row major index.
789 (deftransform aref ((array &rest indices))
790 (with-row-major-index (array indices index)
791 (hairy-data-vector-ref array index)))
793 (deftransform %aset ((array &rest stuff))
794 (let ((indices (butlast stuff)))
795 (with-row-major-index (array indices index new-value)
796 (hairy-data-vector-set array index new-value)))))
798 ;; For AREF of vectors we do the bounds checking in the callee. This
799 ;; lets us do a significantly more efficient check for simple-arrays
800 ;; without bloating the code. If we already know the type of the array
801 ;; with sufficient precision, skip directly to DATA-VECTOR-REF.
802 (deftransform aref ((array index) (t t) * :node node)
803 (let ((type (lvar-type array)))
804 (cond ((and (array-type-p type)
805 (null (array-type-complexp type))
806 (not (eql (extract-upgraded-element-type array)
807 *wild-type*))
808 (eql (length (array-type-dimensions type)) 1))
809 `(data-vector-ref array (%check-bound array
810 (array-dimension array 0)
811 index)))
812 ((policy node (zerop insert-array-bounds-checks))
813 `(hairy-data-vector-ref array index))
815 `(hairy-data-vector-ref/check-bounds array index)))))
817 (deftransform %aset ((array index new-value) (t t t) * :node node)
818 (if (policy node (zerop insert-array-bounds-checks))
819 `(hairy-data-vector-set array index new-value)
820 `(hairy-data-vector-set/check-bounds array index new-value)))
822 ;;; But if we find out later that there's some useful type information
823 ;;; available, switch back to the normal one to give other transforms
824 ;;; a stab at it.
825 (macrolet ((define (name transform-to extra extra-type)
826 `(deftransform ,name ((array index ,@extra))
827 (let ((type (lvar-type array))
828 (element-type (extract-upgraded-element-type array)))
829 ;; If an element type has been declared, we want to
830 ;; use that information it for type checking (even
831 ;; if the access can't be optimized due to the array
832 ;; not being simple).
833 (when (and (eql element-type *wild-type*)
834 ;; This type logic corresponds to the special
835 ;; case for strings in HAIRY-DATA-VECTOR-REF
836 ;; (generic/vm-tran.lisp)
837 (not (csubtypep type (specifier-type 'simple-string))))
838 (when (or (not (array-type-p type))
839 ;; If it's a simple array, we might be able
840 ;; to inline the access completely.
841 (not (null (array-type-complexp type))))
842 (give-up-ir1-transform
843 "Upgraded element type of array is not known at compile time."))))
844 `(,',transform-to array
845 (%check-bound array
846 (array-dimension array 0)
847 index)
848 ,@',extra))))
849 (define hairy-data-vector-ref/check-bounds
850 hairy-data-vector-ref nil nil)
851 (define hairy-data-vector-set/check-bounds
852 hairy-data-vector-set (new-value) (*)))
854 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
855 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
856 ;;; array total size.
857 (deftransform row-major-aref ((array index))
858 `(hairy-data-vector-ref array
859 (%check-bound array (array-total-size array) index)))
860 (deftransform %set-row-major-aref ((array index new-value))
861 `(hairy-data-vector-set array
862 (%check-bound array (array-total-size array) index)
863 new-value))
865 ;;;; bit-vector array operation canonicalization
866 ;;;;
867 ;;;; We convert all bit-vector operations to have the result array
868 ;;;; specified. This allows any result allocation to be open-coded,
869 ;;;; and eliminates the need for any VM-dependent transforms to handle
870 ;;;; these cases.
872 (macrolet ((def (fun)
873 `(progn
874 (deftransform ,fun ((bit-array-1 bit-array-2
875 &optional result-bit-array)
876 (bit-vector bit-vector &optional null) *
877 :policy (>= speed space))
878 `(,',fun bit-array-1 bit-array-2
879 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
880 ;; If result is T, make it the first arg.
881 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
882 (bit-vector bit-vector (eql t)) *)
883 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
884 (def bit-and)
885 (def bit-ior)
886 (def bit-xor)
887 (def bit-eqv)
888 (def bit-nand)
889 (def bit-nor)
890 (def bit-andc1)
891 (def bit-andc2)
892 (def bit-orc1)
893 (def bit-orc2))
895 ;;; Similar for BIT-NOT, but there is only one arg...
896 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
897 (bit-vector &optional null) *
898 :policy (>= speed space))
899 '(bit-not bit-array-1
900 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
901 (deftransform bit-not ((bit-array-1 result-bit-array)
902 (bit-vector (eql t)))
903 '(bit-not bit-array-1 bit-array-1))
905 ;;; Pick off some constant cases.
906 (defoptimizer (array-header-p derive-type) ((array))
907 (let ((type (lvar-type array)))
908 (cond ((not (array-type-p type))
909 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
910 nil)
912 (let ((dims (array-type-dimensions type)))
913 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
914 ;; no array header
915 (specifier-type 'null))
916 ((and (listp dims) (/= (length dims) 1))
917 ;; multi-dimensional array, will have a header
918 (specifier-type '(eql t)))
919 ((eql (array-type-complexp type) t)
920 (specifier-type '(eql t)))
922 nil)))))))