1 ;;;; This file contains miscellaneous utilities used for manipulating
2 ;;;; the IR1 representation.
4 ;;;; This software is part of the SBCL system. See the README file for
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.
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))))
25 (let ((cup (lexenv-cleanup lexenv)))
26 (when cup (return cup)))))
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
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 (let* ((start (make-continuation))
41 (block (continuation-starts-block start))
42 (cont (make-continuation))
44 (make-lexenv :cleanup cleanup)
46 (change-block-successor block1 block2 block)
47 (link-blocks block block2)
48 (ir1-convert start cont form)
49 (setf (block-last block) (continuation-use cont))
52 ;;;; continuation use hacking
54 ;;; Return a list of all the nodes which use Cont.
55 (declaim (ftype (function (continuation) list) find-uses))
56 (defun find-uses (cont)
57 (ecase (continuation-kind cont)
58 ((:block-start :deleted-block-start)
59 (block-start-uses (continuation-block cont)))
60 (:inside-block (list (continuation-use cont)))
64 ;;; Update continuation use information so that NODE is no longer a
65 ;;; use of its CONT. If the old continuation doesn't start its block,
66 ;;; then we don't update the BLOCK-START-USES, since it will be
67 ;;; deleted when we are done.
69 ;;; Note: if you call this function, you may have to do a
70 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
72 (declaim (ftype (function (node) (values)) delete-continuation-use))
73 (defun delete-continuation-use (node)
74 (let* ((cont (node-cont node))
75 (block (continuation-block cont)))
76 (ecase (continuation-kind cont)
78 ((:block-start :deleted-block-start)
79 (let ((uses (delete node (block-start-uses block))))
80 (setf (block-start-uses block) uses)
81 (setf (continuation-use cont)
82 (if (cdr uses) nil (car uses)))))
84 (setf (continuation-kind cont) :unused)
85 (setf (continuation-block cont) nil)
86 (setf (continuation-use cont) nil)
87 (setf (continuation-next cont) nil)))
88 (setf (node-cont node) nil))
91 ;;; Update continuation use information so that NODE uses CONT. If
92 ;;; CONT is :UNUSED, then we set its block to NODE's NODE-BLOCK (which
95 ;;; Note: if you call this function, you may have to do a
96 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
98 (declaim (ftype (function (node continuation) (values)) add-continuation-use))
99 (defun add-continuation-use (node cont)
100 (aver (not (node-cont node)))
101 (let ((block (continuation-block cont)))
102 (ecase (continuation-kind cont)
106 (let ((block (node-block node)))
108 (setf (continuation-block cont) block))
109 (setf (continuation-kind cont) :inside-block)
110 (setf (continuation-use cont) node))
111 ((:block-start :deleted-block-start)
112 (let ((uses (cons node (block-start-uses block))))
113 (setf (block-start-uses block) uses)
114 (setf (continuation-use cont)
115 (if (cdr uses) nil (car uses)))))))
116 (setf (node-cont node) cont)
119 ;;; Return true if CONT is the NODE-CONT for NODE and CONT is
120 ;;; transferred to immediately after the evaluation of NODE.
121 (defun immediately-used-p (cont node)
122 (declare (type continuation cont) (type node node))
123 (and (eq (node-cont node) cont)
124 (not (eq (continuation-kind cont) :deleted))
125 (let ((cblock (continuation-block cont))
126 (nblock (node-block node)))
127 (or (eq cblock nblock)
128 (let ((succ (block-succ nblock)))
129 (and (= (length succ) 1)
130 (eq (first succ) cblock)))))))
132 ;;;; continuation substitution
134 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
135 ;;; NIL. When we are done, we call FLUSH-DEST on OLD to clear its DEST
136 ;;; and to note potential optimization opportunities.
137 (defun substitute-continuation (new old)
138 (declare (type continuation old new))
139 (aver (not (continuation-dest new)))
140 (let ((dest (continuation-dest old)))
143 (cif (setf (if-test dest) new))
144 (cset (setf (set-value dest) new))
145 (creturn (setf (return-result dest) new))
146 (exit (setf (exit-value dest) new))
148 (if (eq old (basic-combination-fun dest))
149 (setf (basic-combination-fun dest) new)
150 (setf (basic-combination-args dest)
151 (nsubst new old (basic-combination-args dest))))))
154 (setf (continuation-dest new) dest)
155 (setf (continuation-%externally-checkable-type new) nil))
158 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
159 ;;; arbitary number of uses. If NEW will end up with more than one
160 ;;; use, then we must arrange for it to start a block if it doesn't
162 (defun substitute-continuation-uses (new old)
163 (declare (type continuation old new))
164 (unless (and (eq (continuation-kind new) :unused)
165 (eq (continuation-kind old) :inside-block))
166 (ensure-block-start new))
169 (delete-continuation-use node)
170 (add-continuation-use node new))
171 (dolist (lexenv-use (continuation-lexenv-uses old))
172 (setf (cadr lexenv-use) new))
174 (reoptimize-continuation new)
177 ;;;; block starting/creation
179 ;;; Return the block that CONT is the start of, making a block if
180 ;;; necessary. This function is called by IR1 translators which may
181 ;;; cause a continuation to be used more than once. Every continuation
182 ;;; which may be used more than once must start a block by the time
183 ;;; that anyone does a USE-CONTINUATION on it.
185 ;;; We also throw the block into the next/prev list for the
186 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
188 (defun continuation-starts-block (cont)
189 (declare (type continuation cont))
190 (ecase (continuation-kind cont)
192 (aver (not (continuation-block cont)))
193 (let* ((head (component-head *current-component*))
194 (next (block-next head))
195 (new-block (make-block cont)))
196 (setf (block-next new-block) next
197 (block-prev new-block) head
198 (block-prev next) new-block
199 (block-next head) new-block
200 (continuation-block cont) new-block
201 (continuation-use cont) nil
202 (continuation-kind cont) :block-start)
205 (continuation-block cont))))
207 ;;; Ensure that CONT is the start of a block (or deleted) so that
208 ;;; the use set can be freely manipulated.
209 ;;; -- If the continuation is :UNUSED or is :INSIDE-BLOCK and the
210 ;;; CONT of LAST in its block, then we make it the start of a new
212 ;;; -- If the continuation is :INSIDE-BLOCK inside a block, then we
213 ;;; split the block using Node-Ends-Block, which makes the
214 ;;; continuation be a :BLOCK-START.
215 (defun ensure-block-start (cont)
216 (declare (type continuation cont))
217 (let ((kind (continuation-kind cont)))
219 ((:deleted :block-start :deleted-block-start))
220 ((:unused :inside-block)
221 (let ((block (continuation-block cont)))
222 (cond ((or (eq kind :unused)
223 (eq (node-cont (block-last block)) cont))
224 (setf (continuation-block cont)
225 (make-block-key :start cont
227 :start-uses (find-uses cont)))
228 (setf (continuation-kind cont) :deleted-block-start))
230 (node-ends-block (continuation-use cont))))))))
233 ;;;; miscellaneous shorthand functions
235 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
236 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
237 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
238 ;;; deleted, and then return its home.
239 (defun node-home-lambda (node)
240 (declare (type node node))
241 (do ((fun (lexenv-lambda (node-lexenv node))
242 (lexenv-lambda (lambda-call-lexenv fun))))
243 ((not (eq (functional-kind fun) :deleted))
245 (when (eq (lambda-home fun) fun)
248 (defun node-block (node)
249 (declare (type node node))
250 (the cblock (continuation-block (node-prev node))))
251 (defun node-component (node)
252 (declare (type node node))
253 (block-component (node-block node)))
254 (defun node-physenv (node)
255 (declare (type node node))
256 (the physenv (lambda-physenv (node-home-lambda node))))
258 (defun lambda-block (clambda)
259 (declare (type clambda clambda))
260 (node-block (lambda-bind clambda)))
261 (defun lambda-component (clambda)
262 (block-component (lambda-block clambda)))
264 ;;; Return the enclosing cleanup for environment of the first or last
266 (defun block-start-cleanup (block)
267 (declare (type cblock block))
268 (node-enclosing-cleanup (continuation-next (block-start block))))
269 (defun block-end-cleanup (block)
270 (declare (type cblock block))
271 (node-enclosing-cleanup (block-last block)))
273 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
274 ;;; if there is none.
276 ;;; There can legitimately be no home lambda in dead code early in the
277 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
278 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
279 ;;; where the block is just a placeholder during parsing and doesn't
280 ;;; actually correspond to code which will be written anywhere.
281 (defun block-home-lambda-or-null (block)
282 (declare (type cblock block))
283 (if (node-p (block-last block))
284 ;; This is the old CMU CL way of doing it.
285 (node-home-lambda (block-last block))
286 ;; Now that SBCL uses this operation more aggressively than CMU
287 ;; CL did, the old CMU CL way of doing it can fail in two ways.
288 ;; 1. It can fail in a few cases even when a meaningful home
289 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
291 ;; 2. It can fail when converting a form which is born orphaned
292 ;; so that it never had a meaningful home lambda, e.g. a form
293 ;; which follows a RETURN-FROM or GO form.
294 (let ((pred-list (block-pred block)))
295 ;; To deal with case 1, we reason that
296 ;; previous-in-target-execution-order blocks should be in the
297 ;; same lambda, and that they seem in practice to be
298 ;; previous-in-compilation-order blocks too, so we look back
299 ;; to find one which is sufficiently initialized to tell us
300 ;; what the home lambda is.
302 ;; We could get fancy about this, flooding through the
303 ;; graph of all the previous blocks, but in practice it
304 ;; seems to work just to grab the first previous block and
306 (node-home-lambda (block-last (first pred-list)))
307 ;; In case 2, we end up with an empty PRED-LIST and
308 ;; have to punt: There's no home lambda.
311 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
312 (defun block-home-lambda (block)
314 (block-home-lambda-or-null block)))
316 ;;; Return the IR1 physical environment for BLOCK.
317 (defun block-physenv (block)
318 (declare (type cblock block))
319 (lambda-physenv (block-home-lambda block)))
321 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
322 ;;; of its original source's top level form in its compilation unit.
323 (defun source-path-tlf-number (path)
324 (declare (list path))
327 ;;; Return the (reversed) list for the PATH in the original source
328 ;;; (with the Top Level Form number last).
329 (defun source-path-original-source (path)
330 (declare (list path) (inline member))
331 (cddr (member 'original-source-start path :test #'eq)))
333 ;;; Return the Form Number of PATH's original source inside the Top
334 ;;; Level Form that contains it. This is determined by the order that
335 ;;; we walk the subforms of the top level source form.
336 (defun source-path-form-number (path)
337 (declare (list path) (inline member))
338 (cadr (member 'original-source-start path :test #'eq)))
340 ;;; Return a list of all the enclosing forms not in the original
341 ;;; source that converted to get to this form, with the immediate
342 ;;; source for node at the start of the list.
343 (defun source-path-forms (path)
344 (subseq path 0 (position 'original-source-start path)))
346 ;;; Return the innermost source form for NODE.
347 (defun node-source-form (node)
348 (declare (type node node))
349 (let* ((path (node-source-path node))
350 (forms (source-path-forms path)))
353 (values (find-original-source path)))))
355 ;;; Return NODE-SOURCE-FORM, T if continuation has a single use,
356 ;;; otherwise NIL, NIL.
357 (defun continuation-source (cont)
358 (let ((use (continuation-use cont)))
360 (values (node-source-form use) t)
363 ;;; Return the LAMBDA that is CONT's home, or NIL if there is none.
364 (defun continuation-home-lambda-or-null (cont)
365 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
366 ;; implementation might not be quite right, or might be uglier than
367 ;; necessary. It appears that the original Python never found a need
368 ;; to do this operation. The obvious things based on
369 ;; NODE-HOME-LAMBDA of CONTINUATION-USE usually work; then if that
370 ;; fails, BLOCK-HOME-LAMBDA of CONTINUATION-BLOCK works, given that
371 ;; we generalize it enough to grovel harder when the simple CMU CL
372 ;; approach fails, and furthermore realize that in some exceptional
373 ;; cases it might return NIL. -- WHN 2001-12-04
374 (cond ((continuation-use cont)
375 (node-home-lambda (continuation-use cont)))
376 ((continuation-block cont)
377 (block-home-lambda-or-null (continuation-block cont)))
379 (bug "confused about home lambda for ~S"))))
381 ;;; Return the LAMBDA that is CONT's home.
382 (defun continuation-home-lambda (cont)
384 (continuation-home-lambda-or-null cont)))
386 ;;; Return a new LEXENV just like DEFAULT except for the specified
387 ;;; slot values. Values for the alist slots are NCONCed to the
388 ;;; beginning of the current value, rather than replacing it entirely.
389 (defun make-lexenv (&key (default *lexenv*)
390 funs vars blocks tags type-restrictions options
391 (lambda (lexenv-lambda default))
392 (cleanup (lexenv-cleanup default))
393 (policy (lexenv-policy default)))
394 (macrolet ((frob (var slot)
395 `(let ((old (,slot default)))
399 (internal-make-lexenv
400 (frob funs lexenv-funs)
401 (frob vars lexenv-vars)
402 (frob blocks lexenv-blocks)
403 (frob tags lexenv-tags)
404 (frob type-restrictions lexenv-type-restrictions)
405 lambda cleanup policy
406 (frob options lexenv-options))))
408 ;;;; flow/DFO/component hackery
410 ;;; Join BLOCK1 and BLOCK2.
411 (defun link-blocks (block1 block2)
412 (declare (type cblock block1 block2))
413 (setf (block-succ block1)
414 (if (block-succ block1)
415 (%link-blocks block1 block2)
417 (push block1 (block-pred block2))
419 (defun %link-blocks (block1 block2)
420 (declare (type cblock block1 block2) (inline member))
421 (let ((succ1 (block-succ block1)))
422 (aver (not (member block2 succ1 :test #'eq)))
423 (cons block2 succ1)))
425 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
426 ;;; this leaves a successor with a single predecessor that ends in an
427 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
428 ;;; now be able to be propagated to the successor.
429 (defun unlink-blocks (block1 block2)
430 (declare (type cblock block1 block2))
431 (let ((succ1 (block-succ block1)))
432 (if (eq block2 (car succ1))
433 (setf (block-succ block1) (cdr succ1))
434 (do ((succ (cdr succ1) (cdr succ))
436 ((eq (car succ) block2)
437 (setf (cdr prev) (cdr succ)))
440 (let ((new-pred (delq block1 (block-pred block2))))
441 (setf (block-pred block2) new-pred)
442 (when (and new-pred (null (rest new-pred)))
443 (let ((pred-block (first new-pred)))
444 (when (if-p (block-last pred-block))
445 (setf (block-test-modified pred-block) t)))))
448 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
449 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
450 ;;; consequent/alternative blocks to point to NEW. We also set
451 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
452 ;;; the new successor.
453 (defun change-block-successor (block old new)
454 (declare (type cblock new old block) (inline member))
455 (unlink-blocks block old)
456 (let ((last (block-last block))
457 (comp (block-component block)))
458 (setf (component-reanalyze comp) t)
461 (setf (block-test-modified block) t)
462 (let* ((succ-left (block-succ block))
463 (new (if (and (eq new (component-tail comp))
467 (unless (member new succ-left :test #'eq)
468 (link-blocks block new))
469 (macrolet ((frob (slot)
470 `(when (eq (,slot last) old)
471 (setf (,slot last) new))))
473 (frob if-alternative)
474 (when (eq (if-consequent last)
475 (if-alternative last))
476 (setf (component-reoptimize (block-component block)) t)))))
478 (unless (member new (block-succ block) :test #'eq)
479 (link-blocks block new)))))
483 ;;; Unlink a block from the next/prev chain. We also null out the
485 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
486 (defun remove-from-dfo (block)
487 (let ((next (block-next block))
488 (prev (block-prev block)))
489 (setf (block-component block) nil)
490 (setf (block-next prev) next)
491 (setf (block-prev next) prev))
494 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
495 ;;; COMPONENT to be the same as for AFTER.
496 (defun add-to-dfo (block after)
497 (declare (type cblock block after))
498 (let ((next (block-next after))
499 (comp (block-component after)))
500 (aver (not (eq (component-kind comp) :deleted)))
501 (setf (block-component block) comp)
502 (setf (block-next after) block)
503 (setf (block-prev block) after)
504 (setf (block-next block) next)
505 (setf (block-prev next) block))
508 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
509 ;;; the head and tail which are set to T.
510 (declaim (ftype (function (component) (values)) clear-flags))
511 (defun clear-flags (component)
512 (let ((head (component-head component))
513 (tail (component-tail component)))
514 (setf (block-flag head) t)
515 (setf (block-flag tail) t)
516 (do-blocks (block component)
517 (setf (block-flag block) nil)))
520 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
521 ;;; true in the head and tail blocks.
522 (declaim (ftype (function nil component) make-empty-component))
523 (defun make-empty-component ()
524 (let* ((head (make-block-key :start nil :component nil))
525 (tail (make-block-key :start nil :component nil))
526 (res (make-component :head head :tail tail)))
527 (setf (block-flag head) t)
528 (setf (block-flag tail) t)
529 (setf (block-component head) res)
530 (setf (block-component tail) res)
531 (setf (block-next head) tail)
532 (setf (block-prev tail) head)
535 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
536 ;;; The new block is added to the DFO immediately following NODE's block.
537 (defun node-ends-block (node)
538 (declare (type node node))
539 (let* ((block (node-block node))
540 (start (node-cont node))
541 (last (block-last block))
542 (last-cont (node-cont last)))
543 (unless (eq last node)
544 (aver (and (eq (continuation-kind start) :inside-block)
545 (not (block-delete-p block))))
546 (let* ((succ (block-succ block))
548 (make-block-key :start start
549 :component (block-component block)
550 :start-uses (list (continuation-use start))
551 :succ succ :last last)))
552 (setf (continuation-kind start) :block-start)
555 (cons new-block (remove block (block-pred b)))))
556 (setf (block-succ block) ())
557 (setf (block-last block) node)
558 (link-blocks block new-block)
559 (add-to-dfo new-block block)
560 (setf (component-reanalyze (block-component block)) t)
562 (do ((cont start (node-cont (continuation-next cont))))
564 (when (eq (continuation-kind last-cont) :inside-block)
565 (setf (continuation-block last-cont) new-block)))
566 (setf (continuation-block cont) new-block))
568 (setf (block-type-asserted block) t)
569 (setf (block-test-modified block) t))))
575 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
576 (defun delete-lambda-var (leaf)
577 (declare (type lambda-var leaf))
579 ;; Iterate over all local calls flushing the corresponding argument,
580 ;; allowing the computation of the argument to be deleted. We also
581 ;; mark the LET for reoptimization, since it may be that we have
582 ;; deleted its last variable.
583 (let* ((fun (lambda-var-home leaf))
584 (n (position leaf (lambda-vars fun))))
585 (dolist (ref (leaf-refs fun))
586 (let* ((cont (node-cont ref))
587 (dest (continuation-dest cont)))
588 (when (and (combination-p dest)
589 (eq (basic-combination-fun dest) cont)
590 (eq (basic-combination-kind dest) :local))
591 (let* ((args (basic-combination-args dest))
593 (reoptimize-continuation arg)
595 (setf (elt args n) nil))))))
597 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
598 ;; too much difficulty, since we can efficiently implement
599 ;; write-only variables. We iterate over the SETs, marking their
600 ;; blocks for dead code flushing, since we can delete SETs whose
602 (dolist (set (lambda-var-sets leaf))
603 (setf (block-flush-p (node-block set)) t))
607 ;;; Note that something interesting has happened to VAR.
608 (defun reoptimize-lambda-var (var)
609 (declare (type lambda-var var))
610 (let ((fun (lambda-var-home var)))
611 ;; We only deal with LET variables, marking the corresponding
612 ;; initial value arg as needing to be reoptimized.
613 (when (and (eq (functional-kind fun) :let)
615 (do ((args (basic-combination-args
618 (first (leaf-refs fun)))))
620 (vars (lambda-vars fun) (cdr vars)))
622 (reoptimize-continuation (car args))))))
625 ;;; Delete a function that has no references. This need only be called
626 ;;; on functions that never had any references, since otherwise
627 ;;; DELETE-REF will handle the deletion.
628 (defun delete-functional (fun)
629 (aver (and (null (leaf-refs fun))
630 (not (functional-entry-fun fun))))
632 (optional-dispatch (delete-optional-dispatch fun))
633 (clambda (delete-lambda fun)))
636 ;;; Deal with deleting the last reference to a CLAMBDA. Since there is
637 ;;; only one way into a CLAMBDA, deleting the last reference to a
638 ;;; CLAMBDA ensures that there is no way to reach any of the code in
639 ;;; it. So we just set the FUNCTIONAL-KIND for FUN and its LETs to
640 ;;; :DELETED, causing IR1 optimization to delete blocks in that
642 (defun delete-lambda (clambda)
643 (declare (type clambda clambda))
644 (let ((original-kind (functional-kind clambda))
645 (bind (lambda-bind clambda)))
646 (aver (not (member original-kind '(:deleted :optional :toplevel))))
647 (aver (not (functional-has-external-references-p clambda)))
648 (setf (functional-kind clambda) :deleted)
649 (setf (lambda-bind clambda) nil)
650 (dolist (let (lambda-lets clambda))
651 (setf (lambda-bind let) nil)
652 (setf (functional-kind let) :deleted))
654 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
655 ;; that we're using the old value of the KIND slot, not the
656 ;; current slot value, which has now been set to :DELETED.)
657 (if (member original-kind '(:let :mv-let :assignment))
658 (let ((home (lambda-home clambda)))
659 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
660 ;; If the function isn't a LET, we unlink the function head
661 ;; and tail from the component head and tail to indicate that
662 ;; the code is unreachable. We also delete the function from
663 ;; COMPONENT-LAMBDAS (it won't be there before local call
664 ;; analysis, but no matter.) If the lambda was never
665 ;; referenced, we give a note.
666 (let* ((bind-block (node-block bind))
667 (component (block-component bind-block))
668 (return (lambda-return clambda)))
669 (dolist (ref (lambda-refs clambda))
670 (let ((home (node-home-lambda ref)))
671 (aver (eq home clambda))))
672 (unless (leaf-ever-used clambda)
673 (let ((*compiler-error-context* bind))
674 (compiler-note "deleting unused function~:[.~;~:*~% ~S~]"
675 (leaf-debug-name clambda))))
676 (unlink-blocks (component-head component) bind-block)
678 (unlink-blocks (node-block return) (component-tail component)))
679 (setf (component-reanalyze component) t)
680 (let ((tails (lambda-tail-set clambda)))
681 (setf (tail-set-funs tails)
682 (delete clambda (tail-set-funs tails)))
683 (setf (lambda-tail-set clambda) nil))
684 (setf (component-lambdas component)
685 (delete clambda (component-lambdas component)))))
687 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
688 ;; ENTRY-FUN so that people will know that it is not an entry
690 (when (eq original-kind :external)
691 (let ((fun (functional-entry-fun clambda)))
692 (setf (functional-entry-fun fun) nil)
693 (when (optional-dispatch-p fun)
694 (delete-optional-dispatch fun)))))
698 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
699 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
700 ;;; is used both before and after local call analysis. Afterward, all
701 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
702 ;;; to the XEP, leaving it with no references at all. So we look at
703 ;;; the XEP to see whether an optional-dispatch is still really being
704 ;;; used. But before local call analysis, there are no XEPs, and all
705 ;;; references are direct.
707 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
708 ;;; entry-points, making them be normal lambdas, and then deleting the
709 ;;; ones with no references. This deletes any e-p lambdas that were
710 ;;; either never referenced, or couldn't be deleted when the last
711 ;;; reference was deleted (due to their :OPTIONAL kind.)
713 ;;; Note that the last optional entry point may alias the main entry,
714 ;;; so when we process the main entry, its KIND may have been changed
715 ;;; to NIL or even converted to a LETlike value.
716 (defun delete-optional-dispatch (leaf)
717 (declare (type optional-dispatch leaf))
718 (let ((entry (functional-entry-fun leaf)))
719 (unless (and entry (leaf-refs entry))
720 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
721 (setf (functional-kind leaf) :deleted)
724 (unless (eq (functional-kind fun) :deleted)
725 (aver (eq (functional-kind fun) :optional))
726 (setf (functional-kind fun) nil)
727 (let ((refs (leaf-refs fun)))
731 (or (maybe-let-convert fun)
732 (maybe-convert-to-assignment fun)))
734 (maybe-convert-to-assignment fun)))))))
736 (dolist (ep (optional-dispatch-entry-points leaf))
738 (when (optional-dispatch-more-entry leaf)
739 (frob (optional-dispatch-more-entry leaf)))
740 (let ((main (optional-dispatch-main-entry leaf)))
741 (when (eq (functional-kind main) :optional)
746 ;;; Do stuff to delete the semantic attachments of a REF node. When
747 ;;; this leaves zero or one reference, we do a type dispatch off of
748 ;;; the leaf to determine if a special action is appropriate.
749 (defun delete-ref (ref)
750 (declare (type ref ref))
751 (let* ((leaf (ref-leaf ref))
752 (refs (delete ref (leaf-refs leaf))))
753 (setf (leaf-refs leaf) refs)
758 (delete-lambda-var leaf))
760 (ecase (functional-kind leaf)
761 ((nil :let :mv-let :assignment :escape :cleanup)
762 (aver (null (functional-entry-fun leaf)))
763 (delete-lambda leaf))
765 (delete-lambda leaf))
766 ((:deleted :optional))))
768 (unless (eq (functional-kind leaf) :deleted)
769 (delete-optional-dispatch leaf)))))
772 (clambda (or (maybe-let-convert leaf)
773 (maybe-convert-to-assignment leaf)))
774 (lambda-var (reoptimize-lambda-var leaf))))
777 (clambda (maybe-convert-to-assignment leaf))))))
781 ;;; This function is called by people who delete nodes; it provides a
782 ;;; way to indicate that the value of a continuation is no longer
783 ;;; used. We null out the CONTINUATION-DEST, set FLUSH-P in the blocks
784 ;;; containing uses of CONT and set COMPONENT-REOPTIMIZE. If the PREV
785 ;;; of the use is deleted, then we blow off reoptimization.
787 ;;; If the continuation is :DELETED, then we don't do anything, since
788 ;;; all semantics have already been flushed. :DELETED-BLOCK-START
789 ;;; start continuations are treated just like :BLOCK-START; it is
790 ;;; possible that the continuation may be given a new dest (e.g. by
791 ;;; SUBSTITUTE-CONTINUATION), so we don't want to delete it.
792 (defun flush-dest (cont)
793 (declare (type continuation cont))
795 (unless (eq (continuation-kind cont) :deleted)
796 (aver (continuation-dest cont))
797 (setf (continuation-dest cont) nil)
798 (setf (continuation-%externally-checkable-type cont) nil)
800 (let ((prev (node-prev use)))
801 (unless (eq (continuation-kind prev) :deleted)
802 (let ((block (continuation-block prev)))
803 (setf (component-reoptimize (block-component block)) t)
804 (setf (block-attributep (block-flags block) flush-p type-asserted)
807 (setf (continuation-%type-check cont) nil)
811 ;;; Do a graph walk backward from BLOCK, marking all predecessor
812 ;;; blocks with the DELETE-P flag.
813 (defun mark-for-deletion (block)
814 (declare (type cblock block))
815 (unless (block-delete-p block)
816 (setf (block-delete-p block) t)
817 (setf (component-reanalyze (block-component block)) t)
818 (dolist (pred (block-pred block))
819 (mark-for-deletion pred)))
822 ;;; Delete CONT, eliminating both control and value semantics. We set
823 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here
824 ;;; we must get the component from the use block, since the
825 ;;; continuation may be a :DELETED-BLOCK-START.
827 ;;; If CONT has DEST, then it must be the case that the DEST is
828 ;;; unreachable, since we can't compute the value desired. In this
829 ;;; case, we call MARK-FOR-DELETION to cause the DEST block and its
830 ;;; predecessors to tell people to ignore them, and to cause them to
831 ;;; be deleted eventually.
832 (defun delete-continuation (cont)
833 (declare (type continuation cont))
834 (aver (not (eq (continuation-kind cont) :deleted)))
837 (let ((prev (node-prev use)))
838 (unless (eq (continuation-kind prev) :deleted)
839 (let ((block (continuation-block prev)))
840 (setf (block-attributep (block-flags block) flush-p type-asserted) t)
841 (setf (component-reoptimize (block-component block)) t)))))
843 (let ((dest (continuation-dest cont)))
845 (let ((prev (node-prev dest)))
847 (not (eq (continuation-kind prev) :deleted)))
848 (let ((block (continuation-block prev)))
849 (unless (block-delete-p block)
850 (mark-for-deletion block)))))))
852 (setf (continuation-kind cont) :deleted)
853 (setf (continuation-dest cont) nil)
854 (setf (continuation-%externally-checkable-type cont) nil)
855 (setf (continuation-next cont) nil)
856 (setf (continuation-asserted-type cont) *empty-type*)
857 (setf (continuation-%derived-type cont) *empty-type*)
858 (setf (continuation-use cont) nil)
859 (setf (continuation-block cont) nil)
860 (setf (continuation-reoptimize cont) nil)
861 (setf (continuation-%type-check cont) nil)
862 (setf (continuation-info cont) nil)
866 ;;; This function does what is necessary to eliminate the code in it
867 ;;; from the IR1 representation. This involves unlinking it from its
868 ;;; predecessors and successors and deleting various node-specific
869 ;;; semantic information.
871 ;;; We mark the START as has having no next and remove the last node
872 ;;; from its CONT's uses. We also flush the DEST for all continuations
873 ;;; whose values are received by nodes in the block.
874 (defun delete-block (block)
875 (declare (type cblock block))
876 (aver (block-component block)) ; else block is already deleted!
877 (note-block-deletion block)
878 (setf (block-delete-p block) t)
880 (let* ((last (block-last block))
881 (cont (node-cont last)))
882 (delete-continuation-use last)
883 (if (eq (continuation-kind cont) :unused)
884 (delete-continuation cont)
885 (reoptimize-continuation cont)))
887 (dolist (b (block-pred block))
888 (unlink-blocks b block)
889 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
890 ;; broken when successors were deleted without setting the
891 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
892 ;; doesn't happen again.
893 (aver (not (and (null (block-succ b))
894 (not (block-delete-p b))
895 (not (eq b (component-head (block-component b))))))))
896 (dolist (b (block-succ block))
897 (unlink-blocks block b))
899 (do-nodes (node cont block)
901 (ref (delete-ref node))
903 (flush-dest (if-test node)))
904 ;; The next two cases serve to maintain the invariant that a LET
905 ;; always has a well-formed COMBINATION, REF and BIND. We delete
906 ;; the lambda whenever we delete any of these, but we must be
907 ;; careful that this LET has not already been partially deleted.
909 (when (and (eq (basic-combination-kind node) :local)
910 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
911 (continuation-use (basic-combination-fun node)))
912 (let ((fun (combination-lambda node)))
913 ;; If our REF was the second-to-last ref, and has been
914 ;; deleted, then FUN may be a LET for some other
916 (when (and (functional-letlike-p fun)
917 (eq (let-combination fun) node))
918 (delete-lambda fun))))
919 (flush-dest (basic-combination-fun node))
920 (dolist (arg (basic-combination-args node))
921 (when arg (flush-dest arg))))
923 (let ((lambda (bind-lambda node)))
924 (unless (eq (functional-kind lambda) :deleted)
925 (aver (functional-somewhat-letlike-p lambda))
926 (delete-lambda lambda))))
928 (let ((value (exit-value node))
929 (entry (exit-entry node)))
933 (setf (entry-exits entry)
934 (delete node (entry-exits entry))))))
936 (flush-dest (return-result node))
937 (delete-return node))
939 (flush-dest (set-value node))
940 (let ((var (set-var node)))
941 (setf (basic-var-sets var)
942 (delete node (basic-var-sets var))))))
944 (delete-continuation (node-prev node)))
946 (remove-from-dfo block)
949 ;;; Do stuff to indicate that the return node Node is being deleted.
950 ;;; We set the RETURN to NIL.
951 (defun delete-return (node)
952 (declare (type creturn node))
953 (let ((fun (return-lambda node)))
954 (aver (lambda-return fun))
955 (setf (lambda-return fun) nil))
958 ;;; If any of the VARS in FUN was never referenced and was not
959 ;;; declared IGNORE, then complain.
960 (defun note-unreferenced-vars (fun)
961 (declare (type clambda fun))
962 (dolist (var (lambda-vars fun))
963 (unless (or (leaf-ever-used var)
964 (lambda-var-ignorep var))
965 (let ((*compiler-error-context* (lambda-bind fun)))
966 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
967 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
968 ;; requires this to be no more than a STYLE-WARNING.
969 (compiler-style-warn "The variable ~S is defined but never used."
970 (leaf-debug-name var)))
971 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
974 (defvar *deletion-ignored-objects* '(t nil))
976 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
977 ;;; our recursion so that we don't get lost in circular structures. We
978 ;;; ignore the car of forms if they are a symbol (to prevent confusing
979 ;;; function referencess with variables), and we also ignore anything
981 (defun present-in-form (obj form depth)
982 (declare (type (integer 0 20) depth))
983 (cond ((= depth 20) nil)
987 (let ((first (car form))
989 (if (member first '(quote function))
991 (or (and (not (symbolp first))
992 (present-in-form obj first depth))
993 (do ((l (cdr form) (cdr l))
995 ((or (atom l) (> n 100))
998 (when (present-in-form obj (car l) depth)
1001 ;;; This function is called on a block immediately before we delete
1002 ;;; it. We check to see whether any of the code about to die appeared
1003 ;;; in the original source, and emit a note if so.
1005 ;;; If the block was in a lambda is now deleted, then we ignore the
1006 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1007 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1008 ;;; reasonable for a function to not return, and there is a different
1009 ;;; note for that case anyway.
1011 ;;; If the actual source is an atom, then we use a bunch of heuristics
1012 ;;; to guess whether this reference really appeared in the original
1014 ;;; -- If a symbol, it must be interned and not a keyword.
1015 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1016 ;;; or a character.)
1017 ;;; -- The atom must be "present" in the original source form, and
1018 ;;; present in all intervening actual source forms.
1019 (defun note-block-deletion (block)
1020 (let ((home (block-home-lambda block)))
1021 (unless (eq (functional-kind home) :deleted)
1022 (do-nodes (node cont block)
1023 (let* ((path (node-source-path node))
1024 (first (first path)))
1025 (when (or (eq first 'original-source-start)
1027 (or (not (symbolp first))
1028 (let ((pkg (symbol-package first)))
1030 (not (eq pkg (symbol-package :end))))))
1031 (not (member first *deletion-ignored-objects*))
1032 (not (typep first '(or fixnum character)))
1034 (present-in-form first x 0))
1035 (source-path-forms path))
1036 (present-in-form first (find-original-source path)
1038 (unless (return-p node)
1039 (let ((*compiler-error-context* node))
1040 (compiler-note "deleting unreachable code")))
1044 ;;; Delete a node from a block, deleting the block if there are no
1045 ;;; nodes left. We remove the node from the uses of its CONT, but we
1046 ;;; don't deal with cleaning up any type-specific semantic
1047 ;;; attachments. If the CONT is :UNUSED after deleting this use, then
1048 ;;; we delete CONT. (Note :UNUSED is not the same as no uses. A
1049 ;;; continuation will only become :UNUSED if it was :INSIDE-BLOCK
1052 ;;; If the node is the last node, there must be exactly one successor.
1053 ;;; We link all of our precedessors to the successor and unlink the
1054 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1055 ;;; left, and the block is a successor of itself, then we replace the
1056 ;;; only node with a degenerate exit node. This provides a way to
1057 ;;; represent the bodyless infinite loop, given the prohibition on
1058 ;;; empty blocks in IR1.
1059 (defun unlink-node (node)
1060 (declare (type node node))
1061 (let* ((cont (node-cont node))
1062 (next (continuation-next cont))
1063 (prev (node-prev node))
1064 (block (continuation-block prev))
1065 (prev-kind (continuation-kind prev))
1066 (last (block-last block)))
1068 (unless (eq (continuation-kind cont) :deleted)
1069 (delete-continuation-use node)
1070 (when (eq (continuation-kind cont) :unused)
1071 (aver (not (continuation-dest cont)))
1072 (delete-continuation cont)))
1074 (setf (block-type-asserted block) t)
1075 (setf (block-test-modified block) t)
1077 (cond ((or (eq prev-kind :inside-block)
1078 (and (eq prev-kind :block-start)
1079 (not (eq node last))))
1080 (cond ((eq node last)
1081 (setf (block-last block) (continuation-use prev))
1082 (setf (continuation-next prev) nil))
1084 (setf (continuation-next prev) next)
1085 (setf (node-prev next) prev)))
1086 (setf (node-prev node) nil)
1089 (aver (eq prev-kind :block-start))
1090 (aver (eq node last))
1091 (let* ((succ (block-succ block))
1092 (next (first succ)))
1093 (aver (and succ (null (cdr succ))))
1095 ((member block succ)
1096 (with-ir1-environment-from-node node
1097 (let ((exit (make-exit))
1098 (dummy (make-continuation)))
1099 (setf (continuation-next prev) nil)
1100 (link-node-to-previous-continuation exit prev)
1101 (add-continuation-use exit dummy)
1102 (setf (block-last block) exit)))
1103 (setf (node-prev node) nil)
1106 (aver (eq (block-start-cleanup block)
1107 (block-end-cleanup block)))
1108 (unlink-blocks block next)
1109 (dolist (pred (block-pred block))
1110 (change-block-successor pred block next))
1111 (remove-from-dfo block)
1112 (cond ((continuation-dest prev)
1113 (setf (continuation-next prev) nil)
1114 (setf (continuation-kind prev) :deleted-block-start))
1116 (delete-continuation prev)))
1117 (setf (node-prev node) nil)
1120 ;;; Return true if NODE has been deleted, false if it is still a valid
1122 (defun node-deleted (node)
1123 (declare (type node node))
1124 (let ((prev (node-prev node)))
1126 (not (eq (continuation-kind prev) :deleted))
1127 (let ((block (continuation-block prev)))
1128 (and (block-component block)
1129 (not (block-delete-p block))))))))
1131 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1132 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1133 ;;; triggered by deletion.
1134 (defun delete-component (component)
1135 (declare (type component component))
1136 (aver (null (component-new-functionals component)))
1137 (setf (component-kind component) :deleted)
1138 (do-blocks (block component)
1139 (setf (block-delete-p block) t))
1140 (dolist (fun (component-lambdas component))
1141 (setf (functional-kind fun) nil)
1142 (setf (functional-entry-fun fun) nil)
1143 (setf (leaf-refs fun) nil)
1144 (delete-functional fun))
1145 (do-blocks (block component)
1146 (delete-block block))
1149 ;;; Convert code of the form
1150 ;;; (FOO ... (FUN ...) ...)
1152 ;;; (FOO ... ... ...).
1153 ;;; In other words, replace the function combination FUN by its
1154 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1155 ;;; to blow out of whatever transform called this. Note, as the number
1156 ;;; of arguments changes, the transform must be prepared to return a
1157 ;;; lambda with a new lambda-list with the correct number of
1159 (defun extract-fun-args (cont fun num-args)
1161 "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1162 to feed directly to the continuation-dest of CONT, which must be
1164 (declare (type continuation cont)
1166 (type index num-args))
1167 (let ((outside (continuation-dest cont))
1168 (inside (continuation-use cont)))
1169 (aver (combination-p outside))
1170 (unless (combination-p inside)
1171 (give-up-ir1-transform))
1172 (let ((inside-fun (combination-fun inside)))
1173 (unless (eq (continuation-fun-name inside-fun) fun)
1174 (give-up-ir1-transform))
1175 (let ((inside-args (combination-args inside)))
1176 (unless (= (length inside-args) num-args)
1177 (give-up-ir1-transform))
1178 (let* ((outside-args (combination-args outside))
1179 (arg-position (position cont outside-args))
1180 (before-args (subseq outside-args 0 arg-position))
1181 (after-args (subseq outside-args (1+ arg-position))))
1182 (dolist (arg inside-args)
1183 (setf (continuation-dest arg) outside)
1184 (setf (continuation-%externally-checkable-type arg) nil))
1185 (setf (combination-args inside) nil)
1186 (setf (combination-args outside)
1187 (append before-args inside-args after-args))
1188 (change-ref-leaf (continuation-use inside-fun)
1189 (find-free-fun 'list "???"))
1190 (setf (combination-kind inside) :full)
1191 (setf (node-derived-type inside) *wild-type*)
1193 (setf (continuation-asserted-type cont) *wild-type*)
1198 ;;; Change the LEAF that a REF refers to.
1199 (defun change-ref-leaf (ref leaf)
1200 (declare (type ref ref) (type leaf leaf))
1201 (unless (eq (ref-leaf ref) leaf)
1202 (push ref (leaf-refs leaf))
1204 (setf (ref-leaf ref) leaf)
1205 (let ((ltype (leaf-type leaf)))
1206 (if (fun-type-p ltype)
1207 (setf (node-derived-type ref) ltype)
1208 (derive-node-type ref ltype)))
1209 (reoptimize-continuation (node-cont ref)))
1212 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1213 (defun substitute-leaf (new-leaf old-leaf)
1214 (declare (type leaf new-leaf old-leaf))
1215 (dolist (ref (leaf-refs old-leaf))
1216 (change-ref-leaf ref new-leaf))
1219 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1220 ;;; whether to substitute
1221 (defun substitute-leaf-if (test new-leaf old-leaf)
1222 (declare (type leaf new-leaf old-leaf) (type function test))
1223 (dolist (ref (leaf-refs old-leaf))
1224 (when (funcall test ref)
1225 (change-ref-leaf ref new-leaf)))
1228 ;;; Return a LEAF which represents the specified constant object. If
1229 ;;; the object is not in *CONSTANTS*, then we create a new constant
1230 ;;; LEAF and enter it.
1231 (defun find-constant (object)
1233 ;; FIXME: What is the significance of this test? ("things
1234 ;; that are worth uniquifying"?)
1235 '(or symbol number character instance))
1236 (or (gethash object *constants*)
1237 (setf (gethash object *constants*)
1238 (make-constant :value object
1239 :%source-name '.anonymous.
1240 :type (ctype-of object)
1241 :where-from :defined)))
1242 (make-constant :value object
1243 :%source-name '.anonymous.
1244 :type (ctype-of object)
1245 :where-from :defined)))
1247 ;;; If there is a non-local exit noted in ENTRY's environment that
1248 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1249 (defun find-nlx-info (entry cont)
1250 (declare (type entry entry) (type continuation cont))
1251 (let ((entry-cleanup (entry-cleanup entry)))
1252 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1253 (when (and (eq (nlx-info-continuation nlx) cont)
1254 (eq (nlx-info-cleanup nlx) entry-cleanup))
1257 ;;;; functional hackery
1259 (declaim (ftype (function (functional) clambda) main-entry))
1260 (defun main-entry (functional)
1261 (etypecase functional
1262 (clambda functional)
1264 (optional-dispatch-main-entry functional))))
1266 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1267 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1268 ;;; optional with null default and no SUPPLIED-P. There must be a
1269 ;;; &REST arg with no references.
1270 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1271 (defun looks-like-an-mv-bind (functional)
1272 (and (optional-dispatch-p functional)
1273 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1275 (let ((info (lambda-var-arg-info (car arg))))
1276 (unless info (return nil))
1277 (case (arg-info-kind info)
1279 (when (or (arg-info-supplied-p info) (arg-info-default info))
1282 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1286 ;;; Return true if function is an external entry point. This is true
1287 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1288 ;;; (:TOPLEVEL kind.)
1290 (declare (type functional fun))
1291 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1293 ;;; If CONT's only use is a non-notinline global function reference,
1294 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1295 ;;; is true, then we don't care if the leaf is NOTINLINE.
1296 (defun continuation-fun-name (cont &optional notinline-ok)
1297 (declare (type continuation cont))
1298 (let ((use (continuation-use cont)))
1300 (let ((leaf (ref-leaf use)))
1301 (if (and (global-var-p leaf)
1302 (eq (global-var-kind leaf) :global-function)
1303 (or (not (defined-fun-p leaf))
1304 (not (eq (defined-fun-inlinep leaf) :notinline))
1306 (leaf-source-name leaf)
1310 ;;; Return the source name of a combination. (This is an idiom
1311 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1312 (defun combination-fun-source-name (combination)
1313 (let ((ref (continuation-use (combination-fun combination))))
1314 (leaf-source-name (ref-leaf ref))))
1316 ;;; Return the COMBINATION node that is the call to the LET FUN.
1317 (defun let-combination (fun)
1318 (declare (type clambda fun))
1319 (aver (functional-letlike-p fun))
1320 (continuation-dest (node-cont (first (leaf-refs fun)))))
1322 ;;; Return the initial value continuation for a LET variable, or NIL
1323 ;;; if there is none.
1324 (defun let-var-initial-value (var)
1325 (declare (type lambda-var var))
1326 (let ((fun (lambda-var-home var)))
1327 (elt (combination-args (let-combination fun))
1328 (position-or-lose var (lambda-vars fun)))))
1330 ;;; Return the LAMBDA that is called by the local CALL.
1331 (defun combination-lambda (call)
1332 (declare (type basic-combination call))
1333 (aver (eq (basic-combination-kind call) :local))
1334 (ref-leaf (continuation-use (basic-combination-fun call))))
1336 (defvar *inline-expansion-limit* 200
1338 "an upper limit on the number of inline function calls that will be expanded
1339 in any given code object (single function or block compilation)")
1341 ;;; Check whether NODE's component has exceeded its inline expansion
1342 ;;; limit, and warn if so, returning NIL.
1343 (defun inline-expansion-ok (node)
1344 (let ((expanded (incf (component-inline-expansions
1346 (node-block node))))))
1347 (cond ((> expanded *inline-expansion-limit*) nil)
1348 ((= expanded *inline-expansion-limit*)
1349 ;; FIXME: If the objective is to stop the recursive
1350 ;; expansion of inline functions, wouldn't it be more
1351 ;; correct to look back through surrounding expansions
1352 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1353 ;; possibly stored elsewhere too) and suppress expansion
1354 ;; and print this warning when the function being proposed
1355 ;; for inline expansion is found there? (I don't like the
1356 ;; arbitrary numerical limit in principle, and I think
1357 ;; it'll be a nuisance in practice if we ever want the
1358 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1359 ;; arbitrarily huge blocks of code. -- WHN)
1360 (let ((*compiler-error-context* node))
1361 (compiler-note "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1362 probably trying to~% ~
1363 inline a recursive function."
1364 *inline-expansion-limit*))
1370 ;;; Apply a function to some arguments, returning a list of the values
1371 ;;; resulting of the evaluation. If an error is signalled during the
1372 ;;; application, then we produce a warning message using WARN-FUN and
1373 ;;; return NIL as our second value to indicate this. NODE is used as
1374 ;;; the error context for any error message, and CONTEXT is a string
1375 ;;; that is spliced into the warning.
1376 (declaim (ftype (function ((or symbol function) list node function string)
1377 (values list boolean))
1379 (defun careful-call (function args node warn-fun context)
1381 (multiple-value-list
1382 (handler-case (apply function args)
1384 (let ((*compiler-error-context* node))
1385 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1386 (return-from careful-call (values nil nil))))))
1389 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1392 ((deffrob (basic careful compiler transform)
1394 (defun ,careful (specifier)
1395 (handler-case (,basic specifier)
1396 (simple-error (condition)
1397 (values nil (list* (simple-condition-format-control condition)
1398 (simple-condition-format-arguments condition))))))
1399 (defun ,compiler (specifier)
1400 (multiple-value-bind (type error-args) (,careful specifier)
1402 (apply #'compiler-error error-args))))
1403 (defun ,transform (specifier)
1404 (multiple-value-bind (type error-args) (,careful specifier)
1406 (apply #'give-up-ir1-transform
1408 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1409 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1412 ;;;; utilities used at run-time for parsing &KEY args in IR1
1414 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1415 ;;; the continuation for the value of the &KEY argument KEY in the
1416 ;;; list of continuations ARGS. It returns the continuation if the
1417 ;;; keyword is present, or NIL otherwise. The legality and
1418 ;;; constantness of the keywords should already have been checked.
1419 (declaim (ftype (function (list keyword) (or continuation null))
1420 find-keyword-continuation))
1421 (defun find-keyword-continuation (args key)
1422 (do ((arg args (cddr arg)))
1424 (when (eq (continuation-value (first arg)) key)
1425 (return (second arg)))))
1427 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1428 ;;; verify that alternating continuations in ARGS are constant and
1429 ;;; that there is an even number of args.
1430 (declaim (ftype (function (list) boolean) check-key-args-constant))
1431 (defun check-key-args-constant (args)
1432 (do ((arg args (cddr arg)))
1434 (unless (and (rest arg)
1435 (constant-continuation-p (first arg)))
1438 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1439 ;;; verify that the list of continuations ARGS is a well-formed &KEY
1440 ;;; arglist and that only keywords present in the list KEYS are
1442 (declaim (ftype (function (list list) boolean) check-transform-keys))
1443 (defun check-transform-keys (args keys)
1444 (and (check-key-args-constant args)
1445 (do ((arg args (cddr arg)))
1447 (unless (member (continuation-value (first arg)) keys)
1452 ;;; Called by the expansion of the EVENT macro.
1453 (declaim (ftype (function (event-info (or node null)) *) %event))
1454 (defun %event (info node)
1455 (incf (event-info-count info))
1456 (when (and (>= (event-info-level info) *event-note-threshold*)
1457 (policy (or node *lexenv*)
1458 (= inhibit-warnings 0)))
1459 (let ((*compiler-error-context* node))
1460 (compiler-note (event-info-description info))))
1462 (let ((action (event-info-action info)))
1463 (when action (funcall action node))))