/[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.14 - (hide annotations)
Tue Nov 12 14:14:14 1991 UTC (22 years, 5 months ago) by ram
Branch: MAIN
Changes since 1.13: +3 -5 lines
Quoted arg/result types in :EVAL-NAME DEFTRANSFORMs.
1 wlott 1.1 ;;; -*- Package: C; Log: C.Log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.9 ;;; 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 ram 1.14 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/array-tran.lisp,v 1.14 1991/11/12 14:14:14 ram Exp $")
11 ram 1.9 ;;;
12 wlott 1.1 ;;; **********************************************************************
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 wlott 1.3 ;;; ASSERT-ARRAY-RANK -- internal
24 wlott 1.1 ;;;
25     ;;; Array operations that use a specific number of indices implicitly assert
26     ;;; that the array is of that rank.
27     ;;;
28 wlott 1.3 (defun assert-array-rank (array rank)
29     (assert-continuation-type
30 wlott 1.1 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 wlott 1.4 (assert-array-rank array (length indices))
77 wlott 1.1 (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 ram 1.10
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 wlott 1.1
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 wlott 1.11 (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 wlott 1.1
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     (let ((len (length elements))
161     (n -1))
162     (once-only ((n-vec `(make-array ,len)))
163     `(progn
164     ,@(mapcar #'(lambda (el)
165     (once-only ((n-val el))
166     `(locally (declare (optimize (safety 0)))
167     (setf (svref ,n-vec ,(incf n)) ,n-val))))
168     elements)
169     ,n-vec))))
170    
171    
172     ;;; MAKE-STRING -- source-transform.
173     ;;;
174     ;;; Just convert it into a make-array.
175     ;;;
176     (def-source-transform make-string (length &key (initial-element #\NULL))
177 wlott 1.13 `(make-array (the index ,length)
178 wlott 1.12 :element-type 'base-char
179 wlott 1.5 :initial-element ,initial-element))
180 wlott 1.1
181     (defconstant array-info
182 wlott 1.12 '((base-char #\NULL 8 vm:simple-string-type)
183 wlott 1.1 (single-float 0.0s0 32 vm:simple-array-single-float-type)
184     (double-float 0.0d0 64 vm:simple-array-double-float-type)
185     (bit 0 1 vm:simple-bit-vector-type)
186     ((unsigned-byte 2) 0 2 vm:simple-array-unsigned-byte-2-type)
187     ((unsigned-byte 4) 0 4 vm:simple-array-unsigned-byte-4-type)
188     ((unsigned-byte 8) 0 8 vm:simple-array-unsigned-byte-8-type)
189     ((unsigned-byte 16) 0 16 vm:simple-array-unsigned-byte-16-type)
190     ((unsigned-byte 32) 0 32 vm:simple-array-unsigned-byte-32-type)
191     (t 0 32 vm:simple-vector-type)))
192    
193     ;;; MAKE-ARRAY -- source-transform.
194     ;;;
195     ;;; The integer type restriction on the length assures that it will be a
196     ;;; vector. The lack of adjustable, fill-pointer, and displaced-to keywords
197     ;;; assures that it will be simple.
198     ;;;
199     (deftransform make-array ((length &key initial-element element-type)
200     (integer &rest *))
201     (let* ((eltype (cond ((not element-type) t)
202     ((not (constant-continuation-p element-type))
203     (give-up "Element-Type is not constant."))
204     (t
205     (continuation-value element-type))))
206     (len (if (constant-continuation-p length)
207     (continuation-value length)
208     '*))
209     (spec `(simple-array ,eltype (,len)))
210     (eltype-type (specifier-type eltype)))
211     (multiple-value-bind
212     (default-initial-element element-size typecode)
213     (dolist (info array-info
214     (give-up "Cannot open-code creation of ~S" spec))
215     (when (csubtypep eltype-type (specifier-type (car info)))
216     (return (values-list (cdr info)))))
217 wlott 1.6 (let* ((nwords-form
218     (if (>= element-size vm:word-bits)
219     `(* length ,(/ element-size vm:word-bits))
220     (let ((elements-per-word (/ 32 element-size)))
221     `(truncate (+ length
222     ,(if (eq 'vm:simple-string-type typecode)
223     elements-per-word
224     (1- elements-per-word)))
225     ,elements-per-word))))
226     (constructor
227     `(truly-the ,spec
228     (allocate-vector ,typecode length ,nwords-form))))
229 wlott 1.1 (values
230     (if (and default-initial-element
231     (or (null initial-element)
232     (and (constant-continuation-p initial-element)
233     (eql (continuation-value initial-element)
234     default-initial-element))))
235     constructor
236     `(truly-the ,spec (fill ,constructor initial-element)))
237     '((declare (type index length))))))))
238    
239     ;;; MAKE-ARRAY -- transform.
240     ;;;
241     ;;; The list type restriction does not assure that the result will be a
242     ;;; multi-dimensional array. But the lack of
243     ;;;
244     (deftransform make-array ((dims &key initial-element element-type)
245     (list &rest *))
246     (unless (or (null element-type) (constant-continuation-p element-type))
247     (give-up "Element-type not constant; cannot open code array creation"))
248     (unless (constant-continuation-p dims)
249     (give-up "Dimension list not constant; cannot open code array creation"))
250     (let ((dims (continuation-value dims)))
251     (unless (every #'integerp dims)
252 wlott 1.6 (give-up "Dimension list contains something other than an integer: ~S"
253 wlott 1.1 dims))
254     (if (= (length dims) 1)
255     `(make-array ',(car dims)
256     ,@(when initial-element
257     '(:initial-element initial-element))
258     ,@(when element-type
259     '(:element-type element-type)))
260     (let* ((total-size (reduce #'* dims))
261     (rank (length dims))
262     (spec `(simple-array
263     ,(cond ((null element-type) t)
264     ((constant-continuation-p element-type)
265     (continuation-value element-type))
266     (t '*))
267     ,(make-list rank :initial-element '*))))
268     `(let ((header (make-array-header vm:simple-array-type ,rank)))
269     (setf (%array-fill-pointer header) ,total-size)
270     (setf (%array-fill-pointer-p header) nil)
271     (setf (%array-available-elements header) ,total-size)
272     (setf (%array-data-vector header)
273     (make-array ,total-size
274     ,@(when element-type
275     '(:element-type element-type))
276     ,@(when initial-element
277     '(:initial-element initial-element))))
278     (setf (%array-displaced-p header) nil)
279     ,@(let ((axis -1))
280     (mapcar #'(lambda (dim)
281     `(setf (%array-dimension header ,(incf axis))
282     ,dim))
283     dims))
284     (truly-the ,spec header))))))
285    
286    
287     ;;;; Random properties of arrays.
288    
289     ;;; Transforms for various random array properties. If the property is know
290     ;;; at compile time because of a type spec, use that constant value.
291    
292     ;;; ARRAY-RANK -- transform.
293     ;;;
294     ;;; If we can tell the rank from the type info, use it instead.
295     ;;;
296     (deftransform array-rank ((array))
297     (let ((array-type (continuation-type array)))
298     (unless (array-type-p array-type)
299     (give-up))
300     (let ((dims (array-type-dimensions array-type)))
301     (if (not (listp dims))
302     (give-up "Array rank not known at compile time: ~S" dims)
303     (length dims)))))
304    
305     ;;; ARRAY-DIMENSION -- transform.
306     ;;;
307     ;;; If we know the dimensions at compile time, just use it. Otherwise, if
308     ;;; we can tell that the axis is in bounds, convert to %array-dimension
309     ;;; (which just indirects the array header) or length (if it's simple and a
310     ;;; vector).
311     ;;;
312     (deftransform array-dimension ((array axis)
313     (array index))
314     (unless (constant-continuation-p axis)
315     (give-up "Axis not constant."))
316     (let ((array-type (continuation-type array))
317     (axis (continuation-value axis)))
318     (unless (array-type-p array-type)
319     (give-up))
320     (let ((dims (array-type-dimensions array-type)))
321     (unless (listp dims)
322     (give-up
323     "Array dimensions unknown, must call array-dimension at runtime."))
324     (unless (> (length dims) axis)
325     (abort-transform "Array has dimensions ~S, ~D is too large."
326     dims axis))
327     (let ((dim (nth axis dims)))
328     (cond ((integerp dim)
329     dim)
330     ((= (length dims) 1)
331     (ecase (array-type-complexp array-type)
332     ((t)
333     '(%array-dimension array 0))
334     ((nil)
335     '(length array))
336     (*
337     (give-up "Can't tell if array is simple."))))
338     (t
339     '(%array-dimension array axis)))))))
340    
341     ;;; LENGTH -- transform.
342     ;;;
343     ;;; If the length has been declared and it's simple, just return it.
344     ;;;
345     (deftransform length ((vector)
346     ((simple-array * (*))))
347     (let ((type (continuation-type vector)))
348     (unless (array-type-p type)
349     (give-up))
350     (let ((dims (array-type-dimensions type)))
351     (unless (and (listp dims) (integerp (car dims)))
352     (give-up "Vector length unknown, must call length at runtime."))
353     (car dims))))
354    
355     ;;; LENGTH -- transform.
356     ;;;
357     ;;; All vectors can get their length by using vector-length. If it's simple,
358     ;;; it will extract the length slot from the vector. It it's complex, it will
359     ;;; extract the fill pointer slot from the array header.
360     ;;;
361     (deftransform length ((vector) (vector))
362     '(vector-length vector))
363    
364 ram 1.7
365     ;;; If a simple array with known dimensions, then vector-length is a
366     ;;; compile-time constant.
367     ;;;
368     (deftransform vector-length ((vector) ((simple-array * (*))))
369     (let ((vtype (continuation-type vector)))
370     (if (array-type-p vtype)
371     (let ((dim (first (array-type-dimensions vtype))))
372     (when (eq dim '*) (give-up))
373     dim)
374     (give-up))))
375    
376    
377 wlott 1.1 ;;; ARRAY-TOTAL-SIZE -- transform.
378     ;;;
379     ;;; Again, if we can tell the results from the type, just use it. Otherwise,
380     ;;; if we know the rank, convert into a computation based on array-dimension.
381     ;;; We can wrap a truly-the index around the multiplications because we know
382     ;;; that the total size must be an index.
383     ;;;
384     (deftransform array-total-size ((array)
385     (array))
386     (let ((array-type (continuation-type array)))
387     (unless (array-type-p array-type)
388     (give-up))
389     (let ((dims (array-type-dimensions array-type)))
390     (unless (listp dims)
391 wlott 1.2 (give-up "Can't tell the rank at compile time."))
392     (if (member '* dims)
393     (do ((form 1 `(truly-the index
394     (* (array-dimension array ,i) ,form)))
395     (i 0 (1+ i)))
396     ((= i (length dims)) form))
397     (reduce #'* dims)))))
398 wlott 1.1
399     ;;; ARRAY-HAS-FILL-POINTER-P -- transform.
400     ;;;
401     ;;; Only complex vectors have fill pointers.
402     ;;;
403     (deftransform array-has-fill-pointer-p ((array))
404     (let ((array-type (continuation-type array)))
405     (unless (array-type-p array-type)
406     (give-up))
407     (let ((dims (array-type-dimensions array-type)))
408     (if (and (listp dims) (not (= (length dims) 1)))
409     nil
410     (ecase (array-type-complexp array-type)
411     ((t)
412     t)
413     ((nil)
414     nil)
415     (*
416     (give-up "Array type ambiguous; must call ~
417     array-has-fill-pointer-p at runtime.")))))))
418    
419     ;;; %CHECK-BOUND -- transform.
420     ;;;
421     ;;; Primitive used to verify indicies into arrays. If we can tell at
422     ;;; compile-time or we are generating unsafe code, don't bother with the VOP.
423     ;;;
424     (deftransform %check-bound ((array dimension index))
425     (unless (constant-continuation-p dimension)
426     (give-up))
427     (let ((dim (continuation-value dimension)))
428     `(the (integer 0 ,dim) index)))
429     ;;;
430     (deftransform %check-bound ((array dimension index) * *
431     :policy (and (> speed safety) (= safety 0)))
432     'index)
433    
434    
435     ;;; WITH-ROW-MAJOR-INDEX -- internal.
436     ;;;
437     ;;; Handy macro for computing the row-major index given a set of indices. We
438     ;;; wrap each index with a call to %check-bound to assure that everything
439     ;;; works out correctly. We can wrap all the interior arith with truly-the
440     ;;; index because we know the the resultant row-major index must be an index.
441     ;;;
442     (eval-when (compile eval)
443     ;;;
444     (defmacro with-row-major-index ((array indices index &optional new-value)
445     &rest body)
446     `(let (n-indices dims)
447     (dotimes (i (length ,indices))
448     (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
449     (push (make-symbol (format nil "DIM-~D" i)) dims))
450     (setf n-indices (nreverse n-indices))
451     (setf dims (nreverse dims))
452     `(lambda (,',array ,@n-indices ,@',(when new-value (list new-value)))
453     (let* (,@(let ((,index -1))
454     (mapcar #'(lambda (name)
455     `(,name (array-dimension ,',array
456     ,(incf ,index))))
457     dims))
458     (,',index
459     ,(if (null dims)
460     0
461     (do* ((dims dims (cdr dims))
462     (indices n-indices (cdr indices))
463     (last-dim nil (car dims))
464     (form `(%check-bound ,',array
465     ,(car dims)
466     ,(car indices))
467     `(truly-the index
468     (+ (truly-the index
469     (* ,form
470     ,last-dim))
471     (%check-bound
472     ,',array
473     ,(car dims)
474     ,(car indices))))))
475     ((null (cdr dims)) form)))))
476     ,',@body))))
477     ;;;
478     ); eval-when
479    
480     ;;; ARRAY-ROW-MAJOR-INDEX -- transform.
481     ;;;
482     ;;; Just return the index after computing it.
483     ;;;
484     (deftransform array-row-major-index ((array &rest indices))
485     (with-row-major-index (array indices index)
486     index))
487    
488    
489    
490     ;;;; Array accessors:
491    
492     ;;; SVREF, %SVSET, SCHAR, %SCHARSET, CHAR,
493     ;;; %CHARSET, SBIT, %SBITSET, BIT, %BITSET
494     ;;; -- source transforms.
495     ;;;
496     ;;; We convert all typed array accessors into aref and %aset with type
497     ;;; assertions on the array.
498     ;;;
499     (macrolet ((frob (reffer setter type)
500     `(progn
501     (def-source-transform ,reffer (a &rest i)
502     `(aref (the ,',type ,a) ,@i))
503     (def-source-transform ,setter (a &rest i)
504     `(%aset (the ,',type ,a) ,@i)))))
505     (frob svref %svset simple-vector)
506     (frob schar %scharset simple-string)
507     (frob char %charset string)
508     (frob sbit %sbitset (simple-array bit))
509     (frob bit %bitset (array bit)))
510    
511     ;;; AREF, %ASET -- transform.
512     ;;;
513     ;;; Convert into a data-vector-ref (or set) with the set of indices replaced
514     ;;; with the an expression for the row major index.
515     ;;;
516     (deftransform aref ((array &rest indices))
517     (with-row-major-index (array indices index)
518     (data-vector-ref array index)))
519     ;;;
520     (deftransform %aset ((array &rest stuff))
521     (let ((indices (butlast stuff)))
522     (with-row-major-index (array indices index new-value)
523     (data-vector-set array index new-value))))
524    
525     ;;; ROW-MAJOR-AREF, %SET-ROW-MAJOR-AREF -- transform.
526     ;;;
527     ;;; Just convert into a data-vector-ref (or set) after checking that the
528     ;;; index is inside the array total size.
529     ;;;
530     (deftransform row-major-aref ((array index))
531     `(data-vector-ref array (%check-bound array (array-total-size array) index)))
532     ;;;
533     (deftransform %set-row-major-aref ((array index new-value))
534     `(data-vector-set array
535     (%check-bound array (array-total-size array) index)
536     new-value))
537 ram 1.7
538    
539     ;;;; Bit-vector array operation canonicalization:
540     ;;;
541     ;;; We convert all bit-vector operations to have the result array specified.
542     ;;; This allows any result allocation to be open-coded, and eliminates the need
543     ;;; for any VM-dependent transforms to handle these cases.
544    
545     (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
546     bit-andc2 bit-orc1 bit-orc2))
547     ;;
548     ;; Make a result array if result is NIL or unsupplied.
549     (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
550 ram 1.14 '(bit-vector bit-vector &optional null) '*
551 ram 1.7 :eval-name t :policy (>= speed space))
552     `(,fun bit-array-1 bit-array-2
553     (make-array (length bit-array-1) :element-type 'bit)))
554     ;;
555     ;; If result its T, make it the first arg.
556     (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
557 ram 1.14 '(bit-vector bit-vector (member t)) '*
558 ram 1.7 :eval-name t)
559     `(,fun bit-array-1 bit-array-2 bit-array-1)))
560    
561     ;;; Similar for BIT-NOT, but there is only one arg...
562     ;;;
563     (deftransform bit-not ((bit-array-1 &optional result-bit-array)
564     (bit-vector &optional null) *
565     :policy (>= speed space))
566     '(bit-not bit-array-1
567     (make-array (length bit-array-1) :element-type 'bit)))
568     ;;;
569     (deftransform bit-not ((bit-array-1 result-bit-array)
570     (bit-vector (constant-argument t)))
571     '(bit-not bit-array-1 bit-array-1)))

  ViewVC Help
Powered by ViewVC 1.1.5