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