1.0.6.4: restore buildability on non-SBCL hosts
[sbcl.git] / src / compiler / array-tran.lisp
blob0d47738a46502b66a9186a93b796680f2cb56e56
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 (defoptimizer (%with-array-data derive-type) ((array start end))
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 (*))))))
147 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
148 (assert-array-rank array (length indices))
149 *universal-type*)
151 (defoptimizer (row-major-aref derive-type) ((array index))
152 (extract-upgraded-element-type array))
154 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
155 (assert-new-value-type new-value array))
157 (defoptimizer (make-array derive-type)
158 ((dims &key initial-element element-type initial-contents
159 adjustable fill-pointer displaced-index-offset displaced-to))
160 (let ((simple (and (unsupplied-or-nil adjustable)
161 (unsupplied-or-nil displaced-to)
162 (unsupplied-or-nil fill-pointer))))
163 (or (careful-specifier-type
164 `(,(if simple 'simple-array 'array)
165 ,(cond ((not element-type) t)
166 ((constant-lvar-p element-type)
167 (let ((ctype (careful-specifier-type
168 (lvar-value element-type))))
169 (cond
170 ((or (null ctype) (unknown-type-p ctype)) '*)
171 (t (sb!xc:upgraded-array-element-type
172 (lvar-value element-type))))))
174 '*))
175 ,(cond ((constant-lvar-p dims)
176 (let* ((val (lvar-value dims))
177 (cdims (if (listp val) val (list val))))
178 (if simple
179 cdims
180 (length cdims))))
181 ((csubtypep (lvar-type dims)
182 (specifier-type 'integer))
183 '(*))
185 '*))))
186 (specifier-type 'array))))
188 ;;; Complex array operations should assert that their array argument
189 ;;; is complex. In SBCL, vectors with fill-pointers are complex.
190 (defoptimizer (fill-pointer derive-type) ((vector))
191 (assert-array-complex vector))
192 (defoptimizer (%set-fill-pointer derive-type) ((vector index))
193 (declare (ignorable index))
194 (assert-array-complex vector))
196 (defoptimizer (vector-push derive-type) ((object vector))
197 (declare (ignorable object))
198 (assert-array-complex vector))
199 (defoptimizer (vector-push-extend derive-type)
200 ((object vector &optional index))
201 (declare (ignorable object index))
202 (assert-array-complex vector))
203 (defoptimizer (vector-pop derive-type) ((vector))
204 (assert-array-complex vector))
206 ;;;; constructors
208 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
209 ;;; elements.
210 (define-source-transform vector (&rest elements)
211 (let ((len (length elements))
212 (n -1))
213 (once-only ((n-vec `(make-array ,len)))
214 `(progn
215 ,@(mapcar (lambda (el)
216 (once-only ((n-val el))
217 `(locally (declare (optimize (safety 0)))
218 (setf (svref ,n-vec ,(incf n)) ,n-val))))
219 elements)
220 ,n-vec))))
222 ;;; Just convert it into a MAKE-ARRAY.
223 (deftransform make-string ((length &key
224 (element-type 'character)
225 (initial-element
226 #.*default-init-char-form*)))
227 `(the simple-string (make-array (the index length)
228 :element-type element-type
229 ,@(when initial-element
230 '(:initial-element initial-element)))))
232 (deftransform make-array ((dims &key initial-element element-type
233 adjustable fill-pointer)
234 (t &rest *))
235 (when (null initial-element)
236 (give-up-ir1-transform))
237 (let* ((eltype (cond ((not element-type) t)
238 ((not (constant-lvar-p element-type))
239 (give-up-ir1-transform
240 "ELEMENT-TYPE is not constant."))
242 (lvar-value element-type))))
243 (eltype-type (ir1-transform-specifier-type eltype))
244 (saetp (find-if (lambda (saetp)
245 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
246 sb!vm:*specialized-array-element-type-properties*))
247 (creation-form `(make-array dims
248 :element-type ',(type-specifier (sb!vm:saetp-ctype saetp))
249 ,@(when fill-pointer
250 '(:fill-pointer fill-pointer))
251 ,@(when adjustable
252 '(:adjustable adjustable)))))
254 (unless saetp
255 (give-up-ir1-transform "ELEMENT-TYPE not found in *SAETP*: ~S" eltype))
257 (cond ((and (constant-lvar-p initial-element)
258 (eql (lvar-value initial-element)
259 (sb!vm:saetp-initial-element-default saetp)))
260 creation-form)
262 ;; error checking for target, disabled on the host because
263 ;; (CTYPE-OF #\Null) is not possible.
264 #-sb-xc-host
265 (when (constant-lvar-p initial-element)
266 (let ((value (lvar-value initial-element)))
267 (cond
268 ((not (ctypep value (sb!vm:saetp-ctype saetp)))
269 ;; this case will cause an error at runtime, so we'd
270 ;; better WARN about it now.
271 (warn 'array-initial-element-mismatch
272 :format-control "~@<~S is not a ~S (which is the ~
273 ~S of ~S).~@:>"
274 :format-arguments
275 (list
276 value
277 (type-specifier (sb!vm:saetp-ctype saetp))
278 'upgraded-array-element-type
279 eltype)))
280 ((not (ctypep value eltype-type))
281 ;; this case will not cause an error at runtime, but
282 ;; it's still worth STYLE-WARNing about.
283 (compiler-style-warn "~S is not a ~S."
284 value eltype)))))
285 `(let ((array ,creation-form))
286 (multiple-value-bind (vector)
287 (%data-vector-and-index array 0)
288 (fill vector initial-element))
289 array)))))
291 ;;; The integer type restriction on the length ensures that it will be
292 ;;; a vector. The lack of :ADJUSTABLE, :FILL-POINTER, and
293 ;;; :DISPLACED-TO keywords ensures that it will be simple; the lack of
294 ;;; :INITIAL-ELEMENT relies on another transform to deal with that
295 ;;; kind of initialization efficiently.
296 (deftransform make-array ((length &key element-type)
297 (integer &rest *))
298 (let* ((eltype (cond ((not element-type) t)
299 ((not (constant-lvar-p element-type))
300 (give-up-ir1-transform
301 "ELEMENT-TYPE is not constant."))
303 (lvar-value element-type))))
304 (len (if (constant-lvar-p length)
305 (lvar-value length)
306 '*))
307 (eltype-type (ir1-transform-specifier-type eltype))
308 (result-type-spec
309 `(simple-array
310 ,(if (unknown-type-p eltype-type)
311 (give-up-ir1-transform
312 "ELEMENT-TYPE is an unknown type: ~S" eltype)
313 (sb!xc:upgraded-array-element-type eltype))
314 (,len)))
315 (saetp (find-if (lambda (saetp)
316 (csubtypep eltype-type (sb!vm:saetp-ctype saetp)))
317 sb!vm:*specialized-array-element-type-properties*)))
318 (unless saetp
319 (give-up-ir1-transform
320 "cannot open-code creation of ~S" result-type-spec))
321 #-sb-xc-host
322 (unless (ctypep (sb!vm:saetp-initial-element-default saetp) eltype-type)
323 ;; This situation arises e.g. in (MAKE-ARRAY 4 :ELEMENT-TYPE
324 ;; '(INTEGER 1 5)) ANSI's definition of MAKE-ARRAY says "If
325 ;; INITIAL-ELEMENT is not supplied, the consequences of later
326 ;; reading an uninitialized element of new-array are undefined,"
327 ;; so this could be legal code as long as the user plans to
328 ;; write before he reads, and if he doesn't we're free to do
329 ;; anything we like. But in case the user doesn't know to write
330 ;; elements before he reads elements (or to read manuals before
331 ;; he writes code:-), we'll signal a STYLE-WARNING in case he
332 ;; didn't realize this.
333 (compiler-style-warn "The default initial element ~S is not a ~S."
334 (sb!vm:saetp-initial-element-default saetp)
335 eltype))
336 (let* ((n-bits-per-element (sb!vm:saetp-n-bits saetp))
337 (typecode (sb!vm:saetp-typecode saetp))
338 (n-pad-elements (sb!vm:saetp-n-pad-elements saetp))
339 (padded-length-form (if (zerop n-pad-elements)
340 'length
341 `(+ length ,n-pad-elements)))
342 (n-words-form
343 (cond
344 ((= n-bits-per-element 0) 0)
345 ((>= n-bits-per-element sb!vm:n-word-bits)
346 `(* ,padded-length-form
347 (the fixnum ; i.e., not RATIO
348 ,(/ n-bits-per-element sb!vm:n-word-bits))))
350 (let ((n-elements-per-word (/ sb!vm:n-word-bits
351 n-bits-per-element)))
352 (declare (type index n-elements-per-word)) ; i.e., not RATIO
353 `(ceiling ,padded-length-form ,n-elements-per-word))))))
354 (values
355 `(truly-the ,result-type-spec
356 (allocate-vector ,typecode length ,n-words-form))
357 '((declare (type index length)))))))
359 ;;; The list type restriction does not ensure that the result will be a
360 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
361 ;;; and displaced-to keywords ensures that it will be simple.
363 ;;; FIXME: should we generalize this transform to non-simple (though
364 ;;; non-displaced-to) arrays, given that we have %WITH-ARRAY-DATA to
365 ;;; deal with those? Maybe when the DEFTRANSFORM
366 ;;; %DATA-VECTOR-AND-INDEX in the VECTOR case problem is solved? --
367 ;;; CSR, 2002-07-01
368 (deftransform make-array ((dims &key element-type)
369 (list &rest *))
370 (unless (or (null element-type) (constant-lvar-p element-type))
371 (give-up-ir1-transform
372 "The element-type is not constant; cannot open code array creation."))
373 (unless (constant-lvar-p dims)
374 (give-up-ir1-transform
375 "The dimension list is not constant; cannot open code array creation."))
376 (let ((dims (lvar-value dims)))
377 (unless (every #'integerp dims)
378 (give-up-ir1-transform
379 "The dimension list contains something other than an integer: ~S"
380 dims))
381 (if (= (length dims) 1)
382 `(make-array ',(car dims)
383 ,@(when element-type
384 '(:element-type element-type)))
385 (let* ((total-size (reduce #'* dims))
386 (rank (length dims))
387 (spec `(simple-array
388 ,(cond ((null element-type) t)
389 ((and (constant-lvar-p element-type)
390 (ir1-transform-specifier-type
391 (lvar-value element-type)))
392 (sb!xc:upgraded-array-element-type
393 (lvar-value element-type)))
394 (t '*))
395 ,(make-list rank :initial-element '*))))
396 `(let ((header (make-array-header sb!vm:simple-array-widetag ,rank)))
397 (setf (%array-fill-pointer header) ,total-size)
398 (setf (%array-fill-pointer-p header) nil)
399 (setf (%array-available-elements header) ,total-size)
400 (setf (%array-data-vector header)
401 (make-array ,total-size
402 ,@(when element-type
403 '(:element-type element-type))))
404 (setf (%array-displaced-p header) nil)
405 ,@(let ((axis -1))
406 (mapcar (lambda (dim)
407 `(setf (%array-dimension header ,(incf axis))
408 ,dim))
409 dims))
410 (truly-the ,spec header))))))
412 ;;;; miscellaneous properties of arrays
414 ;;; Transforms for various array properties. If the property is know
415 ;;; at compile time because of a type spec, use that constant value.
417 ;;; Most of this logic may end up belonging in code/late-type.lisp;
418 ;;; however, here we also need the -OR-GIVE-UP for the transforms, and
419 ;;; maybe this is just too sloppy for actual type logic. -- CSR,
420 ;;; 2004-02-18
421 (defun array-type-dimensions-or-give-up (type)
422 (typecase type
423 (array-type (array-type-dimensions type))
424 (union-type
425 (let ((types (union-type-types type)))
426 ;; there are at least two types, right?
427 (aver (> (length types) 1))
428 (let ((result (array-type-dimensions-or-give-up (car types))))
429 (dolist (type (cdr types) result)
430 (unless (equal (array-type-dimensions-or-give-up type) result)
431 (give-up-ir1-transform))))))
432 ;; FIXME: intersection type [e.g. (and (array * (*)) (satisfies foo)) ]
433 (t (give-up-ir1-transform))))
435 (defun conservative-array-type-complexp (type)
436 (typecase type
437 (array-type (array-type-complexp type))
438 (union-type
439 (let ((types (union-type-types type)))
440 (aver (> (length types) 1))
441 (let ((result (conservative-array-type-complexp (car types))))
442 (dolist (type (cdr types) result)
443 (unless (eq (conservative-array-type-complexp type) result)
444 (return-from conservative-array-type-complexp :maybe))))))
445 ;; FIXME: intersection type
446 (t :maybe)))
448 ;;; If we can tell the rank from the type info, use it instead.
449 (deftransform array-rank ((array))
450 (let ((array-type (lvar-type array)))
451 (let ((dims (array-type-dimensions-or-give-up array-type)))
452 (if (not (listp dims))
453 (give-up-ir1-transform
454 "The array rank is not known at compile time: ~S"
455 dims)
456 (length dims)))))
458 ;;; If we know the dimensions at compile time, just use it. Otherwise,
459 ;;; if we can tell that the axis is in bounds, convert to
460 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
461 ;;; (if it's simple and a vector).
462 (deftransform array-dimension ((array axis)
463 (array index))
464 (unless (constant-lvar-p axis)
465 (give-up-ir1-transform "The axis is not constant."))
466 (let ((array-type (lvar-type array))
467 (axis (lvar-value axis)))
468 (let ((dims (array-type-dimensions-or-give-up array-type)))
469 (unless (listp dims)
470 (give-up-ir1-transform
471 "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
472 (unless (> (length dims) axis)
473 (abort-ir1-transform "The array has dimensions ~S, ~W is too large."
474 dims
475 axis))
476 (let ((dim (nth axis dims)))
477 (cond ((integerp dim)
478 dim)
479 ((= (length dims) 1)
480 (ecase (conservative-array-type-complexp array-type)
481 ((t)
482 '(%array-dimension array 0))
483 ((nil)
484 '(length array))
485 ((:maybe)
486 (give-up-ir1-transform
487 "can't tell whether array is simple"))))
489 '(%array-dimension array axis)))))))
491 ;;; If the length has been declared and it's simple, just return it.
492 (deftransform length ((vector)
493 ((simple-array * (*))))
494 (let ((type (lvar-type vector)))
495 (let ((dims (array-type-dimensions-or-give-up type)))
496 (unless (and (listp dims) (integerp (car dims)))
497 (give-up-ir1-transform
498 "Vector length is unknown, must call LENGTH at runtime."))
499 (car dims))))
501 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
502 ;;; simple, it will extract the length slot from the vector. It it's
503 ;;; complex, it will extract the fill pointer slot from the array
504 ;;; header.
505 (deftransform length ((vector) (vector))
506 '(vector-length vector))
508 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
509 ;;; compile-time constant.
510 (deftransform vector-length ((vector))
511 (let ((vtype (lvar-type vector)))
512 (let ((dim (first (array-type-dimensions-or-give-up vtype))))
513 (when (eq dim '*)
514 (give-up-ir1-transform))
515 (when (conservative-array-type-complexp vtype)
516 (give-up-ir1-transform))
517 dim)))
519 ;;; Again, if we can tell the results from the type, just use it.
520 ;;; Otherwise, if we know the rank, convert into a computation based
521 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
522 ;;; multiplications because we know that the total size must be an
523 ;;; INDEX.
524 (deftransform array-total-size ((array)
525 (array))
526 (let ((array-type (lvar-type array)))
527 (let ((dims (array-type-dimensions-or-give-up array-type)))
528 (unless (listp dims)
529 (give-up-ir1-transform "can't tell the rank at compile time"))
530 (if (member '* dims)
531 (do ((form 1 `(truly-the index
532 (* (array-dimension array ,i) ,form)))
533 (i 0 (1+ i)))
534 ((= i (length dims)) form))
535 (reduce #'* dims)))))
537 ;;; Only complex vectors have fill pointers.
538 (deftransform array-has-fill-pointer-p ((array))
539 (let ((array-type (lvar-type array)))
540 (let ((dims (array-type-dimensions-or-give-up array-type)))
541 (if (and (listp dims) (not (= (length dims) 1)))
543 (ecase (conservative-array-type-complexp array-type)
544 ((t)
546 ((nil)
547 nil)
548 ((:maybe)
549 (give-up-ir1-transform
550 "The array type is ambiguous; must call ~
551 ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
553 ;;; Primitive used to verify indices into arrays. If we can tell at
554 ;;; compile-time or we are generating unsafe code, don't bother with
555 ;;; the VOP.
556 (deftransform %check-bound ((array dimension index) * * :node node)
557 (cond ((policy node (= insert-array-bounds-checks 0))
558 'index)
559 ((not (constant-lvar-p dimension))
560 (give-up-ir1-transform))
562 (let ((dim (lvar-value dimension)))
563 `(the (integer 0 (,dim)) index)))))
565 ;;;; WITH-ARRAY-DATA
567 ;;; This checks to see whether the array is simple and the start and
568 ;;; end are in bounds. If so, it proceeds with those values.
569 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
570 ;;; may be further optimized.
572 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
573 ;;; START-VAR and END-VAR to the start and end of the designated
574 ;;; portion of the data vector. SVALUE and EVALUE are any start and
575 ;;; end specified to the original operation, and are factored into the
576 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
577 ;;; offset of all displacements encountered, and does not include
578 ;;; SVALUE.
580 ;;; When FORCE-INLINE is set, the underlying %WITH-ARRAY-DATA form is
581 ;;; forced to be inline, overriding the ordinary judgment of the
582 ;;; %WITH-ARRAY-DATA DEFTRANSFORMs. Ordinarily the DEFTRANSFORMs are
583 ;;; fairly picky about their arguments, figuring that if you haven't
584 ;;; bothered to get all your ducks in a row, you probably don't care
585 ;;; that much about speed anyway! But in some cases it makes sense to
586 ;;; do type testing inside %WITH-ARRAY-DATA instead of outside, and
587 ;;; the DEFTRANSFORM can't tell that that's going on, so it can make
588 ;;; sense to use FORCE-INLINE option in that case.
589 (def!macro with-array-data (((data-var array &key offset-var)
590 (start-var &optional (svalue 0))
591 (end-var &optional (evalue nil))
592 &key force-inline)
593 &body forms)
594 (once-only ((n-array array)
595 (n-svalue `(the index ,svalue))
596 (n-evalue `(the (or index null) ,evalue)))
597 `(multiple-value-bind (,data-var
598 ,start-var
599 ,end-var
600 ,@(when offset-var `(,offset-var)))
601 (if (not (array-header-p ,n-array))
602 (let ((,n-array ,n-array))
603 (declare (type (simple-array * (*)) ,n-array))
604 ,(once-only ((n-len `(length ,n-array))
605 (n-end `(or ,n-evalue ,n-len)))
606 `(if (<= ,n-svalue ,n-end ,n-len)
607 ;; success
608 (values ,n-array ,n-svalue ,n-end 0)
609 (failed-%with-array-data ,n-array
610 ,n-svalue
611 ,n-evalue))))
612 (,(if force-inline '%with-array-data-macro '%with-array-data)
613 ,n-array ,n-svalue ,n-evalue))
614 ,@forms)))
616 ;;; This is the fundamental definition of %WITH-ARRAY-DATA, for use in
617 ;;; DEFTRANSFORMs and DEFUNs.
618 (def!macro %with-array-data-macro (array
619 start
621 &key
622 (element-type '*)
623 unsafe?
624 fail-inline?)
625 (with-unique-names (size defaulted-end data cumulative-offset)
626 `(let* ((,size (array-total-size ,array))
627 (,defaulted-end
628 (cond (,end
629 (unless (or ,unsafe? (<= ,end ,size))
630 ,(if fail-inline?
631 `(error 'bounding-indices-bad-error
632 :datum (cons ,start ,end)
633 :expected-type `(cons (integer 0 ,',size)
634 (integer ,',start ,',size))
635 :object ,array)
636 `(failed-%with-array-data ,array ,start ,end)))
637 ,end)
638 (t ,size))))
639 (unless (or ,unsafe? (<= ,start ,defaulted-end))
640 ,(if fail-inline?
641 `(error 'bounding-indices-bad-error
642 :datum (cons ,start ,end)
643 :expected-type `(cons (integer 0 ,',size)
644 (integer ,',start ,',size))
645 :object ,array)
646 `(failed-%with-array-data ,array ,start ,end)))
647 (do ((,data ,array (%array-data-vector ,data))
648 (,cumulative-offset 0
649 (+ ,cumulative-offset
650 (%array-displacement ,data))))
651 ((not (array-header-p ,data))
652 (values (the (simple-array ,element-type 1) ,data)
653 (the index (+ ,cumulative-offset ,start))
654 (the index (+ ,cumulative-offset ,defaulted-end))
655 (the index ,cumulative-offset)))
656 (declare (type index ,cumulative-offset))))))
658 (deftransform %with-array-data ((array start end)
659 ;; It might very well be reasonable to
660 ;; allow general ARRAY here, I just
661 ;; haven't tried to understand the
662 ;; performance issues involved. --
663 ;; WHN, and also CSR 2002-05-26
664 ((or vector simple-array) index (or index null))
666 :node node
667 :policy (> speed space))
668 "inline non-SIMPLE-vector-handling logic"
669 (let ((element-type (upgraded-element-type-specifier-or-give-up array)))
670 `(%with-array-data-macro array start end
671 :unsafe? ,(policy node (= safety 0))
672 :element-type ,element-type)))
674 ;;;; array accessors
676 ;;; We convert all typed array accessors into AREF and %ASET with type
677 ;;; assertions on the array.
678 (macrolet ((define-bit-frob (reffer setter simplep)
679 `(progn
680 (define-source-transform ,reffer (a &rest i)
681 `(aref (the (,',(if simplep 'simple-array 'array)
683 ,(mapcar (constantly '*) i))
684 ,a) ,@i))
685 (define-source-transform ,setter (a &rest i)
686 `(%aset (the (,',(if simplep 'simple-array 'array)
688 ,(cdr (mapcar (constantly '*) i)))
689 ,a) ,@i)))))
690 (define-bit-frob sbit %sbitset t)
691 (define-bit-frob bit %bitset nil))
692 (macrolet ((define-frob (reffer setter type)
693 `(progn
694 (define-source-transform ,reffer (a i)
695 `(aref (the ,',type ,a) ,i))
696 (define-source-transform ,setter (a i v)
697 `(%aset (the ,',type ,a) ,i ,v)))))
698 (define-frob svref %svset simple-vector)
699 (define-frob schar %scharset simple-string)
700 (define-frob char %charset string))
702 (macrolet (;; This is a handy macro for computing the row-major index
703 ;; given a set of indices. We wrap each index with a call
704 ;; to %CHECK-BOUND to ensure that everything works out
705 ;; correctly. We can wrap all the interior arithmetic with
706 ;; TRULY-THE INDEX because we know the resultant
707 ;; row-major index must be an index.
708 (with-row-major-index ((array indices index &optional new-value)
709 &rest body)
710 `(let (n-indices dims)
711 (dotimes (i (length ,indices))
712 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
713 (push (make-symbol (format nil "DIM-~D" i)) dims))
714 (setf n-indices (nreverse n-indices))
715 (setf dims (nreverse dims))
716 `(lambda (,',array ,@n-indices
717 ,@',(when new-value (list new-value)))
718 (let* (,@(let ((,index -1))
719 (mapcar (lambda (name)
720 `(,name (array-dimension
721 ,',array
722 ,(incf ,index))))
723 dims))
724 (,',index
725 ,(if (null dims)
727 (do* ((dims dims (cdr dims))
728 (indices n-indices (cdr indices))
729 (last-dim nil (car dims))
730 (form `(%check-bound ,',array
731 ,(car dims)
732 ,(car indices))
733 `(truly-the
734 index
735 (+ (truly-the index
736 (* ,form
737 ,last-dim))
738 (%check-bound
739 ,',array
740 ,(car dims)
741 ,(car indices))))))
742 ((null (cdr dims)) form)))))
743 ,',@body)))))
745 ;; Just return the index after computing it.
746 (deftransform array-row-major-index ((array &rest indices))
747 (with-row-major-index (array indices index)
748 index))
750 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
751 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
752 ;; expression for the row major index.
753 (deftransform aref ((array &rest indices))
754 (with-row-major-index (array indices index)
755 (hairy-data-vector-ref array index)))
757 (deftransform %aset ((array &rest stuff))
758 (let ((indices (butlast stuff)))
759 (with-row-major-index (array indices index new-value)
760 (hairy-data-vector-set array index new-value)))))
762 ;; For AREF of vectors we do the bounds checking in the callee. This
763 ;; lets us do a significantly more efficient check for simple-arrays
764 ;; without bloating the code.
765 (deftransform aref ((array index) (t t) * :node node)
766 (if (policy node (zerop insert-array-bounds-checks))
767 `(hairy-data-vector-ref array index)
768 `(hairy-data-vector-ref/check-bounds array index)))
770 (deftransform %aset ((array index new-value) (t t t) * :node node)
771 (if (policy node (zerop insert-array-bounds-checks))
772 `(hairy-data-vector-set array index new-value)
773 `(hairy-data-vector-set/check-bounds array index new-value)))
775 ;;; But if we find out later that there's some useful type information
776 ;;; available, switch back to the normal one to give other transforms
777 ;;; a stab at it.
778 (macrolet ((define (name transform-to extra extra-type)
779 `(deftransform ,name ((array index ,@extra))
780 (let ((type (lvar-type array))
781 (element-type (extract-upgraded-element-type array)))
782 ;; If an element type has been declared, we want to
783 ;; use that information it for type checking (even
784 ;; if the access can't be optimized due to the array
785 ;; not being simple).
786 (when (and (eql element-type *wild-type*)
787 ;; This type logic corresponds to the special
788 ;; case for strings in HAIRY-DATA-VECTOR-REF
789 ;; (generic/vm-tran.lisp)
790 (not (csubtypep type (specifier-type 'simple-string))))
791 (when (or (not (array-type-p type))
792 ;; If it's a simple array, we might be able
793 ;; to inline the access completely.
794 (not (null (array-type-complexp type))))
795 (give-up-ir1-transform
796 "Upgraded element type of array is not known at compile time."))))
797 `(,',transform-to array
798 (%check-bound array
799 (array-dimension array 0)
800 index)
801 ,@',extra))))
802 (define hairy-data-vector-ref/check-bounds
803 hairy-data-vector-ref nil nil)
804 (define hairy-data-vector-set/check-bounds
805 hairy-data-vector-set (new-value) (*)))
807 (deftransform aref ((array index) ((or simple-vector
808 (simple-unboxed-array 1))
809 index))
810 (let ((type (lvar-type array)))
811 (unless (array-type-p type)
812 ;; Not an exactly specified one-dimensional simple array -> punt
813 ;; to the complex version.
814 (give-up-ir1-transform)))
815 `(data-vector-ref array (%check-bound array
816 (array-dimension array 0)
817 index)))
819 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
820 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
821 ;;; array total size.
822 (deftransform row-major-aref ((array index))
823 `(hairy-data-vector-ref array
824 (%check-bound array (array-total-size array) index)))
825 (deftransform %set-row-major-aref ((array index new-value))
826 `(hairy-data-vector-set array
827 (%check-bound array (array-total-size array) index)
828 new-value))
830 ;;;; bit-vector array operation canonicalization
831 ;;;;
832 ;;;; We convert all bit-vector operations to have the result array
833 ;;;; specified. This allows any result allocation to be open-coded,
834 ;;;; and eliminates the need for any VM-dependent transforms to handle
835 ;;;; these cases.
837 (macrolet ((def (fun)
838 `(progn
839 (deftransform ,fun ((bit-array-1 bit-array-2
840 &optional result-bit-array)
841 (bit-vector bit-vector &optional null) *
842 :policy (>= speed space))
843 `(,',fun bit-array-1 bit-array-2
844 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
845 ;; If result is T, make it the first arg.
846 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
847 (bit-vector bit-vector (eql t)) *)
848 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
849 (def bit-and)
850 (def bit-ior)
851 (def bit-xor)
852 (def bit-eqv)
853 (def bit-nand)
854 (def bit-nor)
855 (def bit-andc1)
856 (def bit-andc2)
857 (def bit-orc1)
858 (def bit-orc2))
860 ;;; Similar for BIT-NOT, but there is only one arg...
861 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
862 (bit-vector &optional null) *
863 :policy (>= speed space))
864 '(bit-not bit-array-1
865 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
866 (deftransform bit-not ((bit-array-1 result-bit-array)
867 (bit-vector (eql t)))
868 '(bit-not bit-array-1 bit-array-1))
870 ;;; Pick off some constant cases.
871 (defoptimizer (array-header-p derive-type) ((array))
872 (let ((type (lvar-type array)))
873 (cond ((not (array-type-p type))
874 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
875 nil)
877 (let ((dims (array-type-dimensions type)))
878 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
879 ;; no array header
880 (specifier-type 'null))
881 ((and (listp dims) (/= (length dims) 1))
882 ;; multi-dimensional array, will have a header
883 (specifier-type '(eql t)))
884 ((eql (array-type-complexp type) t)
885 (specifier-type '(eql t)))
887 nil)))))))