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

  ViewVC Help
Powered by ViewVC 1.1.5