0.9.4.54:
[sbcl.git] / src / compiler / typetran.lisp
1 ;;;; This file contains stuff that implements the portable IR1
2 ;;;; semantics of type tests and coercion. The main thing we do is
3 ;;;; convert complex type operations into simpler code that can be
4 ;;;; compiled inline.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
14
15 (in-package "SB!C")
16 \f
17 ;;;; type predicate translation
18 ;;;;
19 ;;;; We maintain a bidirectional association between type predicates
20 ;;;; and the tested type. The presence of a predicate in this
21 ;;;; association implies that it is desirable to implement tests of
22 ;;;; this type using the predicate. These are either predicates that
23 ;;;; the back end is likely to have special knowledge about, or
24 ;;;; predicates so complex that the only reasonable implentation is
25 ;;;; via function call.
26 ;;;;
27 ;;;; Some standard types (such as SEQUENCE) are best tested by letting
28 ;;;; the TYPEP source transform do its thing with the expansion. These
29 ;;;; types (and corresponding predicates) are not maintained in this
30 ;;;; association. In this case, there need not be any predicate
31 ;;;; function unless it is required by the Common Lisp specification.
32 ;;;;
33 ;;;; The mapping between predicates and type structures is considered
34 ;;;; part of the backend; different backends can support different
35 ;;;; sets of predicates.
36
37 ;;; Establish an association between the type predicate NAME and the
38 ;;; corresponding TYPE. This causes the type predicate to be
39 ;;; recognized for purposes of optimization.
40 (defmacro define-type-predicate (name type)
41   `(%define-type-predicate ',name ',type))
42 (defun %define-type-predicate (name specifier)
43   (let ((type (specifier-type specifier)))
44     (setf (gethash name *backend-predicate-types*) type)
45     (setf *backend-type-predicates*
46           (cons (cons type name)
47                 (remove name *backend-type-predicates*
48                         :key #'cdr)))
49     (%deftransform name '(function (t) *) #'fold-type-predicate)
50     name))
51 \f
52 ;;;; IR1 transforms
53
54 ;;; If we discover the type argument is constant during IR1
55 ;;; optimization, then give the source transform another chance. The
56 ;;; source transform can't pass, since we give it an explicit
57 ;;; constant. At worst, it will convert to %TYPEP, which will prevent
58 ;;; spurious attempts at transformation (and possible repeated
59 ;;; warnings.)
60 (deftransform typep ((object type))
61   (unless (constant-lvar-p type)
62     (give-up-ir1-transform "can't open-code test of non-constant type"))
63   `(typep object ',(lvar-value type)))
64
65 ;;; If the lvar OBJECT definitely is or isn't of the specified
66 ;;; type, then return T or NIL as appropriate. Otherwise quietly
67 ;;; GIVE-UP-IR1-TRANSFORM.
68 (defun ir1-transform-type-predicate (object type)
69   (declare (type lvar object) (type ctype type))
70   (let ((otype (lvar-type object)))
71     (cond ((not (types-equal-or-intersect otype type))
72            nil)
73           ((csubtypep otype type)
74            t)
75           ((eq type *empty-type*)
76            nil)
77           (t
78            (give-up-ir1-transform)))))
79
80 ;;; Flush %TYPEP tests whose result is known at compile time.
81 (deftransform %typep ((object type))
82   (unless (constant-lvar-p type)
83     (give-up-ir1-transform))
84   (ir1-transform-type-predicate
85    object
86    (ir1-transform-specifier-type (lvar-value type))))
87
88 ;;; This is the IR1 transform for simple type predicates. It checks
89 ;;; whether the single argument is known to (not) be of the
90 ;;; appropriate type, expanding to T or NIL as appropriate.
91 (deftransform fold-type-predicate ((object) * * :node node :defun-only t)
92   (let ((ctype (gethash (leaf-source-name
93                          (ref-leaf
94                           (lvar-uses
95                            (basic-combination-fun node))))
96                         *backend-predicate-types*)))
97     (aver ctype)
98     (ir1-transform-type-predicate object ctype)))
99
100 ;;; If FIND-CLASS is called on a constant class, locate the CLASS-CELL
101 ;;; at load time.
102 (deftransform find-classoid ((name) ((constant-arg symbol)) *)
103   (let* ((name (lvar-value name))
104          (cell (find-classoid-cell name)))
105     `(or (classoid-cell-classoid ',cell)
106          (error "class not yet defined: ~S" name))))
107 \f
108 ;;;; standard type predicates, i.e. those defined in package COMMON-LISP,
109 ;;;; plus at least one oddball (%INSTANCEP)
110 ;;;;
111 ;;;; Various other type predicates (e.g. low-level representation
112 ;;;; stuff like SIMPLE-ARRAY-SINGLE-FLOAT-P) are defined elsewhere.
113
114 ;;; FIXME: This function is only called once, at top level. Why not
115 ;;; just expand all its operations into toplevel code?
116 (defun !define-standard-type-predicates ()
117   (define-type-predicate arrayp array)
118   ; (The ATOM predicate is handled separately as (NOT CONS).)
119   (define-type-predicate bit-vector-p bit-vector)
120   (define-type-predicate characterp character)
121   (define-type-predicate compiled-function-p compiled-function)
122   (define-type-predicate complexp complex)
123   (define-type-predicate complex-rational-p (complex rational))
124   (define-type-predicate complex-float-p (complex float))
125   (define-type-predicate consp cons)
126   (define-type-predicate floatp float)
127   (define-type-predicate functionp function)
128   (define-type-predicate integerp integer)
129   (define-type-predicate keywordp keyword)
130   (define-type-predicate listp list)
131   (define-type-predicate null null)
132   (define-type-predicate numberp number)
133   (define-type-predicate rationalp rational)
134   (define-type-predicate realp real)
135   (define-type-predicate simple-bit-vector-p simple-bit-vector)
136   (define-type-predicate simple-string-p simple-string)
137   (define-type-predicate simple-vector-p simple-vector)
138   (define-type-predicate stringp string)
139   (define-type-predicate %instancep instance)
140   (define-type-predicate funcallable-instance-p funcallable-instance)
141   (define-type-predicate symbolp symbol)
142   (define-type-predicate vectorp vector))
143 (!define-standard-type-predicates)
144 \f
145 ;;;; transforms for type predicates not implemented primitively
146 ;;;;
147 ;;;; See also VM dependent transforms.
148
149 (define-source-transform atom (x)
150   `(not (consp ,x)))
151 #!+sb-unicode
152 (define-source-transform base-char-p (x)
153   `(typep ,x 'base-char))
154 \f
155 ;;;; TYPEP source transform
156
157 ;;; Return a form that tests the variable N-OBJECT for being in the
158 ;;; binds specified by TYPE. BASE is the name of the base type, for
159 ;;; declaration. We make SAFETY locally 0 to inhibit any checking of
160 ;;; this assertion.
161 (defun transform-numeric-bound-test (n-object type base)
162   (declare (type numeric-type type))
163   (let ((low (numeric-type-low type))
164         (high (numeric-type-high type)))
165     `(locally
166        (declare (optimize (safety 0)))
167        (and ,@(when low
168                 (if (consp low)
169                     `((> (truly-the ,base ,n-object) ,(car low)))
170                     `((>= (truly-the ,base ,n-object) ,low))))
171             ,@(when high
172                 (if (consp high)
173                     `((< (truly-the ,base ,n-object) ,(car high)))
174                     `((<= (truly-the ,base ,n-object) ,high))))))))
175
176 ;;; Do source transformation of a test of a known numeric type. We can
177 ;;; assume that the type doesn't have a corresponding predicate, since
178 ;;; those types have already been picked off. In particular, CLASS
179 ;;; must be specified, since it is unspecified only in NUMBER and
180 ;;; COMPLEX. Similarly, we assume that COMPLEXP is always specified.
181 ;;;
182 ;;; For non-complex types, we just test that the number belongs to the
183 ;;; base type, and then test that it is in bounds. When CLASS is
184 ;;; INTEGER, we check to see whether the range is no bigger than
185 ;;; FIXNUM. If so, we check for FIXNUM instead of INTEGER. This allows
186 ;;; us to use fixnum comparison to test the bounds.
187 ;;;
188 ;;; For complex types, we must test for complex, then do the above on
189 ;;; both the real and imaginary parts. When CLASS is float, we need
190 ;;; only check the type of the realpart, since the format of the
191 ;;; realpart and the imagpart must be the same.
192 (defun source-transform-numeric-typep (object type)
193   (let* ((class (numeric-type-class type))
194          (base (ecase class
195                  (integer (containing-integer-type
196                            (if (numeric-type-complexp type)
197                                (modified-numeric-type type
198                                                       :complexp :real)
199                                type)))
200                  (rational 'rational)
201                  (float (or (numeric-type-format type) 'float))
202                  ((nil) 'real))))
203     (once-only ((n-object object))
204       (ecase (numeric-type-complexp type)
205         (:real
206          `(and (typep ,n-object ',base)
207                ,(transform-numeric-bound-test n-object type base)))
208         (:complex
209          `(and (complexp ,n-object)
210                ,(once-only ((n-real `(realpart (truly-the complex ,n-object)))
211                             (n-imag `(imagpart (truly-the complex ,n-object))))
212                   `(progn
213                      ,n-imag ; ignorable
214                      (and (typep ,n-real ',base)
215                           ,@(when (eq class 'integer)
216                               `((typep ,n-imag ',base)))
217                           ,(transform-numeric-bound-test n-real type base)
218                           ,(transform-numeric-bound-test n-imag type
219                                                          base))))))))))
220
221 ;;; Do the source transformation for a test of a hairy type. AND,
222 ;;; SATISFIES and NOT are converted into the obvious code. We convert
223 ;;; unknown types to %TYPEP, emitting an efficiency note if
224 ;;; appropriate.
225 (defun source-transform-hairy-typep (object type)
226   (declare (type hairy-type type))
227   (let ((spec (hairy-type-specifier type)))
228     (cond ((unknown-type-p type)
229            (when (policy *lexenv* (> speed inhibit-warnings))
230              (compiler-notify "can't open-code test of unknown type ~S"
231                               (type-specifier type)))
232            `(%typep ,object ',spec))
233           (t
234            (ecase (first spec)
235              (satisfies `(if (funcall #',(second spec) ,object) t nil))
236              ((not and)
237               (once-only ((n-obj object))
238                 `(,(first spec) ,@(mapcar (lambda (x)
239                                             `(typep ,n-obj ',x))
240                                           (rest spec))))))))))
241
242 (defun source-transform-negation-typep (object type)
243   (declare (type negation-type type))
244   (let ((spec (type-specifier (negation-type-type type))))
245     `(not (typep ,object ',spec))))
246
247 ;;; Do source transformation for TYPEP of a known union type. If a
248 ;;; union type contains LIST, then we pull that out and make it into a
249 ;;; single LISTP call.  Note that if SYMBOL is in the union, then LIST
250 ;;; will be a subtype even without there being any (member NIL).  We
251 ;;; currently just drop through to the general code in this case,
252 ;;; rather than trying to optimize it (but FIXME CSR 2004-04-05: it
253 ;;; wouldn't be hard to optimize it after all).
254 (defun source-transform-union-typep (object type)
255   (let* ((types (union-type-types type))
256          (type-cons (specifier-type 'cons))
257          (mtype (find-if #'member-type-p types))
258          (members (when mtype (member-type-members mtype))))
259     (if (and mtype
260              (memq nil members)
261              (memq type-cons types))
262         (once-only ((n-obj object))
263           `(or (listp ,n-obj)
264                (typep ,n-obj
265                       '(or ,@(mapcar #'type-specifier
266                                      (remove type-cons
267                                              (remove mtype types)))
268                         (member ,@(remove nil members))))))
269         (once-only ((n-obj object))
270           `(or ,@(mapcar (lambda (x)
271                            `(typep ,n-obj ',(type-specifier x)))
272                          types))))))
273
274 ;;; Do source transformation for TYPEP of a known intersection type.
275 (defun source-transform-intersection-typep (object type)
276   (once-only ((n-obj object))
277     `(and ,@(mapcar (lambda (x)
278                       `(typep ,n-obj ',(type-specifier x)))
279                     (intersection-type-types type)))))
280
281 ;;; If necessary recurse to check the cons type.
282 (defun source-transform-cons-typep (object type)
283   (let* ((car-type (cons-type-car-type type))
284          (cdr-type (cons-type-cdr-type type)))
285     (let ((car-test-p (not (type= car-type *universal-type*)))
286           (cdr-test-p (not (type= cdr-type *universal-type*))))
287       (if (and (not car-test-p) (not cdr-test-p))
288           `(consp ,object)
289           (once-only ((n-obj object))
290             `(and (consp ,n-obj)
291                   ,@(if car-test-p
292                         `((typep (car ,n-obj)
293                                  ',(type-specifier car-type))))
294                   ,@(if cdr-test-p
295                         `((typep (cdr ,n-obj)
296                                  ',(type-specifier cdr-type))))))))))
297
298 (defun source-transform-character-set-typep (object type)
299   (let ((pairs (character-set-type-pairs type)))
300     (if (and (= (length pairs) 1)
301             (= (caar pairs) 0)
302             (= (cdar pairs) (1- sb!xc:char-code-limit)))
303        `(characterp ,object)
304        (once-only ((n-obj object))
305          (let ((n-code (gensym "CODE")))
306            `(and (characterp ,n-obj)
307                  (let ((,n-code (sb!xc:char-code ,n-obj)))
308                    (or
309                     ,@(loop for pair in pairs
310                             collect
311                             `(<= ,(car pair) ,n-code ,(cdr pair)))))))))))
312
313 ;;; Return the predicate and type from the most specific entry in
314 ;;; *TYPE-PREDICATES* that is a supertype of TYPE.
315 (defun find-supertype-predicate (type)
316   (declare (type ctype type))
317   (let ((res nil)
318         (res-type nil))
319     (dolist (x *backend-type-predicates*)
320       (let ((stype (car x)))
321         (when (and (csubtypep type stype)
322                    (or (not res-type)
323                        (csubtypep stype res-type)))
324           (setq res-type stype)
325           (setq res (cdr x)))))
326     (values res res-type)))
327
328 ;;; Return forms to test that OBJ has the rank and dimensions
329 ;;; specified by TYPE, where STYPE is the type we have checked against
330 ;;; (which is the same but for dimensions and element type).
331 (defun test-array-dimensions (obj type stype)
332   (declare (type array-type type stype))
333   (let ((obj `(truly-the ,(type-specifier stype) ,obj))
334         (dims (array-type-dimensions type)))
335     (unless (or (eq dims '*)
336                 (equal dims (array-type-dimensions stype)))
337       (collect ((res))
338         (when (eq (array-type-dimensions stype) '*)
339           (res `(= (array-rank ,obj) ,(length dims))))
340         (do ((i 0 (1+ i))
341              (dim dims (cdr dim)))
342             ((null dim))
343           (let ((dim (car dim)))
344             (unless (eq dim '*)
345               (res `(= (array-dimension ,obj ,i) ,dim)))))
346         (res)))))
347
348 ;;; Return forms to test that OBJ has the element-type specified by
349 ;;; type specified by TYPE, where STYPE is the type we have checked
350 ;;; against (which is the same but for dimensions and element type).
351 (defun test-array-element-type (obj type stype)
352   (declare (type array-type type stype))
353   (let ((obj `(truly-the ,(type-specifier stype) ,obj))
354         (eltype (array-type-specialized-element-type type)))
355     (unless (type= eltype (array-type-specialized-element-type stype))
356       (with-unique-names (data)
357         `((do ((,data ,obj (%array-data-vector ,data)))
358               ((not (array-header-p ,data))
359                ;; KLUDGE: this isn't in fact maximally efficient,
360                ;; because though we know that DATA is a (SIMPLE-ARRAY *
361                ;; (*)), we will still check to see if the lowtag is
362                ;; appropriate.
363                (typep ,data
364                       '(simple-array ,(type-specifier eltype) (*))))))))))
365
366 ;;; If we can find a type predicate that tests for the type without
367 ;;; dimensions, then use that predicate and test for dimensions.
368 ;;; Otherwise, just do %TYPEP.
369 (defun source-transform-array-typep (obj type)
370   (multiple-value-bind (pred stype) (find-supertype-predicate type)
371     (if (and (array-type-p stype)
372              ;; (If the element type hasn't been defined yet, it's
373              ;; not safe to assume here that it will eventually
374              ;; have (UPGRADED-ARRAY-ELEMENT-TYPE type)=T, so punt.)
375              (not (unknown-type-p (array-type-element-type type)))
376              (eq (array-type-complexp stype) (array-type-complexp type)))
377         (once-only ((n-obj obj))
378           `(and (,pred ,n-obj)
379                 ,@(test-array-dimensions n-obj type stype)
380                 ,@(test-array-element-type n-obj type stype)))
381         `(%typep ,obj ',(type-specifier type)))))
382
383 ;;; Transform a type test against some instance type. The type test is
384 ;;; flushed if the result is known at compile time. If not properly
385 ;;; named, error. If sealed and has no subclasses, just test for
386 ;;; layout-EQ. If a structure then test for layout-EQ and then a
387 ;;; general test based on layout-inherits. If safety is important,
388 ;;; then we also check whether the layout for the object is invalid
389 ;;; and signal an error if so. Otherwise, look up the indirect
390 ;;; class-cell and call CLASS-CELL-TYPEP at runtime.
391 (deftransform %instance-typep ((object spec) (* *) * :node node)
392   (aver (constant-lvar-p spec))
393   (let* ((spec (lvar-value spec))
394          (class (specifier-type spec))
395          (name (classoid-name class))
396          (otype (lvar-type object))
397          (layout (let ((res (info :type :compiler-layout name)))
398                    (if (and res (not (layout-invalid res)))
399                        res
400                        nil))))
401     (cond
402       ;; Flush tests whose result is known at compile time.
403       ((not (types-equal-or-intersect otype class))
404        nil)
405       ((csubtypep otype class)
406        t)
407       ;; If not properly named, error.
408       ((not (and name (eq (find-classoid name) class)))
409        (compiler-error "can't compile TYPEP of anonymous or undefined ~
410                         class:~%  ~S"
411                        class))
412       (t
413         ;; Delay the type transform to give type propagation a chance.
414         (delay-ir1-transform node :constraint)
415
416        ;; Otherwise transform the type test.
417        (multiple-value-bind (pred get-layout)
418            (cond
419              ((csubtypep class (specifier-type 'funcallable-instance))
420               (values 'funcallable-instance-p '%funcallable-instance-layout))
421              ((csubtypep class (specifier-type 'instance))
422               (values '%instancep '%instance-layout))
423              (t
424               (values '(lambda (x) (declare (ignore x)) t) 'layout-of)))
425          (cond
426            ((and (eq (classoid-state class) :sealed) layout
427                  (not (classoid-subclasses class)))
428             ;; Sealed and has no subclasses.
429             (let ((n-layout (gensym)))
430               `(and (,pred object)
431                     (let ((,n-layout (,get-layout object)))
432                       ,@(when (policy *lexenv* (>= safety speed))
433                               `((when (layout-invalid ,n-layout)
434                                   (%layout-invalid-error object ',layout))))
435                       (eq ,n-layout ',layout)))))
436            ((and (typep class 'basic-structure-classoid) layout)
437             ;; structure type tests; hierarchical layout depths
438             (let ((depthoid (layout-depthoid layout))
439                   (n-layout (gensym)))
440               `(and (,pred object)
441                     (let ((,n-layout (,get-layout object)))
442                       ,@(when (policy *lexenv* (>= safety speed))
443                               `((when (layout-invalid ,n-layout)
444                                   (%layout-invalid-error object ',layout))))
445                       (if (eq ,n-layout ',layout)
446                           t
447                           (and (> (layout-depthoid ,n-layout)
448                                   ,depthoid)
449                                (locally (declare (optimize (safety 0)))
450                                  (eq (svref (layout-inherits ,n-layout)
451                                             ,depthoid)
452                                      ',layout))))))))
453            ((and layout (>= (layout-depthoid layout) 0))
454             ;; hierarchical layout depths for other things (e.g.
455             ;; CONDITIONs)
456             (let ((depthoid (layout-depthoid layout))
457                   (n-layout (gensym))
458                   (n-inherits (gensym)))
459               `(and (,pred object)
460                     (let ((,n-layout (,get-layout object)))
461                       ,@(when (policy *lexenv* (>= safety speed))
462                           `((when (layout-invalid ,n-layout)
463                               (%layout-invalid-error object ',layout))))
464                       (if (eq ,n-layout ',layout)
465                           t
466                           (let ((,n-inherits (layout-inherits ,n-layout)))
467                             (declare (optimize (safety 0)))
468                             (and (> (length ,n-inherits) ,depthoid)
469                                  (eq (svref ,n-inherits ,depthoid)
470                                      ',layout))))))))
471            (t
472             (/noshow "default case -- ,PRED and CLASS-CELL-TYPEP")
473             `(and (,pred object)
474                   (classoid-cell-typep (,get-layout object)
475                                        ',(find-classoid-cell name)
476                                        object)))))))))
477
478 ;;; If the specifier argument is a quoted constant, then we consider
479 ;;; converting into a simple predicate or other stuff. If the type is
480 ;;; constant, but we can't transform the call, then we convert to
481 ;;; %TYPEP. We only pass when the type is non-constant. This allows us
482 ;;; to recognize between calls that might later be transformed
483 ;;; successfully when a constant type is discovered. We don't give an
484 ;;; efficiency note when we pass, since the IR1 transform will give
485 ;;; one if necessary and appropriate.
486 ;;;
487 ;;; If the type is TYPE= to a type that has a predicate, then expand
488 ;;; to that predicate. Otherwise, we dispatch off of the type's type.
489 ;;; These transformations can increase space, but it is hard to tell
490 ;;; when, so we ignore policy and always do them.
491 (define-source-transform typep (object spec)
492   ;; KLUDGE: It looks bad to only do this on explicitly quoted forms,
493   ;; since that would overlook other kinds of constants. But it turns
494   ;; out that the DEFTRANSFORM for TYPEP detects any constant
495   ;; lvar, transforms it into a quoted form, and gives this
496   ;; source transform another chance, so it all works out OK, in a
497   ;; weird roundabout way. -- WHN 2001-03-18
498   (if (and (consp spec) (eq (car spec) 'quote))
499       (let ((type (careful-specifier-type (cadr spec))))
500         (or (when (not type)
501               (compiler-warn "illegal type specifier for TYPEP: ~S"
502                              (cadr spec))
503               `(%typep ,object ,spec))
504             (let ((pred (cdr (assoc type *backend-type-predicates*
505                                     :test #'type=))))
506               (when pred `(,pred ,object)))
507             (typecase type
508               (hairy-type
509                (source-transform-hairy-typep object type))
510               (negation-type
511                (source-transform-negation-typep object type))
512               (union-type
513                (source-transform-union-typep object type))
514               (intersection-type
515                (source-transform-intersection-typep object type))
516               (member-type
517                `(if (member ,object ',(member-type-members type)) t))
518               (args-type
519                (compiler-warn "illegal type specifier for TYPEP: ~S"
520                               (cadr spec))
521                `(%typep ,object ,spec))
522               (t nil))
523             (typecase type
524               (numeric-type
525                (source-transform-numeric-typep object type))
526               (classoid
527                `(%instance-typep ,object ,spec))
528               (array-type
529                (source-transform-array-typep object type))
530               (cons-type
531                (source-transform-cons-typep object type))
532               (character-set-type
533                (source-transform-character-set-typep object type))
534               (t nil))
535             `(%typep ,object ,spec)))
536       (values nil t)))
537 \f
538 ;;;; coercion
539
540 ;;; Constant-folding.
541 ;;;
542 #-sb-xc-host
543 (defoptimizer (coerce optimizer) ((x type) node)
544   (when (and (constant-lvar-p x) (constant-lvar-p type))
545     (let ((value (lvar-value x)))
546       (when (or (numberp value) (characterp value))
547         (constant-fold-call node)
548         t))))
549
550 (deftransform coerce ((x type) (* *) * :node node)
551   (unless (constant-lvar-p type)
552     (give-up-ir1-transform))
553   (let ((tspec (ir1-transform-specifier-type (lvar-value type))))
554     (if (csubtypep (lvar-type x) tspec)
555         'x
556         ;; Note: The THE here makes sure that specifiers like
557         ;; (SINGLE-FLOAT 0.0 1.0) can raise a TYPE-ERROR.
558         `(the ,(lvar-value type)
559            ,(cond
560              ((csubtypep tspec (specifier-type 'double-float))
561               '(%double-float x))
562              ;; FIXME: #!+long-float (t ,(error "LONG-FLOAT case needed"))
563              ((csubtypep tspec (specifier-type 'float))
564               '(%single-float x))
565              ((and (csubtypep tspec (specifier-type 'simple-vector))
566                    ;; Can we avoid checking for dimension issues like
567                    ;; (COERCE FOO '(SIMPLE-VECTOR 5)) returning a
568                    ;; vector of length 6?
569                    (or (policy node (< safety 3)) ; no need in unsafe code
570                        (and (array-type-p tspec) ; no need when no dimensions
571                             (equal (array-type-dimensions tspec) '(*)))))
572               `(if (simple-vector-p x)
573                    x
574                    (replace (make-array (length x)) x)))
575              ;; FIXME: other VECTOR types?
576              (t
577               (give-up-ir1-transform)))))))
578
579