1.0.12.8: refactor bounding index error signalling functions
[sbcl/simd.git] / src / compiler / array-tran.lisp
blob522413454797ae6c3b572f1e39f3a96c88bdb015
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 (= 0 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 `(values ,n-array ,n-svalue ,n-end 0)
617 `(if (<= ,n-svalue ,n-end ,n-len)
618 (values ,n-array ,n-svalue ,n-end 0)
619 ,(if check-fill-pointer
620 `(sequence-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue)
621 `(array-bounding-indices-bad-error ,n-array ,n-svalue ,n-evalue))))))
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 (if (and (array-type-p type)
665 (listp (array-type-dimensions type))
666 (not (null (cdr (array-type-dimensions type)))))
667 ;; If it's a simple multidimensional array, then just return
668 ;; its data vector directly rather than going through
669 ;; %WITH-ARRAY-DATA-MACRO. SBCL doesn't generally generate
670 ;; code that would use this currently, but we have encouraged
671 ;; users to use WITH-ARRAY-DATA and we may use it ourselves at
672 ;; some point in the future for optimized libraries or
673 ;; similar.
675 ;; FIXME: The return values here don't seem sane, and
676 ;; bounds-checks are elided!
677 `(let ((data (truly-the (simple-array ,element-type (*))
678 (%array-data-vector array))))
679 (values data 0 (length data) 0))
680 `(%with-array-data-macro array start end
681 :check-fill-pointer ,check-fill-pointer
682 :check-bounds ,(policy node (< 0 insert-array-bounds-checks))
683 :element-type ,element-type))))
685 ;; It might very well be reasonable to allow general ARRAY here, I
686 ;; just haven't tried to understand the performance issues involved.
687 ;; -- WHN, and also CSR 2002-05-26
688 (deftransform %with-array-data ((array start end)
689 ((or vector simple-array) index (or index null) t)
691 :node node
692 :policy (> speed space))
693 "inline non-SIMPLE-vector-handling logic"
694 (transform-%with-array-data/muble array node nil))
695 (deftransform %with-array-data/fp ((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 t))
703 ;;;; array accessors
705 ;;; We convert all typed array accessors into AREF and %ASET with type
706 ;;; assertions on the array.
707 (macrolet ((define-bit-frob (reffer setter simplep)
708 `(progn
709 (define-source-transform ,reffer (a &rest i)
710 `(aref (the (,',(if simplep 'simple-array 'array)
712 ,(mapcar (constantly '*) i))
713 ,a) ,@i))
714 (define-source-transform ,setter (a &rest i)
715 `(%aset (the (,',(if simplep 'simple-array 'array)
717 ,(cdr (mapcar (constantly '*) i)))
718 ,a) ,@i)))))
719 (define-bit-frob sbit %sbitset t)
720 (define-bit-frob bit %bitset nil))
721 (macrolet ((define-frob (reffer setter type)
722 `(progn
723 (define-source-transform ,reffer (a i)
724 `(aref (the ,',type ,a) ,i))
725 (define-source-transform ,setter (a i v)
726 `(%aset (the ,',type ,a) ,i ,v)))))
727 (define-frob svref %svset simple-vector)
728 (define-frob schar %scharset simple-string)
729 (define-frob char %charset string))
731 (macrolet (;; This is a handy macro for computing the row-major index
732 ;; given a set of indices. We wrap each index with a call
733 ;; to %CHECK-BOUND to ensure that everything works out
734 ;; correctly. We can wrap all the interior arithmetic with
735 ;; TRULY-THE INDEX because we know the resultant
736 ;; row-major index must be an index.
737 (with-row-major-index ((array indices index &optional new-value)
738 &rest body)
739 `(let (n-indices dims)
740 (dotimes (i (length ,indices))
741 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
742 (push (make-symbol (format nil "DIM-~D" i)) dims))
743 (setf n-indices (nreverse n-indices))
744 (setf dims (nreverse dims))
745 `(lambda (,',array ,@n-indices
746 ,@',(when new-value (list new-value)))
747 (let* (,@(let ((,index -1))
748 (mapcar (lambda (name)
749 `(,name (array-dimension
750 ,',array
751 ,(incf ,index))))
752 dims))
753 (,',index
754 ,(if (null dims)
756 (do* ((dims dims (cdr dims))
757 (indices n-indices (cdr indices))
758 (last-dim nil (car dims))
759 (form `(%check-bound ,',array
760 ,(car dims)
761 ,(car indices))
762 `(truly-the
763 index
764 (+ (truly-the index
765 (* ,form
766 ,last-dim))
767 (%check-bound
768 ,',array
769 ,(car dims)
770 ,(car indices))))))
771 ((null (cdr dims)) form)))))
772 ,',@body)))))
774 ;; Just return the index after computing it.
775 (deftransform array-row-major-index ((array &rest indices))
776 (with-row-major-index (array indices index)
777 index))
779 ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
780 ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
781 ;; expression for the row major index.
782 (deftransform aref ((array &rest indices))
783 (with-row-major-index (array indices index)
784 (hairy-data-vector-ref array index)))
786 (deftransform %aset ((array &rest stuff))
787 (let ((indices (butlast stuff)))
788 (with-row-major-index (array indices index new-value)
789 (hairy-data-vector-set array index new-value)))))
791 ;; For AREF of vectors we do the bounds checking in the callee. This
792 ;; lets us do a significantly more efficient check for simple-arrays
793 ;; without bloating the code. If we already know the type of the array
794 ;; with sufficient precision, skip directly to DATA-VECTOR-REF.
795 (deftransform aref ((array index) (t t) * :node node)
796 (let ((type (lvar-type array)))
797 (cond ((and (array-type-p type)
798 (null (array-type-complexp type))
799 (not (eql (extract-upgraded-element-type array)
800 *wild-type*))
801 (eql (length (array-type-dimensions type)) 1))
802 `(data-vector-ref array (%check-bound array
803 (array-dimension array 0)
804 index)))
805 ((policy node (zerop insert-array-bounds-checks))
806 `(hairy-data-vector-ref array index))
808 `(hairy-data-vector-ref/check-bounds array index)))))
810 (deftransform %aset ((array index new-value) (t t t) * :node node)
811 (if (policy node (zerop insert-array-bounds-checks))
812 `(hairy-data-vector-set array index new-value)
813 `(hairy-data-vector-set/check-bounds array index new-value)))
815 ;;; But if we find out later that there's some useful type information
816 ;;; available, switch back to the normal one to give other transforms
817 ;;; a stab at it.
818 (macrolet ((define (name transform-to extra extra-type)
819 `(deftransform ,name ((array index ,@extra))
820 (let ((type (lvar-type array))
821 (element-type (extract-upgraded-element-type array)))
822 ;; If an element type has been declared, we want to
823 ;; use that information it for type checking (even
824 ;; if the access can't be optimized due to the array
825 ;; not being simple).
826 (when (and (eql element-type *wild-type*)
827 ;; This type logic corresponds to the special
828 ;; case for strings in HAIRY-DATA-VECTOR-REF
829 ;; (generic/vm-tran.lisp)
830 (not (csubtypep type (specifier-type 'simple-string))))
831 (when (or (not (array-type-p type))
832 ;; If it's a simple array, we might be able
833 ;; to inline the access completely.
834 (not (null (array-type-complexp type))))
835 (give-up-ir1-transform
836 "Upgraded element type of array is not known at compile time."))))
837 `(,',transform-to array
838 (%check-bound array
839 (array-dimension array 0)
840 index)
841 ,@',extra))))
842 (define hairy-data-vector-ref/check-bounds
843 hairy-data-vector-ref nil nil)
844 (define hairy-data-vector-set/check-bounds
845 hairy-data-vector-set (new-value) (*)))
847 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
848 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
849 ;;; array total size.
850 (deftransform row-major-aref ((array index))
851 `(hairy-data-vector-ref array
852 (%check-bound array (array-total-size array) index)))
853 (deftransform %set-row-major-aref ((array index new-value))
854 `(hairy-data-vector-set array
855 (%check-bound array (array-total-size array) index)
856 new-value))
858 ;;;; bit-vector array operation canonicalization
859 ;;;;
860 ;;;; We convert all bit-vector operations to have the result array
861 ;;;; specified. This allows any result allocation to be open-coded,
862 ;;;; and eliminates the need for any VM-dependent transforms to handle
863 ;;;; these cases.
865 (macrolet ((def (fun)
866 `(progn
867 (deftransform ,fun ((bit-array-1 bit-array-2
868 &optional result-bit-array)
869 (bit-vector bit-vector &optional null) *
870 :policy (>= speed space))
871 `(,',fun bit-array-1 bit-array-2
872 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
873 ;; If result is T, make it the first arg.
874 (deftransform ,fun ((bit-array-1 bit-array-2 result-bit-array)
875 (bit-vector bit-vector (eql t)) *)
876 `(,',fun bit-array-1 bit-array-2 bit-array-1)))))
877 (def bit-and)
878 (def bit-ior)
879 (def bit-xor)
880 (def bit-eqv)
881 (def bit-nand)
882 (def bit-nor)
883 (def bit-andc1)
884 (def bit-andc2)
885 (def bit-orc1)
886 (def bit-orc2))
888 ;;; Similar for BIT-NOT, but there is only one arg...
889 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
890 (bit-vector &optional null) *
891 :policy (>= speed space))
892 '(bit-not bit-array-1
893 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
894 (deftransform bit-not ((bit-array-1 result-bit-array)
895 (bit-vector (eql t)))
896 '(bit-not bit-array-1 bit-array-1))
898 ;;; Pick off some constant cases.
899 (defoptimizer (array-header-p derive-type) ((array))
900 (let ((type (lvar-type array)))
901 (cond ((not (array-type-p type))
902 ;; FIXME: use analogue of ARRAY-TYPE-DIMENSIONS-OR-GIVE-UP
903 nil)
905 (let ((dims (array-type-dimensions type)))
906 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
907 ;; no array header
908 (specifier-type 'null))
909 ((and (listp dims) (/= (length dims) 1))
910 ;; multi-dimensional array, will have a header
911 (specifier-type '(eql t)))
912 ((eql (array-type-complexp type) t)
913 (specifier-type '(eql t)))
915 nil)))))))