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

  ViewVC Help
Powered by ViewVC 1.1.5