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