/[cmucl]/src/compiler/array-tran.lisp
ViewVC logotype

Contents of /src/compiler/array-tran.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Tue May 11 13:49:57 1993 UTC (20 years, 11 months ago) by ram
Branch: MAIN
Changes since 1.14: +25 -16 lines
Update transforms to inhibit ones which pessimize byte code.
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/array-tran.lisp,v 1.15 1993/05/11 13:49:57 ram Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; This file contains array specific optimizers and transforms.
15 ;;;
16 ;;; Extracted from srctran and extended by William Lott.
17 ;;;
18 (in-package "C")
19
20
21 ;;;; Derive-Type Optimizers
22
23 ;;; ASSERT-ARRAY-RANK -- internal
24 ;;;
25 ;;; Array operations that use a specific number of indices implicitly assert
26 ;;; that the array is of that rank.
27 ;;;
28 (defun assert-array-rank (array rank)
29 (assert-continuation-type
30 array
31 (specifier-type `(array * ,(make-list rank :initial-element '*)))))
32
33 ;;; EXTRACT-ELEMENT-TYPE -- internal
34 ;;;
35 ;;; Array access functions return an object from the array, hence it's type
36 ;;; is going to be the array element type.
37 ;;;
38 (defun extract-element-type (array)
39 (let ((type (continuation-type array)))
40 (if (array-type-p type)
41 (array-type-element-type type)
42 *universal-type*)))
43
44 ;;; ASSERT-NEW-VALUE-TYPE -- internal
45 ;;;
46 ;;; The ``new-value'' for array setters must fit in the array, and the
47 ;;; return type is going to be the same as the new-value for setf functions.
48 ;;;
49 (defun assert-new-value-type (new-value array)
50 (let ((type (continuation-type array)))
51 (when (array-type-p type)
52 (assert-continuation-type new-value (array-type-element-type type))))
53 (continuation-type new-value))
54
55 ;;; Unsupplied-Or-NIL -- Internal
56 ;;;
57 ;;; Return true if Arg is NIL, or is a constant-continuation whose value is
58 ;;; NIL, false otherwise.
59 ;;;
60 (defun unsupplied-or-nil (arg)
61 (declare (type (or continuation null) arg))
62 (or (not arg)
63 (and (constant-continuation-p arg)
64 (not (continuation-value arg)))))
65
66
67 ;;; ARRAY-IN-BOUNDS-P -- derive-type optimizer.
68 ;;;
69 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
70 (assert-array-rank array (length indices))
71 *universal-type*)
72
73 ;;; AREF -- derive-type optimizer.
74 ;;;
75 (defoptimizer (aref derive-type) ((array &rest indices))
76 (assert-array-rank array (length indices))
77 (extract-element-type array))
78
79 ;;; %ASET -- derive-type optimizer.
80 ;;;
81 (defoptimizer (%aset derive-type) ((array &rest stuff))
82 (assert-array-rank array (1- (length stuff)))
83 (assert-new-value-type (car (last stuff)) array))
84
85 ;;; DATA-VECTOR-REF -- derive-type optimizer.
86 ;;;
87 (defoptimizer (data-vector-ref derive-type) ((array index))
88 (extract-element-type array))
89
90 ;;; DATA-VECTOR-SET -- derive-type optimizer.
91 ;;;
92 (defoptimizer (data-vector-set derive-type) ((array index new-value))
93 (assert-new-value-type new-value array))
94
95 ;;; %WITH-ARRAY-DATA -- derive-type optimizer.
96 ;;;
97 ;;; Figure out the type of the data vector if we know the argument element
98 ;;; type.
99 ;;;
100 (defoptimizer (%with-array-data derive-type) ((array start end))
101 (let ((atype (continuation-type array)))
102 (when (array-type-p atype)
103 (values-specifier-type
104 `(values (simple-array ,(type-specifier
105 (array-type-element-type atype))
106 (*))
107 index index index)))))
108
109
110 ;;; ARRAY-ROW-MAJOR-INDEX -- derive-type optimizer.
111 ;;;
112 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
113 (assert-array-rank array (length indices))
114 *universal-type*)
115
116 ;;; ROW-MAJOR-AREF -- derive-type optimizer.
117 ;;;
118 (defoptimizer (row-major-aref derive-type) ((array index))
119 (extract-element-type array))
120
121 ;;; %SET-ROW-MAJOR-AREF -- derive-type optimizer.
122 ;;;
123 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
124 (assert-new-value-type new-value array))
125
126 ;;; MAKE-ARRAY -- derive-type optimizer.
127 ;;;
128 (defoptimizer (make-array derive-type)
129 ((dims &key initial-element element-type initial-contents
130 adjustable fill-pointer displaced-index-offset displaced-to))
131 (let ((simple (and (unsupplied-or-nil adjustable)
132 (unsupplied-or-nil displaced-to)
133 (unsupplied-or-nil fill-pointer))))
134 (specifier-type
135 `(,(if simple 'simple-array 'array)
136 ,(cond ((not element-type) 't)
137 ((constant-continuation-p element-type)
138 (continuation-value element-type))
139 (t
140 '*))
141 ,(cond ((not simple)
142 '*)
143 ((constant-continuation-p dims)
144 (let ((val (continuation-value dims)))
145 (if (listp val) val (list val))))
146 ((csubtypep (continuation-type dims)
147 (specifier-type 'integer))
148 '(*))
149 (t
150 '*))))))
151
152
153 ;;;; Constructors.
154
155 ;;; VECTOR -- source-transform.
156 ;;;
157 ;;; Convert VECTOR into a make-array followed by setfs of all the elements.
158 ;;;
159 (def-source-transform vector (&rest elements)
160 (if *byte-compiling*
161 (values nil t)
162 (let ((len (length elements))
163 (n -1))
164 (once-only ((n-vec `(make-array ,len)))
165 `(progn
166 ,@(mapcar #'(lambda (el)
167 (once-only ((n-val el))
168 `(locally (declare (optimize (safety 0)))
169 (setf (svref ,n-vec ,(incf n))
170 ,n-val))))
171 elements)
172 ,n-vec)))))
173
174
175 ;;; MAKE-STRING -- source-transform.
176 ;;;
177 ;;; Just convert it into a make-array.
178 ;;;
179 (def-source-transform make-string (length &key (initial-element #\NULL))
180 (if *byte-compiling*
181 (values nil t)
182 `(make-array (the index ,length)
183 :element-type 'base-char
184 :initial-element ,initial-element)))
185
186 (defconstant array-info
187 '((base-char #\NULL 8 vm:simple-string-type)
188 (single-float 0.0s0 32 vm:simple-array-single-float-type)
189 (double-float 0.0d0 64 vm:simple-array-double-float-type)
190 (bit 0 1 vm:simple-bit-vector-type)
191 ((unsigned-byte 2) 0 2 vm:simple-array-unsigned-byte-2-type)
192 ((unsigned-byte 4) 0 4 vm:simple-array-unsigned-byte-4-type)
193 ((unsigned-byte 8) 0 8 vm:simple-array-unsigned-byte-8-type)
194 ((unsigned-byte 16) 0 16 vm:simple-array-unsigned-byte-16-type)
195 ((unsigned-byte 32) 0 32 vm:simple-array-unsigned-byte-32-type)
196 (t 0 32 vm:simple-vector-type)))
197
198 ;;; MAKE-ARRAY -- source-transform.
199 ;;;
200 ;;; The integer type restriction on the length assures that it will be a
201 ;;; vector. The lack of adjustable, fill-pointer, and displaced-to keywords
202 ;;; assures that it will be simple.
203 ;;;
204 (deftransform make-array ((length &key initial-element element-type)
205 (integer &rest *))
206 (let* ((eltype (cond ((not element-type) t)
207 ((not (constant-continuation-p element-type))
208 (give-up "Element-Type is not constant."))
209 (t
210 (continuation-value element-type))))
211 (len (if (constant-continuation-p length)
212 (continuation-value length)
213 '*))
214 (spec `(simple-array ,eltype (,len)))
215 (eltype-type (specifier-type eltype)))
216 (multiple-value-bind
217 (default-initial-element element-size typecode)
218 (dolist (info array-info
219 (give-up "Cannot open-code creation of ~S" spec))
220 (when (csubtypep eltype-type (specifier-type (car info)))
221 (return (values-list (cdr info)))))
222 (let* ((nwords-form
223 (if (>= element-size vm:word-bits)
224 `(* length ,(/ element-size vm:word-bits))
225 (let ((elements-per-word (/ 32 element-size)))
226 `(truncate (+ length
227 ,(if (eq 'vm:simple-string-type typecode)
228 elements-per-word
229 (1- elements-per-word)))
230 ,elements-per-word))))
231 (constructor
232 `(truly-the ,spec
233 (allocate-vector ,typecode length ,nwords-form))))
234 (values
235 (if (and default-initial-element
236 (or (null initial-element)
237 (and (constant-continuation-p initial-element)
238 (eql (continuation-value initial-element)
239 default-initial-element))))
240 constructor
241 `(truly-the ,spec (fill ,constructor initial-element)))
242 '((declare (type index length))))))))
243
244 ;;; MAKE-ARRAY -- transform.
245 ;;;
246 ;;; The list type restriction does not assure that the result will be a
247 ;;; multi-dimensional array. But the lack of
248 ;;;
249 (deftransform make-array ((dims &key initial-element element-type)
250 (list &rest *))
251 (unless (or (null element-type) (constant-continuation-p element-type))
252 (give-up "Element-type not constant; cannot open code array creation"))
253 (unless (constant-continuation-p dims)
254 (give-up "Dimension list not constant; cannot open code array creation"))
255 (let ((dims (continuation-value dims)))
256 (unless (every #'integerp dims)
257 (give-up "Dimension list contains something other than an integer: ~S"
258 dims))
259 (if (= (length dims) 1)
260 `(make-array ',(car dims)
261 ,@(when initial-element
262 '(:initial-element initial-element))
263 ,@(when element-type
264 '(:element-type element-type)))
265 (let* ((total-size (reduce #'* dims))
266 (rank (length dims))
267 (spec `(simple-array
268 ,(cond ((null element-type) t)
269 ((constant-continuation-p element-type)
270 (continuation-value element-type))
271 (t '*))
272 ,(make-list rank :initial-element '*))))
273 `(let ((header (make-array-header vm:simple-array-type ,rank)))
274 (setf (%array-fill-pointer header) ,total-size)
275 (setf (%array-fill-pointer-p header) nil)
276 (setf (%array-available-elements header) ,total-size)
277 (setf (%array-data-vector header)
278 (make-array ,total-size
279 ,@(when element-type
280 '(:element-type element-type))
281 ,@(when initial-element
282 '(:initial-element initial-element))))
283 (setf (%array-displaced-p header) nil)
284 ,@(let ((axis -1))
285 (mapcar #'(lambda (dim)
286 `(setf (%array-dimension header ,(incf axis))
287 ,dim))
288 dims))
289 (truly-the ,spec header))))))
290
291
292 ;;;; Random properties of arrays.
293
294 ;;; Transforms for various random array properties. If the property is know
295 ;;; at compile time because of a type spec, use that constant value.
296
297 ;;; ARRAY-RANK -- transform.
298 ;;;
299 ;;; If we can tell the rank from the type info, use it instead.
300 ;;;
301 (deftransform array-rank ((array))
302 (let ((array-type (continuation-type array)))
303 (unless (array-type-p array-type)
304 (give-up))
305 (let ((dims (array-type-dimensions array-type)))
306 (if (not (listp dims))
307 (give-up "Array rank not known at compile time: ~S" dims)
308 (length dims)))))
309
310 ;;; ARRAY-DIMENSION -- transform.
311 ;;;
312 ;;; If we know the dimensions at compile time, just use it. Otherwise, if
313 ;;; we can tell that the axis is in bounds, convert to %array-dimension
314 ;;; (which just indirects the array header) or length (if it's simple and a
315 ;;; vector).
316 ;;;
317 (deftransform array-dimension ((array axis)
318 (array index))
319 (unless (constant-continuation-p axis)
320 (give-up "Axis not constant."))
321 (let ((array-type (continuation-type array))
322 (axis (continuation-value axis)))
323 (unless (array-type-p array-type)
324 (give-up))
325 (let ((dims (array-type-dimensions array-type)))
326 (unless (listp dims)
327 (give-up
328 "Array dimensions unknown, must call array-dimension at runtime."))
329 (unless (> (length dims) axis)
330 (abort-transform "Array has dimensions ~S, ~D is too large."
331 dims axis))
332 (let ((dim (nth axis dims)))
333 (cond ((integerp dim)
334 dim)
335 ((= (length dims) 1)
336 (ecase (array-type-complexp array-type)
337 ((t)
338 '(%array-dimension array 0))
339 ((nil)
340 '(length array))
341 (*
342 (give-up "Can't tell if array is simple."))))
343 (t
344 '(%array-dimension array axis)))))))
345
346 ;;; LENGTH -- transform.
347 ;;;
348 ;;; If the length has been declared and it's simple, just return it.
349 ;;;
350 (deftransform length ((vector)
351 ((simple-array * (*))))
352 (let ((type (continuation-type vector)))
353 (unless (array-type-p type)
354 (give-up))
355 (let ((dims (array-type-dimensions type)))
356 (unless (and (listp dims) (integerp (car dims)))
357 (give-up "Vector length unknown, must call length at runtime."))
358 (car dims))))
359
360 ;;; LENGTH -- transform.
361 ;;;
362 ;;; All vectors can get their length by using vector-length. If it's simple,
363 ;;; it will extract the length slot from the vector. It it's complex, it will
364 ;;; extract the fill pointer slot from the array header.
365 ;;;
366 (deftransform length ((vector) (vector))
367 '(vector-length vector))
368
369
370 ;;; If a simple array with known dimensions, then vector-length is a
371 ;;; compile-time constant.
372 ;;;
373 (deftransform vector-length ((vector) ((simple-array * (*))))
374 (let ((vtype (continuation-type vector)))
375 (if (array-type-p vtype)
376 (let ((dim (first (array-type-dimensions vtype))))
377 (when (eq dim '*) (give-up))
378 dim)
379 (give-up))))
380
381
382 ;;; ARRAY-TOTAL-SIZE -- transform.
383 ;;;
384 ;;; Again, if we can tell the results from the type, just use it. Otherwise,
385 ;;; if we know the rank, convert into a computation based on array-dimension.
386 ;;; We can wrap a truly-the index around the multiplications because we know
387 ;;; that the total size must be an index.
388 ;;;
389 (deftransform array-total-size ((array)
390 (array))
391 (let ((array-type (continuation-type array)))
392 (unless (array-type-p array-type)
393 (give-up))
394 (let ((dims (array-type-dimensions array-type)))
395 (unless (listp dims)
396 (give-up "Can't tell the rank at compile time."))
397 (if (member '* dims)
398 (do ((form 1 `(truly-the index
399 (* (array-dimension array ,i) ,form)))
400 (i 0 (1+ i)))
401 ((= i (length dims)) form))
402 (reduce #'* dims)))))
403
404 ;;; ARRAY-HAS-FILL-POINTER-P -- transform.
405 ;;;
406 ;;; Only complex vectors have fill pointers.
407 ;;;
408 (deftransform array-has-fill-pointer-p ((array))
409 (let ((array-type (continuation-type array)))
410 (unless (array-type-p array-type)
411 (give-up))
412 (let ((dims (array-type-dimensions array-type)))
413 (if (and (listp dims) (not (= (length dims) 1)))
414 nil
415 (ecase (array-type-complexp array-type)
416 ((t)
417 t)
418 ((nil)
419 nil)
420 (*
421 (give-up "Array type ambiguous; must call ~
422 array-has-fill-pointer-p at runtime.")))))))
423
424 ;;; %CHECK-BOUND -- transform.
425 ;;;
426 ;;; Primitive used to verify indicies into arrays. If we can tell at
427 ;;; compile-time or we are generating unsafe code, don't bother with the VOP.
428 ;;;
429 (deftransform %check-bound ((array dimension index))
430 (unless (constant-continuation-p dimension)
431 (give-up))
432 (let ((dim (continuation-value dimension)))
433 `(the (integer 0 ,dim) index)))
434 ;;;
435 (deftransform %check-bound ((array dimension index) * *
436 :policy (and (> speed safety) (= safety 0)))
437 'index)
438
439
440 ;;; WITH-ROW-MAJOR-INDEX -- internal.
441 ;;;
442 ;;; Handy macro for computing the row-major index given a set of indices. We
443 ;;; wrap each index with a call to %check-bound to assure that everything
444 ;;; works out correctly. We can wrap all the interior arith with truly-the
445 ;;; index because we know the the resultant row-major index must be an index.
446 ;;;
447 (eval-when (compile eval)
448 ;;;
449 (defmacro with-row-major-index ((array indices index &optional new-value)
450 &rest body)
451 `(let (n-indices dims)
452 (dotimes (i (length ,indices))
453 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
454 (push (make-symbol (format nil "DIM-~D" i)) dims))
455 (setf n-indices (nreverse n-indices))
456 (setf dims (nreverse dims))
457 `(lambda (,',array ,@n-indices ,@',(when new-value (list new-value)))
458 (let* (,@(let ((,index -1))
459 (mapcar #'(lambda (name)
460 `(,name (array-dimension ,',array
461 ,(incf ,index))))
462 dims))
463 (,',index
464 ,(if (null dims)
465 0
466 (do* ((dims dims (cdr dims))
467 (indices n-indices (cdr indices))
468 (last-dim nil (car dims))
469 (form `(%check-bound ,',array
470 ,(car dims)
471 ,(car indices))
472 `(truly-the index
473 (+ (truly-the index
474 (* ,form
475 ,last-dim))
476 (%check-bound
477 ,',array
478 ,(car dims)
479 ,(car indices))))))
480 ((null (cdr dims)) form)))))
481 ,',@body))))
482 ;;;
483 ); eval-when
484
485 ;;; ARRAY-ROW-MAJOR-INDEX -- transform.
486 ;;;
487 ;;; Just return the index after computing it.
488 ;;;
489 (deftransform array-row-major-index ((array &rest indices))
490 (with-row-major-index (array indices index)
491 index))
492
493
494
495 ;;;; Array accessors:
496
497 ;;; SVREF, %SVSET, SCHAR, %SCHARSET, CHAR,
498 ;;; %CHARSET, SBIT, %SBITSET, BIT, %BITSET
499 ;;; -- source transforms.
500 ;;;
501 ;;; We convert all typed array accessors into aref and %aset with type
502 ;;; assertions on the array.
503 ;;;
504 (macrolet ((frob (reffer setter type)
505 `(progn
506 (def-source-transform ,reffer (a &rest i)
507 (if *byte-compiling*
508 (values nil t)
509 `(aref (the ,',type ,a) ,@i)))
510 (def-source-transform ,setter (a &rest i)
511 (if *byte-compiling*
512 (values nil t)
513 `(%aset (the ,',type ,a) ,@i))))))
514 (frob svref %svset simple-vector)
515 (frob schar %scharset simple-string)
516 (frob char %charset string)
517 (frob sbit %sbitset (simple-array bit))
518 (frob bit %bitset (array bit)))
519
520 ;;; AREF, %ASET -- transform.
521 ;;;
522 ;;; Convert into a data-vector-ref (or set) with the set of indices replaced
523 ;;; with the an expression for the row major index.
524 ;;;
525 (deftransform aref ((array &rest indices))
526 (with-row-major-index (array indices index)
527 (data-vector-ref array index)))
528 ;;;
529 (deftransform %aset ((array &rest stuff))
530 (let ((indices (butlast stuff)))
531 (with-row-major-index (array indices index new-value)
532 (data-vector-set array index new-value))))
533
534 ;;; ROW-MAJOR-AREF, %SET-ROW-MAJOR-AREF -- transform.
535 ;;;
536 ;;; Just convert into a data-vector-ref (or set) after checking that the
537 ;;; index is inside the array total size.
538 ;;;
539 (deftransform row-major-aref ((array index))
540 `(data-vector-ref array (%check-bound array (array-total-size array) index)))
541 ;;;
542 (deftransform %set-row-major-aref ((array index new-value))
543 `(data-vector-set array
544 (%check-bound array (array-total-size array) index)
545 new-value))
546
547
548 ;;;; Bit-vector array operation canonicalization:
549 ;;;
550 ;;; We convert all bit-vector operations to have the result array specified.
551 ;;; This allows any result allocation to be open-coded, and eliminates the need
552 ;;; for any VM-dependent transforms to handle these cases.
553
554 (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
555 bit-andc2 bit-orc1 bit-orc2))
556 ;;
557 ;; Make a result array if result is NIL or unsupplied.
558 (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
559 '(bit-vector bit-vector &optional null) '*
560 :eval-name t :policy (>= speed space))
561 `(,fun bit-array-1 bit-array-2
562 (make-array (length bit-array-1) :element-type 'bit)))
563 ;;
564 ;; If result its T, make it the first arg.
565 (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
566 '(bit-vector bit-vector (member t)) '*
567 :eval-name t)
568 `(,fun bit-array-1 bit-array-2 bit-array-1)))
569
570 ;;; Similar for BIT-NOT, but there is only one arg...
571 ;;;
572 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
573 (bit-vector &optional null) *
574 :policy (>= speed space))
575 '(bit-not bit-array-1
576 (make-array (length bit-array-1) :element-type 'bit)))
577 ;;;
578 (deftransform bit-not ((bit-array-1 result-bit-array)
579 (bit-vector (constant-argument t)))
580 '(bit-not bit-array-1 bit-array-1)))

  ViewVC Help
Powered by ViewVC 1.1.5