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

  ViewVC Help
Powered by ViewVC 1.1.5