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

  ViewVC Help
Powered by ViewVC 1.1.5