7600eebfa19f18128490efac5b63ba4a6378d022
[sbcl.git] / src / compiler / ir1util.lisp
1 ;;;; This file contains miscellaneous utilities used for manipulating
2 ;;;; the IR1 representation.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14 \f
15 ;;;; cleanup hackery
16
17 ;;; Return the innermost cleanup enclosing NODE, or NIL if there is
18 ;;; none in its function. If NODE has no cleanup, but is in a LET,
19 ;;; then we must still check the environment that the call is in.
20 (defun node-enclosing-cleanup (node)
21   (declare (type node node))
22   (do ((lexenv (node-lexenv node)
23                (lambda-call-lexenv (lexenv-lambda lexenv))))
24       ((null lexenv) nil)
25     (let ((cup (lexenv-cleanup lexenv)))
26       (when cup (return cup)))))
27
28 ;;; Convert the FORM in a block inserted between BLOCK1 and BLOCK2 as
29 ;;; an implicit MV-PROG1. The inserted block is returned. NODE is used
30 ;;; for IR1 context when converting the form. Note that the block is
31 ;;; not assigned a number, and is linked into the DFO at the
32 ;;; beginning. We indicate that we have trashed the DFO by setting
33 ;;; COMPONENT-REANALYZE. If CLEANUP is supplied, then convert with
34 ;;; that cleanup.
35 (defun insert-cleanup-code (block1 block2 node form &optional cleanup)
36   (declare (type cblock block1 block2) (type node node)
37            (type (or cleanup null) cleanup))
38   (setf (component-reanalyze (block-component block1)) t)
39   (with-ir1-environment-from-node node
40     (with-component-last-block (*current-component*
41                                 (block-next (component-head *current-component*)))
42       (let* ((start (make-ctran))
43              (block (ctran-starts-block start))
44              (next (make-ctran))
45              (*lexenv* (if cleanup
46                            (make-lexenv :cleanup cleanup)
47                            *lexenv*)))
48         (change-block-successor block1 block2 block)
49         (link-blocks block block2)
50         (ir1-convert start next nil form)
51         (setf (block-last block) (ctran-use next))
52         (setf (node-next (block-last block)) nil)
53         block))))
54 \f
55 ;;;; lvar use hacking
56
57 ;;; Return a list of all the nodes which use LVAR.
58 (declaim (ftype (sfunction (lvar) list) find-uses))
59 (defun find-uses (lvar)
60   (let ((uses (lvar-uses lvar)))
61     (if (listp uses)
62         uses
63         (list uses))))
64
65 (defun principal-lvar-use (lvar)
66   (let ((use (lvar-uses lvar)))
67     (if (cast-p use)
68         (principal-lvar-use (cast-value use))
69         use)))
70
71 ;;; Update lvar use information so that NODE is no longer a use of its
72 ;;; LVAR.
73 ;;;
74 ;;; Note: if you call this function, you may have to do a
75 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
76 ;;; changed.
77 (declaim (ftype (sfunction (node) (values))
78                 delete-lvar-use
79                 %delete-lvar-use))
80 ;;; Just delete NODE from its LVAR uses; LVAR is preserved so it may
81 ;;; be given a new use.
82 (defun %delete-lvar-use (node)
83   (let ((lvar (node-lvar node)))
84     (when lvar
85       (if (listp (lvar-uses lvar))
86           (let ((new-uses (delq node (lvar-uses lvar))))
87             (setf (lvar-uses lvar)
88                   (if (singleton-p new-uses)
89                       (first new-uses)
90                       new-uses)))
91           (setf (lvar-uses lvar) nil))
92       (setf (node-lvar node) nil)))
93   (values))
94 ;;; Delete NODE from its LVAR uses; if LVAR has no other uses, delete
95 ;;; its DEST's block, which must be unreachable.
96 (defun delete-lvar-use (node)
97   (let ((lvar (node-lvar node)))
98     (when lvar
99       (%delete-lvar-use node)
100       (if (null (lvar-uses lvar))
101           (binding* ((dest (lvar-dest lvar) :exit-if-null)
102                      (() (not (node-deleted dest)) :exit-if-null)
103                      (block (node-block dest)))
104             (mark-for-deletion block))
105           (reoptimize-lvar lvar))))
106   (values))
107
108 ;;; Update lvar use information so that NODE uses LVAR.
109 ;;;
110 ;;; Note: if you call this function, you may have to do a
111 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
112 ;;; changed.
113 (declaim (ftype (sfunction (node (or lvar null)) (values)) add-lvar-use))
114 (defun add-lvar-use (node lvar)
115   (aver (not (node-lvar node)))
116   (when lvar
117     (let ((uses (lvar-uses lvar)))
118       (setf (lvar-uses lvar)
119             (cond ((null uses)
120                    node)
121                   ((listp uses)
122                    (cons node uses))
123                   (t
124                    (list node uses))))
125       (setf (node-lvar node) lvar)))
126
127   (values))
128
129 ;;; Return true if LVAR destination is executed immediately after
130 ;;; NODE. Cleanups are ignored.
131 (defun immediately-used-p (lvar node)
132   (declare (type lvar lvar) (type node node))
133   (aver (eq (node-lvar node) lvar))
134   (let ((dest (lvar-dest lvar)))
135     (acond ((node-next node)
136             (eq (ctran-next it) dest))
137            (t (eq (block-start (first (block-succ (node-block node))))
138                   (node-prev dest))))))
139 \f
140 ;;;; lvar substitution
141
142 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
143 ;;; NIL. We do not flush OLD's DEST.
144 (defun substitute-lvar (new old)
145   (declare (type lvar old new))
146   (aver (not (lvar-dest new)))
147   (let ((dest (lvar-dest old)))
148     (etypecase dest
149       ((or ref bind))
150       (cif (setf (if-test dest) new))
151       (cset (setf (set-value dest) new))
152       (creturn (setf (return-result dest) new))
153       (exit (setf (exit-value dest) new))
154       (basic-combination
155        (if (eq old (basic-combination-fun dest))
156            (setf (basic-combination-fun dest) new)
157            (setf (basic-combination-args dest)
158                  (nsubst new old (basic-combination-args dest)))))
159       (cast (setf (cast-value dest) new)))
160
161     (setf (lvar-dest old) nil)
162     (setf (lvar-dest new) dest)
163     (flush-lvar-externally-checkable-type new))
164   (values))
165
166 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
167 ;;; arbitary number of uses.
168 (defun substitute-lvar-uses (new old)
169   (declare (type lvar old)
170            (type (or lvar null) new))
171
172   (cond (new (do-uses (node old)
173                (%delete-lvar-use node)
174                (add-lvar-use node new))
175              (reoptimize-lvar new))
176         (t (flush-dest old)))
177   (values))
178 \f
179 ;;;; block starting/creation
180
181 ;;; Return the block that CTRAN is the start of, making a block if
182 ;;; necessary. This function is called by IR1 translators which may
183 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
184 ;;; used more than once must start a block by the time that anyone
185 ;;; does a USE-CTRAN on it.
186 ;;;
187 ;;; We also throw the block into the next/prev list for the
188 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
189 ;;; made.
190 (defun ctran-starts-block (ctran)
191   (declare (type ctran ctran))
192   (ecase (ctran-kind ctran)
193     (:unused
194      (aver (not (ctran-block ctran)))
195      (let* ((next (component-last-block *current-component*))
196             (prev (block-prev next))
197             (new-block (make-block ctran)))
198        (setf (block-next new-block) next
199              (block-prev new-block) prev
200              (block-prev next) new-block
201              (block-next prev) new-block
202              (ctran-block ctran) new-block
203              (ctran-kind ctran) :block-start)
204        (aver (not (ctran-use ctran)))
205        new-block))
206     (:block-start
207      (ctran-block ctran))))
208
209 ;;; Ensure that CTRAN is the start of a block so that the use set can
210 ;;; be freely manipulated.
211 (defun ensure-block-start (ctran)
212   (declare (type ctran ctran))
213   (let ((kind (ctran-kind ctran)))
214     (ecase kind
215       ((:block-start))
216       ((:unused)
217        (setf (ctran-block ctran)
218              (make-block-key :start ctran))
219        (setf (ctran-kind ctran) :block-start))
220       ((:inside-block)
221        (node-ends-block (ctran-use ctran)))))
222   (values))
223
224 ;;; CTRAN must be the last ctran in an incomplete block; finish the
225 ;;; block and start a new one if necessary.
226 (defun start-block (ctran)
227   (declare (type ctran ctran))
228   (aver (not (ctran-next ctran)))
229   (ecase (ctran-kind ctran)
230     (:inside-block
231      (let ((block (ctran-block ctran))
232            (node (ctran-use ctran)))
233        (aver (not (block-last block)))
234        (aver node)
235        (setf (block-last block) node)
236        (setf (node-next node) nil)
237        (setf (ctran-use ctran) nil)
238        (setf (ctran-kind ctran) :unused)
239        (setf (ctran-block ctran) nil)
240        (link-blocks block (ctran-starts-block ctran))))
241     (:block-start)))
242 \f
243 ;;;;
244
245 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
246 ;;; call. First argument must be 'DUMMY, which will be replaced with
247 ;;; LVAR. In case of an ordinary call the function should not have
248 ;;; return type NIL. We create a new "filtered" lvar.
249 ;;;
250 ;;; TODO: remove preconditions.
251 (defun filter-lvar (lvar form)
252   (declare (type lvar lvar) (type list form))
253   (let* ((dest (lvar-dest lvar))
254          (ctran (node-prev dest)))
255     (with-ir1-environment-from-node dest
256
257       (ensure-block-start ctran)
258       (let* ((old-block (ctran-block ctran))
259              (new-start (make-ctran))
260              (filtered-lvar (make-lvar))
261              (new-block (ctran-starts-block new-start)))
262
263         ;; Splice in the new block before DEST, giving the new block
264         ;; all of DEST's predecessors.
265         (dolist (block (block-pred old-block))
266           (change-block-successor block old-block new-block))
267
268         (ir1-convert new-start ctran filtered-lvar form)
269
270         ;; KLUDGE: Comments at the head of this function in CMU CL
271         ;; said that somewhere in here we
272         ;;   Set the new block's start and end cleanups to the *start*
273         ;;   cleanup of PREV's block. This overrides the incorrect
274         ;;   default from WITH-IR1-ENVIRONMENT-FROM-NODE.
275         ;; Unfortunately I can't find any code which corresponds to this.
276         ;; Perhaps it was a stale comment? Or perhaps I just don't
277         ;; understand.. -- WHN 19990521
278
279         ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
280         ;; no LET conversion has been done yet.) The [mv-]combination
281         ;; code from the call in the form will be the use of the new
282         ;; check lvar. We substitute for the first argument of
283         ;; this node.
284         (let* ((node (lvar-use filtered-lvar))
285                (args (basic-combination-args node))
286                (victim (first args)))
287           (aver (eq (constant-value (ref-leaf (lvar-use victim)))
288                     'dummy))
289
290           (substitute-lvar filtered-lvar lvar)
291           (substitute-lvar lvar victim)
292           (flush-dest victim))
293
294         ;; Invoking local call analysis converts this call to a LET.
295         (locall-analyze-component *current-component*))))
296   (values))
297
298 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
299 (defun delete-filter (node lvar value)
300   (aver (eq (lvar-dest value) node))
301   (aver (eq (node-lvar node) lvar))
302   (cond (lvar (collect ((merges))
303                 (when (return-p (lvar-dest lvar))
304                   (do-uses (use value)
305                     (when (and (basic-combination-p use)
306                                (eq (basic-combination-kind use) :local))
307                       (merges use))))
308                 (%delete-lvar-use node)
309                 (substitute-lvar-uses lvar value)
310                 (prog1
311                     (unlink-node node)
312                   (dolist (merge (merges))
313                     (merge-tail-sets merge)))))
314         (t (flush-dest value)
315            (unlink-node node))))
316 \f
317 ;;;; miscellaneous shorthand functions
318
319 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
320 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
321 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
322 ;;; deleted, and then return its home.
323 (defun node-home-lambda (node)
324   (declare (type node node))
325   (do ((fun (lexenv-lambda (node-lexenv node))
326             (lexenv-lambda (lambda-call-lexenv fun))))
327       ((not (memq (functional-kind fun) '(:deleted :zombie)))
328        (lambda-home fun))
329     (when (eq (lambda-home fun) fun)
330       (return fun))))
331
332 #!-sb-fluid (declaim (inline node-block))
333 (defun node-block (node)
334   (ctran-block (node-prev node)))
335 (declaim (ftype (sfunction (node) component) node-component))
336 (defun node-component (node)
337   (block-component (node-block node)))
338 (declaim (ftype (sfunction (node) physenv) node-physenv))
339 (defun node-physenv (node)
340   (lambda-physenv (node-home-lambda node)))
341 #!-sb-fluid (declaim (inline node-dest))
342 (defun node-dest (node)
343   (awhen (node-lvar node) (lvar-dest it)))
344
345 (declaim (inline block-to-be-deleted-p))
346 (defun block-to-be-deleted-p (block)
347   (or (block-delete-p block)
348       (eq (functional-kind (block-home-lambda block)) :deleted)))
349
350 ;;; Checks whether NODE is in a block to be deleted
351 (declaim (inline node-to-be-deleted-p))
352 (defun node-to-be-deleted-p (node)
353   (block-to-be-deleted-p (node-block node)))
354
355 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
356 (defun lambda-block (clambda)
357   (node-block (lambda-bind clambda)))
358 (declaim (ftype (sfunction (clambda) component) lambda-component))
359 (defun lambda-component (clambda)
360   (block-component (lambda-block clambda)))
361
362 (declaim (ftype (sfunction (cblock) node) block-start-node))
363 (defun block-start-node (block)
364   (ctran-next (block-start block)))
365
366 ;;; Return the enclosing cleanup for environment of the first or last
367 ;;; node in BLOCK.
368 (defun block-start-cleanup (block)
369   (node-enclosing-cleanup (block-start-node block)))
370 (defun block-end-cleanup (block)
371   (node-enclosing-cleanup (block-last block)))
372
373 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
374 ;;; if there is none.
375 ;;;
376 ;;; There can legitimately be no home lambda in dead code early in the
377 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
378 ;;;   (BLOCK B (RETURN-FROM B) (SETQ X 3))
379 ;;; where the block is just a placeholder during parsing and doesn't
380 ;;; actually correspond to code which will be written anywhere.
381 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
382 (defun block-home-lambda-or-null (block)
383   (if (node-p (block-last block))
384       ;; This is the old CMU CL way of doing it.
385       (node-home-lambda (block-last block))
386       ;; Now that SBCL uses this operation more aggressively than CMU
387       ;; CL did, the old CMU CL way of doing it can fail in two ways.
388       ;;   1. It can fail in a few cases even when a meaningful home
389       ;;      lambda exists, e.g. in IR1-CONVERT of one of the legs of
390       ;;      an IF.
391       ;;   2. It can fail when converting a form which is born orphaned 
392       ;;      so that it never had a meaningful home lambda, e.g. a form
393       ;;      which follows a RETURN-FROM or GO form.
394       (let ((pred-list (block-pred block)))
395         ;; To deal with case 1, we reason that
396         ;; previous-in-target-execution-order blocks should be in the
397         ;; same lambda, and that they seem in practice to be
398         ;; previous-in-compilation-order blocks too, so we look back
399         ;; to find one which is sufficiently initialized to tell us
400         ;; what the home lambda is.
401         (if pred-list
402             ;; We could get fancy about this, flooding through the
403             ;; graph of all the previous blocks, but in practice it
404             ;; seems to work just to grab the first previous block and
405             ;; use it.
406             (node-home-lambda (block-last (first pred-list)))
407             ;; In case 2, we end up with an empty PRED-LIST and
408             ;; have to punt: There's no home lambda.
409             nil))))
410
411 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
412 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
413 (defun block-home-lambda (block)
414   (block-home-lambda-or-null block))
415
416 ;;; Return the IR1 physical environment for BLOCK.
417 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
418 (defun block-physenv (block)
419   (lambda-physenv (block-home-lambda block)))
420
421 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
422 ;;; of its original source's top level form in its compilation unit.
423 (defun source-path-tlf-number (path)
424   (declare (list path))
425   (car (last path)))
426
427 ;;; Return the (reversed) list for the PATH in the original source
428 ;;; (with the Top Level Form number last).
429 (defun source-path-original-source (path)
430   (declare (list path) (inline member))
431   (cddr (member 'original-source-start path :test #'eq)))
432
433 ;;; Return the Form Number of PATH's original source inside the Top
434 ;;; Level Form that contains it. This is determined by the order that
435 ;;; we walk the subforms of the top level source form.
436 (defun source-path-form-number (path)
437   (declare (list path) (inline member))
438   (cadr (member 'original-source-start path :test #'eq)))
439
440 ;;; Return a list of all the enclosing forms not in the original
441 ;;; source that converted to get to this form, with the immediate
442 ;;; source for node at the start of the list.
443 (defun source-path-forms (path)
444   (subseq path 0 (position 'original-source-start path)))
445
446 ;;; Return the innermost source form for NODE.
447 (defun node-source-form (node)
448   (declare (type node node))
449   (let* ((path (node-source-path node))
450          (forms (source-path-forms path)))
451     (if forms
452         (first forms)
453         (values (find-original-source path)))))
454
455 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
456 ;;; NIL, NIL.
457 (defun lvar-source (lvar)
458   (let ((use (lvar-uses lvar)))
459     (if (listp use)
460         (values nil nil)
461         (values (node-source-form use) t))))
462
463 ;;; Return the unique node, delivering a value to LVAR.
464 #!-sb-fluid (declaim (inline lvar-use))
465 (defun lvar-use (lvar)
466   (the (not list) (lvar-uses lvar)))
467
468 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
469 (defun lvar-has-single-use-p (lvar)
470   (typep (lvar-uses lvar) '(not list)))
471
472 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
473 (declaim (ftype (sfunction (ctran) (or clambda null))
474                 ctran-home-lambda-or-null))
475 (defun ctran-home-lambda-or-null (ctran)
476   ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
477   ;; implementation might not be quite right, or might be uglier than
478   ;; necessary. It appears that the original Python never found a need
479   ;; to do this operation. The obvious things based on
480   ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
481   ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
482   ;; generalize it enough to grovel harder when the simple CMU CL
483   ;; approach fails, and furthermore realize that in some exceptional
484   ;; cases it might return NIL. -- WHN 2001-12-04
485   (cond ((ctran-use ctran)
486          (node-home-lambda (ctran-use ctran)))
487         ((ctran-block ctran)
488          (block-home-lambda-or-null (ctran-block ctran)))
489         (t
490          (bug "confused about home lambda for ~S" ctran))))
491
492 ;;; Return the LAMBDA that is CTRAN's home.
493 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
494 (defun ctran-home-lambda (ctran)
495   (ctran-home-lambda-or-null ctran))
496
497 (declaim (inline cast-single-value-p))
498 (defun cast-single-value-p (cast)
499   (not (values-type-p (cast-asserted-type cast))))
500
501 #!-sb-fluid (declaim (inline lvar-single-value-p))
502 (defun lvar-single-value-p (lvar)
503   (or (not lvar)
504       (let ((dest (lvar-dest lvar)))
505         (typecase dest
506           ((or creturn exit)
507            nil)
508           (mv-combination
509            (eq (basic-combination-fun dest) lvar))
510           (cast
511            (locally
512                (declare (notinline lvar-single-value-p))
513              (and (cast-single-value-p dest)
514                   (lvar-single-value-p (node-lvar dest)))))
515           (t
516            t)))))
517
518 (defun principal-lvar-end (lvar)
519   (loop for prev = lvar then (node-lvar dest)
520         for dest = (and prev (lvar-dest prev))
521         while (cast-p dest)
522         finally (return (values dest prev))))
523
524 (defun principal-lvar-single-valuify (lvar)
525   (loop for prev = lvar then (node-lvar dest)
526         for dest = (and prev (lvar-dest prev))
527         while (cast-p dest)
528         do (setf (node-derived-type dest)
529                  (make-short-values-type (list (single-value-type
530                                                 (node-derived-type dest)))))
531         (reoptimize-lvar prev)))
532 \f
533 ;;; Return a new LEXENV just like DEFAULT except for the specified
534 ;;; slot values. Values for the alist slots are NCONCed to the
535 ;;; beginning of the current value, rather than replacing it entirely.
536 (defun make-lexenv (&key (default *lexenv*)
537                          funs vars blocks tags
538                          type-restrictions
539                          (lambda (lexenv-lambda default))
540                          (cleanup (lexenv-cleanup default))
541                          (handled-conditions (lexenv-handled-conditions default))
542                          (disabled-package-locks 
543                           (lexenv-disabled-package-locks default))
544                          (policy (lexenv-policy default)))
545   (macrolet ((frob (var slot)
546                `(let ((old (,slot default)))
547                   (if ,var
548                       (nconc ,var old)
549                       old))))
550     (internal-make-lexenv
551      (frob funs lexenv-funs)
552      (frob vars lexenv-vars)
553      (frob blocks lexenv-blocks)
554      (frob tags lexenv-tags)
555      (frob type-restrictions lexenv-type-restrictions)
556      lambda cleanup handled-conditions 
557      disabled-package-locks policy)))
558
559 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
560 ;;; macroexpander
561 (defun make-restricted-lexenv (lexenv)
562   (flet ((fun-good-p (fun)
563            (destructuring-bind (name . thing) fun
564              (declare (ignore name))
565              (etypecase thing
566                (functional nil)
567                (global-var t)
568                (cons (aver (eq (car thing) 'macro))
569                      t))))
570          (var-good-p (var)
571            (destructuring-bind (name . thing) var
572              (declare (ignore name))
573              (etypecase thing
574                (leaf nil)
575                (cons (aver (eq (car thing) 'macro))
576                      t)
577                (heap-alien-info nil)))))
578     (internal-make-lexenv
579      (remove-if-not #'fun-good-p (lexenv-funs lexenv))
580      (remove-if-not #'var-good-p (lexenv-vars lexenv))
581      nil
582      nil
583      (lexenv-type-restrictions lexenv) ; XXX
584      nil
585      nil
586      (lexenv-handled-conditions lexenv)
587      (lexenv-disabled-package-locks lexenv)
588      (lexenv-policy lexenv))))
589 \f
590 ;;;; flow/DFO/component hackery
591
592 ;;; Join BLOCK1 and BLOCK2.
593 (defun link-blocks (block1 block2)
594   (declare (type cblock block1 block2))
595   (setf (block-succ block1)
596         (if (block-succ block1)
597             (%link-blocks block1 block2)
598             (list block2)))
599   (push block1 (block-pred block2))
600   (values))
601 (defun %link-blocks (block1 block2)
602   (declare (type cblock block1 block2))
603   (let ((succ1 (block-succ block1)))
604     (aver (not (memq block2 succ1)))
605     (cons block2 succ1)))
606
607 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
608 ;;; this leaves a successor with a single predecessor that ends in an
609 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
610 ;;; now be able to be propagated to the successor.
611 (defun unlink-blocks (block1 block2)
612   (declare (type cblock block1 block2))
613   (let ((succ1 (block-succ block1)))
614     (if (eq block2 (car succ1))
615         (setf (block-succ block1) (cdr succ1))
616         (do ((succ (cdr succ1) (cdr succ))
617              (prev succ1 succ))
618             ((eq (car succ) block2)
619              (setf (cdr prev) (cdr succ)))
620           (aver succ))))
621
622   (let ((new-pred (delq block1 (block-pred block2))))
623     (setf (block-pred block2) new-pred)
624     (when (singleton-p new-pred)
625       (let ((pred-block (first new-pred)))
626         (when (if-p (block-last pred-block))
627           (setf (block-test-modified pred-block) t)))))
628   (values))
629
630 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
631 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
632 ;;; consequent/alternative blocks to point to NEW. We also set
633 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
634 ;;; the new successor.
635 (defun change-block-successor (block old new)
636   (declare (type cblock new old block))
637   (unlink-blocks block old)
638   (let ((last (block-last block))
639         (comp (block-component block)))
640     (setf (component-reanalyze comp) t)
641     (typecase last
642       (cif
643        (setf (block-test-modified block) t)
644        (let* ((succ-left (block-succ block))
645               (new (if (and (eq new (component-tail comp))
646                             succ-left)
647                        (first succ-left)
648                        new)))
649          (unless (memq new succ-left)
650            (link-blocks block new))
651          (macrolet ((frob (slot)
652                       `(when (eq (,slot last) old)
653                          (setf (,slot last) new))))
654            (frob if-consequent)
655            (frob if-alternative)
656            (when (eq (if-consequent last)
657                      (if-alternative last))
658              (setf (component-reoptimize (block-component block)) t)))))
659       (t
660        (unless (memq new (block-succ block))
661          (link-blocks block new)))))
662
663   (values))
664
665 ;;; Unlink a block from the next/prev chain. We also null out the
666 ;;; COMPONENT.
667 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
668 (defun remove-from-dfo (block)
669   (let ((next (block-next block))
670         (prev (block-prev block)))
671     (setf (block-component block) nil)
672     (setf (block-next prev) next)
673     (setf (block-prev next) prev))
674   (values))
675
676 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
677 ;;; COMPONENT to be the same as for AFTER.
678 (defun add-to-dfo (block after)
679   (declare (type cblock block after))
680   (let ((next (block-next after))
681         (comp (block-component after)))
682     (aver (not (eq (component-kind comp) :deleted)))
683     (setf (block-component block) comp)
684     (setf (block-next after) block)
685     (setf (block-prev block) after)
686     (setf (block-next block) next)
687     (setf (block-prev next) block))
688   (values))
689
690 ;;; List all NLX-INFOs which BLOCK can exit to.
691 ;;;
692 ;;; We hope that no cleanup actions are performed in the middle of
693 ;;; BLOCK, so it is enough to look only at cleanups in the block
694 ;;; end. The tricky thing is a special cleanup block; all its nodes
695 ;;; have the same cleanup info, corresponding to the start, so the
696 ;;; same approach returns safe result.
697 (defun map-block-nlxes (fun block)
698   (loop for cleanup = (block-end-cleanup block)
699         then (node-enclosing-cleanup (cleanup-mess-up cleanup))
700         while cleanup
701         do (let ((mess-up (cleanup-mess-up cleanup)))
702              (case (cleanup-kind cleanup)
703                ((:block :tagbody)
704                 (aver (entry-p mess-up))
705                 (loop for exit in (entry-exits mess-up)
706                       for nlx-info = (find-nlx-info exit)
707                       do (funcall fun nlx-info)))
708                ((:catch :unwind-protect)
709                 (aver (combination-p mess-up))
710                 (let* ((arg-lvar (first (basic-combination-args mess-up)))
711                        (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
712                 (funcall fun nlx-info)))))))
713
714 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
715 ;;; the head and tail which are set to T.
716 (declaim (ftype (sfunction (component) (values)) clear-flags))
717 (defun clear-flags (component)
718   (let ((head (component-head component))
719         (tail (component-tail component)))
720     (setf (block-flag head) t)
721     (setf (block-flag tail) t)
722     (do-blocks (block component)
723       (setf (block-flag block) nil)))
724   (values))
725
726 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
727 ;;; true in the head and tail blocks.
728 (declaim (ftype (sfunction () component) make-empty-component))
729 (defun make-empty-component ()
730   (let* ((head (make-block-key :start nil :component nil))
731          (tail (make-block-key :start nil :component nil))
732          (res (make-component head tail)))
733     (setf (block-flag head) t)
734     (setf (block-flag tail) t)
735     (setf (block-component head) res)
736     (setf (block-component tail) res)
737     (setf (block-next head) tail)
738     (setf (block-prev tail) head)
739     res))
740
741 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
742 ;;; The new block is added to the DFO immediately following NODE's block.
743 (defun node-ends-block (node)
744   (declare (type node node))
745   (let* ((block (node-block node))
746          (start (node-next node))
747          (last (block-last block)))
748     (unless (eq last node)
749       (aver (and (eq (ctran-kind start) :inside-block)
750                  (not (block-delete-p block))))
751       (let* ((succ (block-succ block))
752              (new-block
753               (make-block-key :start start
754                               :component (block-component block)
755                               :succ succ :last last)))
756         (setf (ctran-kind start) :block-start)
757         (setf (ctran-use start) nil)
758         (setf (block-last block) node)
759         (setf (node-next node) nil)
760         (dolist (b succ)
761           (setf (block-pred b)
762                 (cons new-block (remove block (block-pred b)))))
763         (setf (block-succ block) ())
764         (link-blocks block new-block)
765         (add-to-dfo new-block block)
766         (setf (component-reanalyze (block-component block)) t)
767
768         (do ((ctran start (node-next (ctran-next ctran))))
769             ((not ctran))
770           (setf (ctran-block ctran) new-block))
771
772         (setf (block-type-asserted block) t)
773         (setf (block-test-modified block) t))))
774   (values))
775 \f
776 ;;;; deleting stuff
777
778 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
779 (defun delete-lambda-var (leaf)
780   (declare (type lambda-var leaf))
781
782   ;; Iterate over all local calls flushing the corresponding argument,
783   ;; allowing the computation of the argument to be deleted. We also
784   ;; mark the LET for reoptimization, since it may be that we have
785   ;; deleted its last variable.
786   (let* ((fun (lambda-var-home leaf))
787          (n (position leaf (lambda-vars fun))))
788     (dolist (ref (leaf-refs fun))
789       (let* ((lvar (node-lvar ref))
790              (dest (and lvar (lvar-dest lvar))))
791         (when (and (combination-p dest)
792                    (eq (basic-combination-fun dest) lvar)
793                    (eq (basic-combination-kind dest) :local))
794           (let* ((args (basic-combination-args dest))
795                  (arg (elt args n)))
796             (reoptimize-lvar arg)
797             (flush-dest arg)
798             (setf (elt args n) nil))))))
799
800   ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
801   ;; too much difficulty, since we can efficiently implement
802   ;; write-only variables. We iterate over the SETs, marking their
803   ;; blocks for dead code flushing, since we can delete SETs whose
804   ;; value is unused.
805   (dolist (set (lambda-var-sets leaf))
806     (setf (block-flush-p (node-block set)) t))
807
808   (values))
809
810 ;;; Note that something interesting has happened to VAR.
811 (defun reoptimize-lambda-var (var)
812   (declare (type lambda-var var))
813   (let ((fun (lambda-var-home var)))
814     ;; We only deal with LET variables, marking the corresponding
815     ;; initial value arg as needing to be reoptimized.
816     (when (and (eq (functional-kind fun) :let)
817                (leaf-refs var))
818       (do ((args (basic-combination-args
819                   (lvar-dest (node-lvar (first (leaf-refs fun)))))
820                  (cdr args))
821            (vars (lambda-vars fun) (cdr vars)))
822           ((eq (car vars) var)
823            (reoptimize-lvar (car args))))))
824   (values))
825
826 ;;; Delete a function that has no references. This need only be called
827 ;;; on functions that never had any references, since otherwise
828 ;;; DELETE-REF will handle the deletion.
829 (defun delete-functional (fun)
830   (aver (and (null (leaf-refs fun))
831              (not (functional-entry-fun fun))))
832   (etypecase fun
833     (optional-dispatch (delete-optional-dispatch fun))
834     (clambda (delete-lambda fun)))
835   (values))
836
837 ;;; Deal with deleting the last reference to a CLAMBDA, which means
838 ;;; that the lambda is unreachable, so that its body may be
839 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
840 ;;; IR1-OPTIMIZE to delete its blocks.
841 (defun delete-lambda (clambda)
842   (declare (type clambda clambda))
843   (let ((original-kind (functional-kind clambda))
844         (bind (lambda-bind clambda)))
845     (aver (not (member original-kind '(:deleted :toplevel))))
846     (aver (not (functional-has-external-references-p clambda)))
847     (aver (or (eq original-kind :zombie) bind))
848     (setf (functional-kind clambda) :deleted)
849     (setf (lambda-bind clambda) nil)
850
851     (labels ((delete-children (lambda)
852                (dolist (child (lambda-children lambda))
853                  (cond ((eq (functional-kind child) :deleted)
854                         (delete-children child))
855                        (t
856                         (delete-lambda child))))
857                (setf (lambda-children lambda) nil)
858                (setf (lambda-parent lambda) nil)))
859       (delete-children clambda))
860
861     ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
862     ;; that we're using the old value of the KIND slot, not the
863     ;; current slot value, which has now been set to :DELETED.)
864     (case original-kind
865       (:zombie)
866       ((:let :mv-let :assignment)
867        (let ((bind-block (node-block bind)))
868          (mark-for-deletion bind-block))
869        (let ((home (lambda-home clambda)))
870          (setf (lambda-lets home) (delete clambda (lambda-lets home))))
871        ;; KLUDGE: In presence of NLEs we cannot always understand that
872        ;; LET's BIND dominates its body [for a LET "its" body is not
873        ;; quite its]; let's delete too dangerous for IR2 stuff. --
874        ;; APD, 2004-01-01
875        (dolist (var (lambda-vars clambda))
876          (flet ((delete-node (node)
877                   (mark-for-deletion (node-block node))))
878          (mapc #'delete-node (leaf-refs var))
879          (mapc #'delete-node (lambda-var-sets var)))))
880       (t
881        ;; Function has no reachable references.
882        (dolist (ref (lambda-refs clambda))
883          (mark-for-deletion (node-block ref)))
884        ;; If the function isn't a LET, we unlink the function head
885        ;; and tail from the component head and tail to indicate that
886        ;; the code is unreachable. We also delete the function from
887        ;; COMPONENT-LAMBDAS (it won't be there before local call
888        ;; analysis, but no matter.) If the lambda was never
889        ;; referenced, we give a note.
890        (let* ((bind-block (node-block bind))
891               (component (block-component bind-block))
892               (return (lambda-return clambda))
893               (return-block (and return (node-block return))))
894          (unless (leaf-ever-used clambda)
895            (let ((*compiler-error-context* bind))
896              (compiler-notify 'code-deletion-note
897                               :format-control "deleting unused function~:[.~;~:*~%  ~S~]"
898                               :format-arguments (list (leaf-debug-name clambda)))))
899          (unless (block-delete-p bind-block)
900            (unlink-blocks (component-head component) bind-block))
901          (when (and return-block (not (block-delete-p return-block)))
902            (mark-for-deletion return-block)
903            (unlink-blocks return-block (component-tail component)))
904          (setf (component-reanalyze component) t)
905          (let ((tails (lambda-tail-set clambda)))
906            (setf (tail-set-funs tails)
907                  (delete clambda (tail-set-funs tails)))
908            (setf (lambda-tail-set clambda) nil))
909          (setf (component-lambdas component)
910                (delq clambda (component-lambdas component))))))
911
912     ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
913     ;; ENTRY-FUN so that people will know that it is not an entry
914     ;; point anymore.
915     (when (eq original-kind :external)
916       (let ((fun (functional-entry-fun clambda)))
917         (setf (functional-entry-fun fun) nil)
918         (when (optional-dispatch-p fun)
919           (delete-optional-dispatch fun)))))
920
921   (values))
922
923 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
924 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
925 ;;; is used both before and after local call analysis. Afterward, all
926 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
927 ;;; to the XEP, leaving it with no references at all. So we look at
928 ;;; the XEP to see whether an optional-dispatch is still really being
929 ;;; used. But before local call analysis, there are no XEPs, and all
930 ;;; references are direct.
931 ;;;
932 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
933 ;;; entry-points, making them be normal lambdas, and then deleting the
934 ;;; ones with no references. This deletes any e-p lambdas that were
935 ;;; either never referenced, or couldn't be deleted when the last
936 ;;; reference was deleted (due to their :OPTIONAL kind.)
937 ;;;
938 ;;; Note that the last optional entry point may alias the main entry,
939 ;;; so when we process the main entry, its KIND may have been changed
940 ;;; to NIL or even converted to a LETlike value.
941 (defun delete-optional-dispatch (leaf)
942   (declare (type optional-dispatch leaf))
943   (let ((entry (functional-entry-fun leaf)))
944     (unless (and entry (leaf-refs entry))
945       (aver (or (not entry) (eq (functional-kind entry) :deleted)))
946       (setf (functional-kind leaf) :deleted)
947
948       (flet ((frob (fun)
949                (unless (eq (functional-kind fun) :deleted)
950                  (aver (eq (functional-kind fun) :optional))
951                  (setf (functional-kind fun) nil)
952                  (let ((refs (leaf-refs fun)))
953                    (cond ((null refs)
954                           (delete-lambda fun))
955                          ((null (rest refs))
956                           (or (maybe-let-convert fun)
957                               (maybe-convert-to-assignment fun)))
958                          (t
959                           (maybe-convert-to-assignment fun)))))))
960
961         (dolist (ep (optional-dispatch-entry-points leaf))
962           (when (promise-ready-p ep)
963             (frob (force ep))))
964         (when (optional-dispatch-more-entry leaf)
965           (frob (optional-dispatch-more-entry leaf)))
966         (let ((main (optional-dispatch-main-entry leaf)))
967           (when (eq (functional-kind main) :optional)
968             (frob main))))))
969
970   (values))
971
972 ;;; Do stuff to delete the semantic attachments of a REF node. When
973 ;;; this leaves zero or one reference, we do a type dispatch off of
974 ;;; the leaf to determine if a special action is appropriate.
975 (defun delete-ref (ref)
976   (declare (type ref ref))
977   (let* ((leaf (ref-leaf ref))
978          (refs (delq ref (leaf-refs leaf))))
979     (setf (leaf-refs leaf) refs)
980
981     (cond ((null refs)
982            (typecase leaf
983              (lambda-var
984               (delete-lambda-var leaf))
985              (clambda
986               (ecase (functional-kind leaf)
987                 ((nil :let :mv-let :assignment :escape :cleanup)
988                  (aver (null (functional-entry-fun leaf)))
989                  (delete-lambda leaf))
990                 (:external
991                  (delete-lambda leaf))
992                 ((:deleted :zombie :optional))))
993              (optional-dispatch
994               (unless (eq (functional-kind leaf) :deleted)
995                 (delete-optional-dispatch leaf)))))
996           ((null (rest refs))
997            (typecase leaf
998              (clambda (or (maybe-let-convert leaf)
999                           (maybe-convert-to-assignment leaf)))
1000              (lambda-var (reoptimize-lambda-var leaf))))
1001           (t
1002            (typecase leaf
1003              (clambda (maybe-convert-to-assignment leaf))))))
1004
1005   (values))
1006
1007 ;;; This function is called by people who delete nodes; it provides a
1008 ;;; way to indicate that the value of a lvar is no longer used. We
1009 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1010 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1011 (defun flush-dest (lvar)
1012   (declare (type (or lvar null) lvar))
1013   (unless (null lvar)
1014     (setf (lvar-dest lvar) nil)
1015     (flush-lvar-externally-checkable-type lvar)
1016     (do-uses (use lvar)
1017       (let ((prev (node-prev use)))
1018         (let ((block (ctran-block prev)))
1019           (setf (component-reoptimize (block-component block)) t)
1020           (setf (block-attributep (block-flags block)
1021                                   flush-p type-asserted type-check)
1022                 t)))
1023       (setf (node-lvar use) nil))
1024     (setf (lvar-uses lvar) nil))
1025   (values))
1026
1027 (defun delete-dest (lvar)
1028   (when lvar
1029     (let* ((dest (lvar-dest lvar))
1030            (prev (node-prev dest)))
1031       (let ((block (ctran-block prev)))
1032         (unless (block-delete-p block)
1033           (mark-for-deletion block))))))
1034
1035 ;;; Queue the block for deletion
1036 (defun delete-block-lazily (block)
1037   (declare (type cblock block))
1038   (unless (block-delete-p block)
1039     (setf (block-delete-p block) t)
1040     (push block (component-delete-blocks (block-component block)))))
1041
1042 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1043 ;;; blocks with the DELETE-P flag.
1044 (defun mark-for-deletion (block)
1045   (declare (type cblock block))
1046   (let* ((component (block-component block))
1047          (head (component-head component)))
1048     (labels ((helper (block)
1049                (delete-block-lazily block)
1050                (dolist (pred (block-pred block))
1051                  (unless (or (block-delete-p pred)
1052                              (eq pred head))
1053                    (helper pred)))))
1054       (unless (block-delete-p block)
1055         (helper block)
1056         (setf (component-reanalyze component) t))))
1057   (values))
1058
1059 ;;; This function does what is necessary to eliminate the code in it
1060 ;;; from the IR1 representation. This involves unlinking it from its
1061 ;;; predecessors and successors and deleting various node-specific
1062 ;;; semantic information. BLOCK must be already removed from
1063 ;;; COMPONENT-DELETE-BLOCKS.
1064 (defun delete-block (block &optional silent)
1065   (declare (type cblock block))
1066   (aver (block-component block))      ; else block is already deleted!
1067   #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1068   (unless silent
1069     (note-block-deletion block))
1070   (setf (block-delete-p block) t)
1071
1072   (dolist (b (block-pred block))
1073     (unlink-blocks b block)
1074     ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1075     ;; broken when successors were deleted without setting the
1076     ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1077     ;; doesn't happen again.
1078     (aver (not (and (null (block-succ b))
1079                     (not (block-delete-p b))
1080                     (not (eq b (component-head (block-component b))))))))
1081   (dolist (b (block-succ block))
1082     (unlink-blocks block b))
1083
1084   (do-nodes-carefully (node block)
1085     (when (valued-node-p node)
1086       (delete-lvar-use node))
1087     (etypecase node
1088       (ref (delete-ref node))
1089       (cif (flush-dest (if-test node)))
1090       ;; The next two cases serve to maintain the invariant that a LET
1091       ;; always has a well-formed COMBINATION, REF and BIND. We delete
1092       ;; the lambda whenever we delete any of these, but we must be
1093       ;; careful that this LET has not already been partially deleted.
1094       (basic-combination
1095        (when (and (eq (basic-combination-kind node) :local)
1096                   ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1097                   (lvar-uses (basic-combination-fun node)))
1098          (let ((fun (combination-lambda node)))
1099            ;; If our REF was the second-to-last ref, and has been
1100            ;; deleted, then FUN may be a LET for some other
1101            ;; combination.
1102            (when (and (functional-letlike-p fun)
1103                       (eq (let-combination fun) node))
1104              (delete-lambda fun))))
1105        (flush-dest (basic-combination-fun node))
1106        (dolist (arg (basic-combination-args node))
1107          (when arg (flush-dest arg))))
1108       (bind
1109        (let ((lambda (bind-lambda node)))
1110          (unless (eq (functional-kind lambda) :deleted)
1111            (delete-lambda lambda))))
1112       (exit
1113        (let ((value (exit-value node))
1114              (entry (exit-entry node)))
1115          (when value
1116            (flush-dest value))
1117          (when entry
1118            (setf (entry-exits entry)
1119                  (delq node (entry-exits entry))))))
1120       (entry
1121        (dolist (exit (entry-exits node))
1122          (mark-for-deletion (node-block exit)))
1123        (let ((home (node-home-lambda node)))
1124          (setf (lambda-entries home) (delq node (lambda-entries home)))))
1125       (creturn
1126        (flush-dest (return-result node))
1127        (delete-return node))
1128       (cset
1129        (flush-dest (set-value node))
1130        (let ((var (set-var node)))
1131          (setf (basic-var-sets var)
1132                (delete node (basic-var-sets var)))))
1133       (cast
1134        (flush-dest (cast-value node)))))
1135
1136   (remove-from-dfo block)
1137   (values))
1138
1139 ;;; Do stuff to indicate that the return node NODE is being deleted.
1140 (defun delete-return (node)
1141   (declare (type creturn node))
1142   (let* ((fun (return-lambda node))
1143          (tail-set (lambda-tail-set fun)))
1144     (aver (lambda-return fun))
1145     (setf (lambda-return fun) nil)
1146     (when (and tail-set (not (find-if #'lambda-return
1147                                       (tail-set-funs tail-set))))
1148       (setf (tail-set-type tail-set) *empty-type*)))
1149   (values))
1150
1151 ;;; If any of the VARS in FUN was never referenced and was not
1152 ;;; declared IGNORE, then complain.
1153 (defun note-unreferenced-vars (fun)
1154   (declare (type clambda fun))
1155   (dolist (var (lambda-vars fun))
1156     (unless (or (leaf-ever-used var)
1157                 (lambda-var-ignorep var))
1158       (let ((*compiler-error-context* (lambda-bind fun)))
1159         (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1160           ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1161           ;; requires this to be no more than a STYLE-WARNING.
1162           #-sb-xc-host
1163           (compiler-style-warn "The variable ~S is defined but never used."
1164                                (leaf-debug-name var))
1165           ;; There's no reason to accept this kind of equivocation
1166           ;; when compiling our own code, though.
1167           #+sb-xc-host
1168           (warn "The variable ~S is defined but never used."
1169                 (leaf-debug-name var)))
1170         (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1171   (values))
1172
1173 (defvar *deletion-ignored-objects* '(t nil))
1174
1175 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1176 ;;; our recursion so that we don't get lost in circular structures. We
1177 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1178 ;;; function referencess with variables), and we also ignore anything
1179 ;;; inside ' or #'.
1180 (defun present-in-form (obj form depth)
1181   (declare (type (integer 0 20) depth))
1182   (cond ((= depth 20) nil)
1183         ((eq obj form) t)
1184         ((atom form) nil)
1185         (t
1186          (let ((first (car form))
1187                (depth (1+ depth)))
1188            (if (member first '(quote function))
1189                nil
1190                (or (and (not (symbolp first))
1191                         (present-in-form obj first depth))
1192                    (do ((l (cdr form) (cdr l))
1193                         (n 0 (1+ n)))
1194                        ((or (atom l) (> n 100))
1195                         nil)
1196                      (declare (fixnum n))
1197                      (when (present-in-form obj (car l) depth)
1198                        (return t)))))))))
1199
1200 ;;; This function is called on a block immediately before we delete
1201 ;;; it. We check to see whether any of the code about to die appeared
1202 ;;; in the original source, and emit a note if so.
1203 ;;;
1204 ;;; If the block was in a lambda is now deleted, then we ignore the
1205 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1206 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1207 ;;; reasonable for a function to not return, and there is a different
1208 ;;; note for that case anyway.
1209 ;;;
1210 ;;; If the actual source is an atom, then we use a bunch of heuristics
1211 ;;; to guess whether this reference really appeared in the original
1212 ;;; source:
1213 ;;; -- If a symbol, it must be interned and not a keyword.
1214 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1215 ;;;    or a character.)
1216 ;;; -- The atom must be "present" in the original source form, and
1217 ;;;    present in all intervening actual source forms.
1218 (defun note-block-deletion (block)
1219   (let ((home (block-home-lambda block)))
1220     (unless (eq (functional-kind home) :deleted)
1221       (do-nodes (node nil block)
1222         (let* ((path (node-source-path node))
1223                (first (first path)))
1224           (when (or (eq first 'original-source-start)
1225                     (and (atom first)
1226                          (or (not (symbolp first))
1227                              (let ((pkg (symbol-package first)))
1228                                (and pkg
1229                                     (not (eq pkg (symbol-package :end))))))
1230                          (not (member first *deletion-ignored-objects*))
1231                          (not (typep first '(or fixnum character)))
1232                          (every (lambda (x)
1233                                   (present-in-form first x 0))
1234                                 (source-path-forms path))
1235                          (present-in-form first (find-original-source path)
1236                                           0)))
1237             (unless (return-p node)
1238               (let ((*compiler-error-context* node))
1239                 (compiler-notify 'code-deletion-note
1240                                  :format-control "deleting unreachable code"
1241                                  :format-arguments nil)))
1242             (return))))))
1243   (values))
1244
1245 ;;; Delete a node from a block, deleting the block if there are no
1246 ;;; nodes left. We remove the node from the uses of its LVAR.
1247 ;;;
1248 ;;; If the node is the last node, there must be exactly one successor.
1249 ;;; We link all of our precedessors to the successor and unlink the
1250 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1251 ;;; left, and the block is a successor of itself, then we replace the
1252 ;;; only node with a degenerate exit node. This provides a way to
1253 ;;; represent the bodyless infinite loop, given the prohibition on
1254 ;;; empty blocks in IR1.
1255 (defun unlink-node (node)
1256   (declare (type node node))
1257   (when (valued-node-p node)
1258     (delete-lvar-use node))
1259
1260   (let* ((ctran (node-next node))
1261          (next (and ctran (ctran-next ctran)))
1262          (prev (node-prev node))
1263          (block (ctran-block prev))
1264          (prev-kind (ctran-kind prev))
1265          (last (block-last block)))
1266
1267     (setf (block-type-asserted block) t)
1268     (setf (block-test-modified block) t)
1269
1270     (cond ((or (eq prev-kind :inside-block)
1271                (and (eq prev-kind :block-start)
1272                     (not (eq node last))))
1273            (cond ((eq node last)
1274                   (setf (block-last block) (ctran-use prev))
1275                   (setf (node-next (ctran-use prev)) nil))
1276                  (t
1277                   (setf (ctran-next prev) next)
1278                   (setf (node-prev next) prev)
1279                   (when (if-p next) ; AOP wanted
1280                     (reoptimize-lvar (if-test next)))))
1281            (setf (node-prev node) nil)
1282            nil)
1283           (t
1284            (aver (eq prev-kind :block-start))
1285            (aver (eq node last))
1286            (let* ((succ (block-succ block))
1287                   (next (first succ)))
1288              (aver (singleton-p succ))
1289              (cond
1290               ((eq block (first succ))
1291                (with-ir1-environment-from-node node
1292                  (let ((exit (make-exit)))
1293                    (setf (ctran-next prev) nil)
1294                    (link-node-to-previous-ctran exit prev)
1295                    (setf (block-last block) exit)))
1296                (setf (node-prev node) nil)
1297                nil)
1298               (t
1299                (aver (eq (block-start-cleanup block)
1300                          (block-end-cleanup block)))
1301                (unlink-blocks block next)
1302                (dolist (pred (block-pred block))
1303                  (change-block-successor pred block next))
1304                (when (block-delete-p block)
1305                  (let ((component (block-component block)))
1306                    (setf (component-delete-blocks component)
1307                          (delq block (component-delete-blocks component)))))
1308                (remove-from-dfo block)
1309                (setf (block-delete-p block) t)
1310                (setf (node-prev node) nil)
1311                t)))))))
1312
1313 ;;; Return true if NODE has been deleted, false if it is still a valid
1314 ;;; part of IR1.
1315 (defun node-deleted (node)
1316   (declare (type node node))
1317   (let ((prev (node-prev node)))
1318     (not (and prev
1319               (let ((block (ctran-block prev)))
1320                 (and (block-component block)
1321                      (not (block-delete-p block))))))))
1322
1323 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1324 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1325 ;;; triggered by deletion.
1326 (defun delete-component (component)
1327   (declare (type component component))
1328   (aver (null (component-new-functionals component)))
1329   (setf (component-kind component) :deleted)
1330   (do-blocks (block component)
1331     (delete-block-lazily block))
1332   (dolist (fun (component-lambdas component))
1333     (unless (eq (functional-kind fun) :deleted)
1334       (setf (functional-kind fun) nil)
1335       (setf (functional-entry-fun fun) nil)
1336       (setf (leaf-refs fun) nil)
1337       (delete-functional fun)))
1338   (clean-component component)
1339   (values))
1340
1341 ;;; Remove all pending blocks to be deleted. Return the nearest live
1342 ;;; block after or equal to BLOCK.
1343 (defun clean-component (component &optional block)
1344   (loop while (component-delete-blocks component)
1345         ;; actual deletion of a block may queue new blocks
1346         do (let ((current (pop (component-delete-blocks component))))
1347              (when (eq block current)
1348                (setq block (block-next block)))
1349              (delete-block current)))
1350   block)
1351
1352 ;;; Convert code of the form
1353 ;;;   (FOO ... (FUN ...) ...)
1354 ;;; to
1355 ;;;   (FOO ...    ...    ...).
1356 ;;; In other words, replace the function combination FUN by its
1357 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1358 ;;; to blow out of whatever transform called this. Note, as the number
1359 ;;; of arguments changes, the transform must be prepared to return a
1360 ;;; lambda with a new lambda-list with the correct number of
1361 ;;; arguments.
1362 (defun extract-fun-args (lvar fun num-args)
1363   #!+sb-doc
1364   "If LVAR is a call to FUN with NUM-ARGS args, change those arguments
1365    to feed directly to the LVAR-DEST of LVAR, which must be a
1366    combination."
1367   (declare (type lvar lvar)
1368            (type symbol fun)
1369            (type index num-args))
1370   (let ((outside (lvar-dest lvar))
1371         (inside (lvar-uses lvar)))
1372     (aver (combination-p outside))
1373     (unless (combination-p inside)
1374       (give-up-ir1-transform))
1375     (let ((inside-fun (combination-fun inside)))
1376       (unless (eq (lvar-fun-name inside-fun) fun)
1377         (give-up-ir1-transform))
1378       (let ((inside-args (combination-args inside)))
1379         (unless (= (length inside-args) num-args)
1380           (give-up-ir1-transform))
1381         (let* ((outside-args (combination-args outside))
1382                (arg-position (position lvar outside-args))
1383                (before-args (subseq outside-args 0 arg-position))
1384                (after-args (subseq outside-args (1+ arg-position))))
1385           (dolist (arg inside-args)
1386             (setf (lvar-dest arg) outside)
1387             (flush-lvar-externally-checkable-type arg))
1388           (setf (combination-args inside) nil)
1389           (setf (combination-args outside)
1390                 (append before-args inside-args after-args))
1391           (change-ref-leaf (lvar-uses inside-fun)
1392                            (find-free-fun 'list "???"))
1393           (setf (combination-fun-info inside) (info :function :info 'list)
1394                 (combination-kind inside) :known)
1395           (setf (node-derived-type inside) *wild-type*)
1396           (flush-dest lvar)
1397           (values))))))
1398
1399 (defun flush-combination (combination)
1400   (declare (type combination combination))
1401   (flush-dest (combination-fun combination))
1402   (dolist (arg (combination-args combination))
1403     (flush-dest arg))
1404   (unlink-node combination)
1405   (values))
1406
1407 \f
1408 ;;;; leaf hackery
1409
1410 ;;; Change the LEAF that a REF refers to.
1411 (defun change-ref-leaf (ref leaf)
1412   (declare (type ref ref) (type leaf leaf))
1413   (unless (eq (ref-leaf ref) leaf)
1414     (push ref (leaf-refs leaf))
1415     (delete-ref ref)
1416     (setf (ref-leaf ref) leaf)
1417     (setf (leaf-ever-used leaf) t)
1418     (let* ((ltype (leaf-type leaf))
1419            (vltype (make-single-value-type ltype)))
1420       (if (let* ((lvar (node-lvar ref))
1421                  (dest (and lvar (lvar-dest lvar))))
1422             (and (basic-combination-p dest)
1423                  (eq lvar (basic-combination-fun dest))
1424                  (csubtypep ltype (specifier-type 'function))))
1425           (setf (node-derived-type ref) vltype)
1426           (derive-node-type ref vltype)))
1427     (reoptimize-lvar (node-lvar ref)))
1428   (values))
1429
1430 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1431 (defun substitute-leaf (new-leaf old-leaf)
1432   (declare (type leaf new-leaf old-leaf))
1433   (dolist (ref (leaf-refs old-leaf))
1434     (change-ref-leaf ref new-leaf))
1435   (values))
1436
1437 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1438 ;;; whether to substitute
1439 (defun substitute-leaf-if (test new-leaf old-leaf)
1440   (declare (type leaf new-leaf old-leaf) (type function test))
1441   (dolist (ref (leaf-refs old-leaf))
1442     (when (funcall test ref)
1443       (change-ref-leaf ref new-leaf)))
1444   (values))
1445
1446 ;;; Return a LEAF which represents the specified constant object. If
1447 ;;; the object is not in *CONSTANTS*, then we create a new constant
1448 ;;; LEAF and enter it.
1449 (defun find-constant (object)
1450   (if (typep object
1451              ;; FIXME: What is the significance of this test? ("things
1452              ;; that are worth uniquifying"?)
1453              '(or symbol number character instance))
1454       (or (gethash object *constants*)
1455           (setf (gethash object *constants*)
1456                 (make-constant :value object
1457                                :%source-name '.anonymous.
1458                                :type (ctype-of object)
1459                                :where-from :defined)))
1460       (make-constant :value object
1461                      :%source-name '.anonymous.
1462                      :type (ctype-of object)
1463                      :where-from :defined)))
1464 \f
1465 ;;; Return true if VAR would have to be closed over if environment
1466 ;;; analysis ran now (i.e. if there are any uses that have a different
1467 ;;; home lambda than VAR's home.)
1468 (defun closure-var-p (var)
1469   (declare (type lambda-var var))
1470   (let ((home (lambda-var-home var)))
1471     (cond ((eq (functional-kind home) :deleted)
1472            nil)
1473           (t (let ((home (lambda-home home)))
1474                (flet ((frob (l)
1475                         (find home l
1476                               :key #'node-home-lambda
1477                               :test #'neq)))
1478                  (or (frob (leaf-refs var))
1479                      (frob (basic-var-sets var)))))))))
1480
1481 ;;; If there is a non-local exit noted in ENTRY's environment that
1482 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1483 (defun find-nlx-info (exit)
1484   (declare (type exit exit))
1485   (let ((entry (exit-entry exit)))
1486     (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1487       (when (eq (nlx-info-exit nlx) exit)
1488         (return nlx)))))
1489 \f
1490 ;;;; functional hackery
1491
1492 (declaim (ftype (sfunction (functional) clambda) main-entry))
1493 (defun main-entry (functional)
1494   (etypecase functional
1495     (clambda functional)
1496     (optional-dispatch
1497      (optional-dispatch-main-entry functional))))
1498
1499 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1500 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1501 ;;; optional with null default and no SUPPLIED-P. There must be a
1502 ;;; &REST arg with no references.
1503 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
1504 (defun looks-like-an-mv-bind (functional)
1505   (and (optional-dispatch-p functional)
1506        (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1507            ((null arg) nil)
1508          (let ((info (lambda-var-arg-info (car arg))))
1509            (unless info (return nil))
1510            (case (arg-info-kind info)
1511              (:optional
1512               (when (or (arg-info-supplied-p info) (arg-info-default info))
1513                 (return nil)))
1514              (:rest
1515               (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1516              (t
1517               (return nil)))))))
1518
1519 ;;; Return true if function is an external entry point. This is true
1520 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1521 ;;; (:TOPLEVEL kind.)
1522 (defun xep-p (fun)
1523   (declare (type functional fun))
1524   (not (null (member (functional-kind fun) '(:external :toplevel)))))
1525
1526 ;;; If LVAR's only use is a non-notinline global function reference,
1527 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1528 ;;; is true, then we don't care if the leaf is NOTINLINE.
1529 (defun lvar-fun-name (lvar &optional notinline-ok)
1530   (declare (type lvar lvar))
1531   (let ((use (lvar-uses lvar)))
1532     (if (ref-p use)
1533         (let ((leaf (ref-leaf use)))
1534           (if (and (global-var-p leaf)
1535                    (eq (global-var-kind leaf) :global-function)
1536                    (or (not (defined-fun-p leaf))
1537                        (not (eq (defined-fun-inlinep leaf) :notinline))
1538                        notinline-ok))
1539               (leaf-source-name leaf)
1540               nil))
1541         nil)))
1542
1543 ;;; Return the source name of a combination. (This is an idiom
1544 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1545 (defun combination-fun-source-name (combination)
1546   (let ((ref (lvar-uses (combination-fun combination))))
1547     (leaf-source-name (ref-leaf ref))))
1548
1549 ;;; Return the COMBINATION node that is the call to the LET FUN.
1550 (defun let-combination (fun)
1551   (declare (type clambda fun))
1552   (aver (functional-letlike-p fun))
1553   (lvar-dest (node-lvar (first (leaf-refs fun)))))
1554
1555 ;;; Return the initial value lvar for a LET variable, or NIL if there
1556 ;;; is none.
1557 (defun let-var-initial-value (var)
1558   (declare (type lambda-var var))
1559   (let ((fun (lambda-var-home var)))
1560     (elt (combination-args (let-combination fun))
1561          (position-or-lose var (lambda-vars fun)))))
1562
1563 ;;; Return the LAMBDA that is called by the local CALL.
1564 (defun combination-lambda (call)
1565   (declare (type basic-combination call))
1566   (aver (eq (basic-combination-kind call) :local))
1567   (ref-leaf (lvar-uses (basic-combination-fun call))))
1568
1569 (defvar *inline-expansion-limit* 200
1570   #!+sb-doc
1571   "an upper limit on the number of inline function calls that will be expanded
1572    in any given code object (single function or block compilation)")
1573
1574 ;;; Check whether NODE's component has exceeded its inline expansion
1575 ;;; limit, and warn if so, returning NIL.
1576 (defun inline-expansion-ok (node)
1577   (let ((expanded (incf (component-inline-expansions
1578                          (block-component
1579                           (node-block node))))))
1580     (cond ((> expanded *inline-expansion-limit*) nil)
1581           ((= expanded *inline-expansion-limit*)
1582            ;; FIXME: If the objective is to stop the recursive
1583            ;; expansion of inline functions, wouldn't it be more
1584            ;; correct to look back through surrounding expansions
1585            ;; (which are, I think, stored in the *CURRENT-PATH*, and
1586            ;; possibly stored elsewhere too) and suppress expansion
1587            ;; and print this warning when the function being proposed
1588            ;; for inline expansion is found there? (I don't like the
1589            ;; arbitrary numerical limit in principle, and I think
1590            ;; it'll be a nuisance in practice if we ever want the
1591            ;; compiler to be able to use WITH-COMPILATION-UNIT on
1592            ;; arbitrarily huge blocks of code. -- WHN)
1593            (let ((*compiler-error-context* node))
1594              (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1595                                probably trying to~%  ~
1596                                inline a recursive function."
1597                               *inline-expansion-limit*))
1598            nil)
1599           (t t))))
1600
1601 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
1602 (defun assure-functional-live-p (functional)
1603   (declare (type functional functional))
1604   (when (and (or
1605               ;; looks LET-converted
1606               (functional-somewhat-letlike-p functional)
1607               ;; It's possible for a LET-converted function to end up
1608               ;; deleted later. In that case, for the purposes of this
1609               ;; analysis, it is LET-converted: LET-converted functionals
1610               ;; are too badly trashed to expand them inline, and deleted
1611               ;; LET-converted functionals are even worse.
1612               (memq (functional-kind functional) '(:deleted :zombie))))
1613     (throw 'locall-already-let-converted functional)))
1614
1615 (defun call-full-like-p (call)
1616   (declare (type combination call))
1617   (let ((kind (basic-combination-kind call)))
1618     (or (eq kind :full)
1619         (and (eq kind :known)
1620              (let ((info (basic-combination-fun-info call)))
1621                (and
1622                 (not (fun-info-ir2-convert info))
1623                 (dolist (template (fun-info-templates info) t)
1624                   (when (eq (template-ltn-policy template) :fast-safe)
1625                     (multiple-value-bind (val win)
1626                        (valid-fun-use call (template-type template))
1627                       (when (or val (not win)) (return nil)))))))))))
1628 \f
1629 ;;;; careful call
1630
1631 ;;; Apply a function to some arguments, returning a list of the values
1632 ;;; resulting of the evaluation. If an error is signalled during the
1633 ;;; application, then we produce a warning message using WARN-FUN and
1634 ;;; return NIL as our second value to indicate this. NODE is used as
1635 ;;; the error context for any error message, and CONTEXT is a string
1636 ;;; that is spliced into the warning.
1637 (declaim (ftype (sfunction ((or symbol function) list node function string)
1638                           (values list boolean))
1639                 careful-call))
1640 (defun careful-call (function args node warn-fun context)
1641   (values
1642    (multiple-value-list
1643     (handler-case (apply function args)
1644       (error (condition)
1645         (let ((*compiler-error-context* node))
1646           (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1647           (return-from careful-call (values nil nil))))))
1648    t))
1649
1650 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1651 ;;; specifiers.
1652 (macrolet
1653     ((deffrob (basic careful compiler transform)
1654        `(progn
1655           (defun ,careful (specifier)
1656             (handler-case (,basic specifier)
1657               (sb!kernel::arg-count-error (condition)
1658                 (values nil (list (format nil "~A" condition))))
1659               (simple-error (condition)
1660                 (values nil (list* (simple-condition-format-control condition)
1661                                    (simple-condition-format-arguments condition))))))
1662           (defun ,compiler (specifier)
1663             (multiple-value-bind (type error-args) (,careful specifier)
1664               (or type
1665                   (apply #'compiler-error error-args))))
1666           (defun ,transform (specifier)
1667             (multiple-value-bind (type error-args) (,careful specifier)
1668               (or type
1669                   (apply #'give-up-ir1-transform
1670                          error-args)))))))
1671   (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1672   (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1673
1674 \f
1675 ;;;; utilities used at run-time for parsing &KEY args in IR1
1676
1677 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1678 ;;; the lvar for the value of the &KEY argument KEY in the list of
1679 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
1680 ;;; otherwise. The legality and constantness of the keywords should
1681 ;;; already have been checked.
1682 (declaim (ftype (sfunction (list keyword) (or lvar null))
1683                 find-keyword-lvar))
1684 (defun find-keyword-lvar (args key)
1685   (do ((arg args (cddr arg)))
1686       ((null arg) nil)
1687     (when (eq (lvar-value (first arg)) key)
1688       (return (second arg)))))
1689
1690 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1691 ;;; verify that alternating lvars in ARGS are constant and that there
1692 ;;; is an even number of args.
1693 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
1694 (defun check-key-args-constant (args)
1695   (do ((arg args (cddr arg)))
1696       ((null arg) t)
1697     (unless (and (rest arg)
1698                  (constant-lvar-p (first arg)))
1699       (return nil))))
1700
1701 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1702 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
1703 ;;; and that only keywords present in the list KEYS are supplied.
1704 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
1705 (defun check-transform-keys (args keys)
1706   (and (check-key-args-constant args)
1707        (do ((arg args (cddr arg)))
1708            ((null arg) t)
1709          (unless (member (lvar-value (first arg)) keys)
1710            (return nil)))))
1711 \f
1712 ;;;; miscellaneous
1713
1714 ;;; Called by the expansion of the EVENT macro.
1715 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
1716 (defun %event (info node)
1717   (incf (event-info-count info))
1718   (when (and (>= (event-info-level info) *event-note-threshold*)
1719              (policy (or node *lexenv*)
1720                      (= inhibit-warnings 0)))
1721     (let ((*compiler-error-context* node))
1722       (compiler-notify (event-info-description info))))
1723
1724   (let ((action (event-info-action info)))
1725     (when action (funcall action node))))
1726
1727 ;;;
1728 (defun make-cast (value type policy)
1729   (declare (type lvar value)
1730            (type ctype type)
1731            (type policy policy))
1732   (%make-cast :asserted-type type
1733               :type-to-check (maybe-weaken-check type policy)
1734               :value value
1735               :derived-type (coerce-to-values type)))
1736
1737 (defun cast-type-check (cast)
1738   (declare (type cast cast))
1739   (when (cast-reoptimize cast)
1740     (ir1-optimize-cast cast t))
1741   (cast-%type-check cast))
1742
1743 (defun note-single-valuified-lvar (lvar)
1744   (declare (type (or lvar null) lvar))
1745   (when lvar
1746     (let ((use (lvar-uses lvar)))
1747       (cond ((ref-p use)
1748              (let ((leaf (ref-leaf use)))
1749                (when (and (lambda-var-p leaf)
1750                           (null (rest (leaf-refs leaf))))
1751                  (reoptimize-lambda-var leaf))))
1752             ((or (listp use) (combination-p use))
1753              (do-uses (node lvar)
1754                (setf (node-reoptimize node) t)
1755                (setf (block-reoptimize (node-block node)) t)
1756                (setf (component-reoptimize (node-component node)) t)))))))