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