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 (with-component-last-block (*current-component*
41 (block-next (component-head *current-component*)))
42 (let* ((start (make-continuation))
43 (block (continuation-starts-block start))
44 (cont (make-continuation))
46 (make-lexenv :cleanup cleanup)
48 (change-block-successor block1 block2 block)
49 (link-blocks block block2)
50 (ir1-convert start cont form)
51 (setf (block-last block) (continuation-use cont))
54 ;;;; continuation use hacking
56 ;;; Return a list of all the nodes which use Cont.
57 (declaim (ftype (function (continuation) list) find-uses))
58 (defun find-uses (cont)
59 (ecase (continuation-kind cont)
60 ((:block-start :deleted-block-start)
61 (block-start-uses (continuation-block cont)))
62 (:inside-block (list (continuation-use cont)))
66 ;;; Update continuation use information so that NODE is no longer a
67 ;;; use of its CONT. If the old continuation doesn't start its block,
68 ;;; then we don't update the BLOCK-START-USES, since it will be
69 ;;; deleted when we are done.
71 ;;; Note: if you call this function, you may have to do a
72 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
74 (declaim (ftype (function (node) (values)) delete-continuation-use))
75 (defun delete-continuation-use (node)
76 (let* ((cont (node-cont node))
77 (block (continuation-block cont)))
78 (ecase (continuation-kind cont)
80 ((:block-start :deleted-block-start)
81 (let ((uses (delete node (block-start-uses block))))
82 (setf (block-start-uses block) uses)
83 (setf (continuation-use cont)
84 (if (cdr uses) nil (car uses)))))
86 (setf (continuation-kind cont) :unused)
87 (setf (continuation-block cont) nil)
88 (setf (continuation-use cont) nil)
89 (setf (continuation-next cont) nil)))
90 (setf (node-cont node) nil))
93 ;;; Update continuation use information so that NODE uses CONT. If
94 ;;; CONT is :UNUSED, then we set its block to NODE's NODE-BLOCK (which
97 ;;; Note: if you call this function, you may have to do a
98 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
100 (declaim (ftype (function (node continuation) (values)) add-continuation-use))
101 (defun add-continuation-use (node cont)
102 (aver (not (node-cont node)))
103 (let ((block (continuation-block cont)))
104 (ecase (continuation-kind cont)
108 (let ((block (node-block node)))
110 (setf (continuation-block cont) block))
111 (setf (continuation-kind cont) :inside-block)
112 (setf (continuation-use cont) node))
113 ((:block-start :deleted-block-start)
114 (let ((uses (cons node (block-start-uses block))))
115 (setf (block-start-uses block) uses)
116 (setf (continuation-use cont)
117 (if (cdr uses) nil (car uses)))))))
118 (setf (node-cont node) cont)
121 ;;; Return true if CONT is the NODE-CONT for NODE and CONT is
122 ;;; transferred to immediately after the evaluation of NODE.
123 (defun immediately-used-p (cont node)
124 (declare (type continuation cont) (type node node))
125 (and (eq (node-cont node) cont)
126 (not (eq (continuation-kind cont) :deleted))
127 (let ((cblock (continuation-block cont))
128 (nblock (node-block node)))
129 (or (eq cblock nblock)
130 (let ((succ (block-succ nblock)))
131 (and (= (length succ) 1)
132 (eq (first succ) cblock)))))))
134 ;;;; continuation substitution
136 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
137 ;;; NIL. When we are done, we call FLUSH-DEST on OLD to clear its DEST
138 ;;; and to note potential optimization opportunities.
139 (defun substitute-continuation (new old)
140 (declare (type continuation old new))
141 (aver (not (continuation-dest new)))
142 (let ((dest (continuation-dest old)))
145 (cif (setf (if-test dest) new))
146 (cset (setf (set-value dest) new))
147 (creturn (setf (return-result dest) new))
148 (exit (setf (exit-value dest) new))
150 (if (eq old (basic-combination-fun dest))
151 (setf (basic-combination-fun dest) new)
152 (setf (basic-combination-args dest)
153 (nsubst new old (basic-combination-args dest))))))
156 (setf (continuation-dest new) dest)
157 (setf (continuation-%externally-checkable-type new) nil))
160 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
161 ;;; arbitary number of uses. If NEW will end up with more than one
162 ;;; use, then we must arrange for it to start a block if it doesn't
164 (defun substitute-continuation-uses (new old)
165 (declare (type continuation old new))
166 (unless (and (eq (continuation-kind new) :unused)
167 (eq (continuation-kind old) :inside-block))
168 (ensure-block-start new))
171 (delete-continuation-use node)
172 (add-continuation-use node new))
173 (dolist (lexenv-use (continuation-lexenv-uses old))
174 (setf (cadr lexenv-use) new))
176 (reoptimize-continuation new)
179 ;;;; block starting/creation
181 ;;; Return the block that CONT is the start of, making a block if
182 ;;; necessary. This function is called by IR1 translators which may
183 ;;; cause a continuation to be used more than once. Every continuation
184 ;;; which may be used more than once must start a block by the time
185 ;;; that anyone does a USE-CONTINUATION on it.
187 ;;; We also throw the block into the next/prev list for the
188 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
190 (defun continuation-starts-block (cont)
191 (declare (type continuation cont))
192 (ecase (continuation-kind cont)
194 (aver (not (continuation-block cont)))
195 (let* ((next (component-last-block *current-component*))
196 (prev (block-prev next))
197 (new-block (make-block cont)))
198 (setf (block-next new-block) next
199 (block-prev new-block) prev
200 (block-prev next) new-block
201 (block-next prev) new-block
202 (continuation-block cont) new-block
203 (continuation-use cont) nil
204 (continuation-kind cont) :block-start)
207 (continuation-block cont))))
209 ;;; Ensure that CONT is the start of a block (or deleted) so that
210 ;;; the use set can be freely manipulated.
211 ;;; -- If the continuation is :UNUSED or is :INSIDE-BLOCK and the
212 ;;; CONT of LAST in its block, then we make it the start of a new
214 ;;; -- If the continuation is :INSIDE-BLOCK inside a block, then we
215 ;;; split the block using NODE-ENDS-BLOCK, which makes the
216 ;;; continuation be a :BLOCK-START.
217 (defun ensure-block-start (cont)
218 (declare (type continuation cont))
219 (let ((kind (continuation-kind cont)))
221 ((:deleted :block-start :deleted-block-start))
222 ((:unused :inside-block)
223 (let ((block (continuation-block cont)))
224 (cond ((or (eq kind :unused)
225 (eq (node-cont (block-last block)) cont))
226 (setf (continuation-block cont)
227 (make-block-key :start cont
229 :start-uses (find-uses cont)))
230 (setf (continuation-kind cont) :deleted-block-start))
232 (node-ends-block (continuation-use cont))))))))
235 ;;;; miscellaneous shorthand functions
237 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
238 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
239 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
240 ;;; deleted, and then return its home.
241 (defun node-home-lambda (node)
242 (declare (type node node))
243 (do ((fun (lexenv-lambda (node-lexenv node))
244 (lexenv-lambda (lambda-call-lexenv fun))))
245 ((not (eq (functional-kind fun) :deleted))
247 (when (eq (lambda-home fun) fun)
250 (defun node-block (node)
251 (declare (type node node))
252 (the cblock (continuation-block (node-prev node))))
253 (defun node-component (node)
254 (declare (type node node))
255 (block-component (node-block node)))
256 (defun node-physenv (node)
257 (declare (type node node))
258 (the physenv (lambda-physenv (node-home-lambda node))))
260 (defun lambda-block (clambda)
261 (declare (type clambda clambda))
262 (node-block (lambda-bind clambda)))
263 (defun lambda-component (clambda)
264 (block-component (lambda-block clambda)))
266 ;;; Return the enclosing cleanup for environment of the first or last
268 (defun block-start-cleanup (block)
269 (declare (type cblock block))
270 (node-enclosing-cleanup (continuation-next (block-start block))))
271 (defun block-end-cleanup (block)
272 (declare (type cblock block))
273 (node-enclosing-cleanup (block-last block)))
275 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
276 ;;; if there is none.
278 ;;; There can legitimately be no home lambda in dead code early in the
279 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
280 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
281 ;;; where the block is just a placeholder during parsing and doesn't
282 ;;; actually correspond to code which will be written anywhere.
283 (defun block-home-lambda-or-null (block)
284 (declare (type cblock block))
285 (if (node-p (block-last block))
286 ;; This is the old CMU CL way of doing it.
287 (node-home-lambda (block-last block))
288 ;; Now that SBCL uses this operation more aggressively than CMU
289 ;; CL did, the old CMU CL way of doing it can fail in two ways.
290 ;; 1. It can fail in a few cases even when a meaningful home
291 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
293 ;; 2. It can fail when converting a form which is born orphaned
294 ;; so that it never had a meaningful home lambda, e.g. a form
295 ;; which follows a RETURN-FROM or GO form.
296 (let ((pred-list (block-pred block)))
297 ;; To deal with case 1, we reason that
298 ;; previous-in-target-execution-order blocks should be in the
299 ;; same lambda, and that they seem in practice to be
300 ;; previous-in-compilation-order blocks too, so we look back
301 ;; to find one which is sufficiently initialized to tell us
302 ;; what the home lambda is.
304 ;; We could get fancy about this, flooding through the
305 ;; graph of all the previous blocks, but in practice it
306 ;; seems to work just to grab the first previous block and
308 (node-home-lambda (block-last (first pred-list)))
309 ;; In case 2, we end up with an empty PRED-LIST and
310 ;; have to punt: There's no home lambda.
313 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
314 (defun block-home-lambda (block)
316 (block-home-lambda-or-null block)))
318 ;;; Return the IR1 physical environment for BLOCK.
319 (defun block-physenv (block)
320 (declare (type cblock block))
321 (lambda-physenv (block-home-lambda block)))
323 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
324 ;;; of its original source's top level form in its compilation unit.
325 (defun source-path-tlf-number (path)
326 (declare (list path))
329 ;;; Return the (reversed) list for the PATH in the original source
330 ;;; (with the Top Level Form number last).
331 (defun source-path-original-source (path)
332 (declare (list path) (inline member))
333 (cddr (member 'original-source-start path :test #'eq)))
335 ;;; Return the Form Number of PATH's original source inside the Top
336 ;;; Level Form that contains it. This is determined by the order that
337 ;;; we walk the subforms of the top level source form.
338 (defun source-path-form-number (path)
339 (declare (list path) (inline member))
340 (cadr (member 'original-source-start path :test #'eq)))
342 ;;; Return a list of all the enclosing forms not in the original
343 ;;; source that converted to get to this form, with the immediate
344 ;;; source for node at the start of the list.
345 (defun source-path-forms (path)
346 (subseq path 0 (position 'original-source-start path)))
348 ;;; Return the innermost source form for NODE.
349 (defun node-source-form (node)
350 (declare (type node node))
351 (let* ((path (node-source-path node))
352 (forms (source-path-forms path)))
355 (values (find-original-source path)))))
357 ;;; Return NODE-SOURCE-FORM, T if continuation has a single use,
358 ;;; otherwise NIL, NIL.
359 (defun continuation-source (cont)
360 (let ((use (continuation-use cont)))
362 (values (node-source-form use) t)
365 ;;; Return the LAMBDA that is CONT's home, or NIL if there is none.
366 (defun continuation-home-lambda-or-null (cont)
367 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
368 ;; implementation might not be quite right, or might be uglier than
369 ;; necessary. It appears that the original Python never found a need
370 ;; to do this operation. The obvious things based on
371 ;; NODE-HOME-LAMBDA of CONTINUATION-USE usually work; then if that
372 ;; fails, BLOCK-HOME-LAMBDA of CONTINUATION-BLOCK works, given that
373 ;; we generalize it enough to grovel harder when the simple CMU CL
374 ;; approach fails, and furthermore realize that in some exceptional
375 ;; cases it might return NIL. -- WHN 2001-12-04
376 (cond ((continuation-use cont)
377 (node-home-lambda (continuation-use cont)))
378 ((continuation-block cont)
379 (block-home-lambda-or-null (continuation-block cont)))
381 (bug "confused about home lambda for ~S"))))
383 ;;; Return the LAMBDA that is CONT's home.
384 (defun continuation-home-lambda (cont)
386 (continuation-home-lambda-or-null cont)))
388 #!-sb-fluid (declaim (inline continuation-single-value-p))
389 (defun continuation-single-value-p (cont)
390 (not (typep (continuation-dest cont)
391 '(or creturn exit mv-combination))))
393 ;;; Return a new LEXENV just like DEFAULT except for the specified
394 ;;; slot values. Values for the alist slots are NCONCed to the
395 ;;; beginning of the current value, rather than replacing it entirely.
396 (defun make-lexenv (&key (default *lexenv*)
397 funs vars blocks tags
398 type-restrictions weakend-type-restrictions
399 (lambda (lexenv-lambda default))
400 (cleanup (lexenv-cleanup default))
401 (policy (lexenv-policy default)))
402 (macrolet ((frob (var slot)
403 `(let ((old (,slot default)))
407 (internal-make-lexenv
408 (frob funs lexenv-funs)
409 (frob vars lexenv-vars)
410 (frob blocks lexenv-blocks)
411 (frob tags lexenv-tags)
412 (frob type-restrictions lexenv-type-restrictions)
413 (frob weakend-type-restrictions lexenv-weakend-type-restrictions)
414 lambda cleanup policy)))
416 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
418 (defun make-restricted-lexenv (lexenv)
419 (flet ((fun-good-p (fun)
420 (destructuring-bind (name . thing) fun
421 (declare (ignore name))
425 (cons (aver (eq (car thing) 'macro))
428 (destructuring-bind (name . thing) var
429 (declare (ignore name))
432 (cons (aver (eq (car thing) 'macro))
434 (heap-alien-info nil)))))
435 (internal-make-lexenv
436 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
437 (remove-if-not #'var-good-p (lexenv-vars lexenv))
440 (lexenv-type-restrictions lexenv) ; XXX
441 (lexenv-weakend-type-restrictions lexenv)
444 (lexenv-policy lexenv))))
446 ;;;; flow/DFO/component hackery
448 ;;; Join BLOCK1 and BLOCK2.
449 (defun link-blocks (block1 block2)
450 (declare (type cblock block1 block2))
451 (setf (block-succ block1)
452 (if (block-succ block1)
453 (%link-blocks block1 block2)
455 (push block1 (block-pred block2))
457 (defun %link-blocks (block1 block2)
458 (declare (type cblock block1 block2) (inline member))
459 (let ((succ1 (block-succ block1)))
460 (aver (not (member block2 succ1 :test #'eq)))
461 (cons block2 succ1)))
463 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
464 ;;; this leaves a successor with a single predecessor that ends in an
465 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
466 ;;; now be able to be propagated to the successor.
467 (defun unlink-blocks (block1 block2)
468 (declare (type cblock block1 block2))
469 (let ((succ1 (block-succ block1)))
470 (if (eq block2 (car succ1))
471 (setf (block-succ block1) (cdr succ1))
472 (do ((succ (cdr succ1) (cdr succ))
474 ((eq (car succ) block2)
475 (setf (cdr prev) (cdr succ)))
478 (let ((new-pred (delq block1 (block-pred block2))))
479 (setf (block-pred block2) new-pred)
480 (when (and new-pred (null (rest new-pred)))
481 (let ((pred-block (first new-pred)))
482 (when (if-p (block-last pred-block))
483 (setf (block-test-modified pred-block) t)))))
486 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
487 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
488 ;;; consequent/alternative blocks to point to NEW. We also set
489 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
490 ;;; the new successor.
491 (defun change-block-successor (block old new)
492 (declare (type cblock new old block) (inline member))
493 (unlink-blocks block old)
494 (let ((last (block-last block))
495 (comp (block-component block)))
496 (setf (component-reanalyze comp) t)
499 (setf (block-test-modified block) t)
500 (let* ((succ-left (block-succ block))
501 (new (if (and (eq new (component-tail comp))
505 (unless (member new succ-left :test #'eq)
506 (link-blocks block new))
507 (macrolet ((frob (slot)
508 `(when (eq (,slot last) old)
509 (setf (,slot last) new))))
511 (frob if-alternative)
512 (when (eq (if-consequent last)
513 (if-alternative last))
514 (setf (component-reoptimize (block-component block)) t)))))
516 (unless (member new (block-succ block) :test #'eq)
517 (link-blocks block new)))))
521 ;;; Unlink a block from the next/prev chain. We also null out the
523 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
524 (defun remove-from-dfo (block)
525 (let ((next (block-next block))
526 (prev (block-prev block)))
527 (setf (block-component block) nil)
528 (setf (block-next prev) next)
529 (setf (block-prev next) prev))
532 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
533 ;;; COMPONENT to be the same as for AFTER.
534 (defun add-to-dfo (block after)
535 (declare (type cblock block after))
536 (let ((next (block-next after))
537 (comp (block-component after)))
538 (aver (not (eq (component-kind comp) :deleted)))
539 (setf (block-component block) comp)
540 (setf (block-next after) block)
541 (setf (block-prev block) after)
542 (setf (block-next block) next)
543 (setf (block-prev next) block))
546 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
547 ;;; the head and tail which are set to T.
548 (declaim (ftype (function (component) (values)) clear-flags))
549 (defun clear-flags (component)
550 (let ((head (component-head component))
551 (tail (component-tail component)))
552 (setf (block-flag head) t)
553 (setf (block-flag tail) t)
554 (do-blocks (block component)
555 (setf (block-flag block) nil)))
558 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
559 ;;; true in the head and tail blocks.
560 (declaim (ftype (function nil component) make-empty-component))
561 (defun make-empty-component ()
562 (let* ((head (make-block-key :start nil :component nil))
563 (tail (make-block-key :start nil :component nil))
564 (res (make-component head tail)))
565 (setf (block-flag head) t)
566 (setf (block-flag tail) t)
567 (setf (block-component head) res)
568 (setf (block-component tail) res)
569 (setf (block-next head) tail)
570 (setf (block-prev tail) head)
573 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
574 ;;; The new block is added to the DFO immediately following NODE's block.
575 (defun node-ends-block (node)
576 (declare (type node node))
577 (let* ((block (node-block node))
578 (start (node-cont node))
579 (last (block-last block))
580 (last-cont (node-cont last)))
581 (unless (eq last node)
582 (aver (and (eq (continuation-kind start) :inside-block)
583 (not (block-delete-p block))))
584 (let* ((succ (block-succ block))
586 (make-block-key :start start
587 :component (block-component block)
588 :start-uses (list (continuation-use start))
589 :succ succ :last last)))
590 (setf (continuation-kind start) :block-start)
593 (cons new-block (remove block (block-pred b)))))
594 (setf (block-succ block) ())
595 (setf (block-last block) node)
596 (link-blocks block new-block)
597 (add-to-dfo new-block block)
598 (setf (component-reanalyze (block-component block)) t)
600 (do ((cont start (node-cont (continuation-next cont))))
602 (when (eq (continuation-kind last-cont) :inside-block)
603 (setf (continuation-block last-cont) new-block)))
604 (setf (continuation-block cont) new-block))
606 (setf (block-type-asserted block) t)
607 (setf (block-test-modified block) t))))
613 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
614 (defun delete-lambda-var (leaf)
615 (declare (type lambda-var leaf))
617 ;; Iterate over all local calls flushing the corresponding argument,
618 ;; allowing the computation of the argument to be deleted. We also
619 ;; mark the LET for reoptimization, since it may be that we have
620 ;; deleted its last variable.
621 (let* ((fun (lambda-var-home leaf))
622 (n (position leaf (lambda-vars fun))))
623 (dolist (ref (leaf-refs fun))
624 (let* ((cont (node-cont ref))
625 (dest (continuation-dest cont)))
626 (when (and (combination-p dest)
627 (eq (basic-combination-fun dest) cont)
628 (eq (basic-combination-kind dest) :local))
629 (let* ((args (basic-combination-args dest))
631 (reoptimize-continuation arg)
633 (setf (elt args n) nil))))))
635 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
636 ;; too much difficulty, since we can efficiently implement
637 ;; write-only variables. We iterate over the SETs, marking their
638 ;; blocks for dead code flushing, since we can delete SETs whose
640 (dolist (set (lambda-var-sets leaf))
641 (setf (block-flush-p (node-block set)) t))
645 ;;; Note that something interesting has happened to VAR.
646 (defun reoptimize-lambda-var (var)
647 (declare (type lambda-var var))
648 (let ((fun (lambda-var-home var)))
649 ;; We only deal with LET variables, marking the corresponding
650 ;; initial value arg as needing to be reoptimized.
651 (when (and (eq (functional-kind fun) :let)
653 (do ((args (basic-combination-args
656 (first (leaf-refs fun)))))
658 (vars (lambda-vars fun) (cdr vars)))
660 (reoptimize-continuation (car args))))))
663 ;;; Delete a function that has no references. This need only be called
664 ;;; on functions that never had any references, since otherwise
665 ;;; DELETE-REF will handle the deletion.
666 (defun delete-functional (fun)
667 (aver (and (null (leaf-refs fun))
668 (not (functional-entry-fun fun))))
670 (optional-dispatch (delete-optional-dispatch fun))
671 (clambda (delete-lambda fun)))
674 ;;; Deal with deleting the last reference to a CLAMBDA. Since there is
675 ;;; only one way into a CLAMBDA, deleting the last reference to a
676 ;;; CLAMBDA ensures that there is no way to reach any of the code in
677 ;;; it. So we just set the FUNCTIONAL-KIND for FUN and its LETs to
678 ;;; :DELETED, causing IR1 optimization to delete blocks in that
680 (defun delete-lambda (clambda)
681 (declare (type clambda clambda))
682 (let ((original-kind (functional-kind clambda))
683 (bind (lambda-bind clambda)))
684 (aver (not (member original-kind '(:deleted :optional :toplevel))))
685 (aver (not (functional-has-external-references-p clambda)))
686 (setf (functional-kind clambda) :deleted)
687 (setf (lambda-bind clambda) nil)
688 (dolist (let (lambda-lets clambda))
689 (setf (lambda-bind let) nil)
690 (setf (functional-kind let) :deleted))
692 ;; LET may be deleted if its BIND is unreachable. Autonomous
693 ;; function may be deleted if it has no reachable references.
694 (unless (member original-kind '(:let :mv-let :assignment))
695 (dolist (ref (lambda-refs clambda))
696 (mark-for-deletion (node-block ref))))
698 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
699 ;; that we're using the old value of the KIND slot, not the
700 ;; current slot value, which has now been set to :DELETED.)
701 (if (member original-kind '(:let :mv-let :assignment))
702 (let ((home (lambda-home clambda)))
703 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
704 ;; If the function isn't a LET, we unlink the function head
705 ;; and tail from the component head and tail to indicate that
706 ;; the code is unreachable. We also delete the function from
707 ;; COMPONENT-LAMBDAS (it won't be there before local call
708 ;; analysis, but no matter.) If the lambda was never
709 ;; referenced, we give a note.
710 (let* ((bind-block (node-block bind))
711 (component (block-component bind-block))
712 (return (lambda-return clambda))
713 (return-block (and return (node-block return))))
714 (unless (leaf-ever-used clambda)
715 (let ((*compiler-error-context* bind))
716 (compiler-note "deleting unused function~:[.~;~:*~% ~S~]"
717 (leaf-debug-name clambda))))
718 (unless (block-delete-p bind-block)
719 (unlink-blocks (component-head component) bind-block))
720 (when (and return-block (not (block-delete-p return-block)))
721 (mark-for-deletion return-block)
722 (unlink-blocks return-block (component-tail component)))
723 (setf (component-reanalyze component) t)
724 (let ((tails (lambda-tail-set clambda)))
725 (setf (tail-set-funs tails)
726 (delete clambda (tail-set-funs tails)))
727 (setf (lambda-tail-set clambda) nil))
728 (setf (component-lambdas component)
729 (delete clambda (component-lambdas component)))))
731 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
732 ;; ENTRY-FUN so that people will know that it is not an entry
734 (when (eq original-kind :external)
735 (let ((fun (functional-entry-fun clambda)))
736 (setf (functional-entry-fun fun) nil)
737 (when (optional-dispatch-p fun)
738 (delete-optional-dispatch fun)))))
742 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
743 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
744 ;;; is used both before and after local call analysis. Afterward, all
745 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
746 ;;; to the XEP, leaving it with no references at all. So we look at
747 ;;; the XEP to see whether an optional-dispatch is still really being
748 ;;; used. But before local call analysis, there are no XEPs, and all
749 ;;; references are direct.
751 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
752 ;;; entry-points, making them be normal lambdas, and then deleting the
753 ;;; ones with no references. This deletes any e-p lambdas that were
754 ;;; either never referenced, or couldn't be deleted when the last
755 ;;; reference was deleted (due to their :OPTIONAL kind.)
757 ;;; Note that the last optional entry point may alias the main entry,
758 ;;; so when we process the main entry, its KIND may have been changed
759 ;;; to NIL or even converted to a LETlike value.
760 (defun delete-optional-dispatch (leaf)
761 (declare (type optional-dispatch leaf))
762 (let ((entry (functional-entry-fun leaf)))
763 (unless (and entry (leaf-refs entry))
764 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
765 (setf (functional-kind leaf) :deleted)
768 (unless (eq (functional-kind fun) :deleted)
769 (aver (eq (functional-kind fun) :optional))
770 (setf (functional-kind fun) nil)
771 (let ((refs (leaf-refs fun)))
775 (or (maybe-let-convert fun)
776 (maybe-convert-to-assignment fun)))
778 (maybe-convert-to-assignment fun)))))))
780 (dolist (ep (optional-dispatch-entry-points leaf))
782 (when (optional-dispatch-more-entry leaf)
783 (frob (optional-dispatch-more-entry leaf)))
784 (let ((main (optional-dispatch-main-entry leaf)))
785 (when (eq (functional-kind main) :optional)
790 ;;; Do stuff to delete the semantic attachments of a REF node. When
791 ;;; this leaves zero or one reference, we do a type dispatch off of
792 ;;; the leaf to determine if a special action is appropriate.
793 (defun delete-ref (ref)
794 (declare (type ref ref))
795 (let* ((leaf (ref-leaf ref))
796 (refs (delete ref (leaf-refs leaf))))
797 (setf (leaf-refs leaf) refs)
802 (delete-lambda-var leaf))
804 (ecase (functional-kind leaf)
805 ((nil :let :mv-let :assignment :escape :cleanup)
806 (aver (null (functional-entry-fun leaf)))
807 (delete-lambda leaf))
809 (delete-lambda leaf))
810 ((:deleted :optional))))
812 (unless (eq (functional-kind leaf) :deleted)
813 (delete-optional-dispatch leaf)))))
816 (clambda (or (maybe-let-convert leaf)
817 (maybe-convert-to-assignment leaf)))
818 (lambda-var (reoptimize-lambda-var leaf))))
821 (clambda (maybe-convert-to-assignment leaf))))))
825 ;;; This function is called by people who delete nodes; it provides a
826 ;;; way to indicate that the value of a continuation is no longer
827 ;;; used. We null out the CONTINUATION-DEST, set FLUSH-P in the blocks
828 ;;; containing uses of CONT and set COMPONENT-REOPTIMIZE. If the PREV
829 ;;; of the use is deleted, then we blow off reoptimization.
831 ;;; If the continuation is :DELETED, then we don't do anything, since
832 ;;; all semantics have already been flushed. :DELETED-BLOCK-START
833 ;;; start continuations are treated just like :BLOCK-START; it is
834 ;;; possible that the continuation may be given a new dest (e.g. by
835 ;;; SUBSTITUTE-CONTINUATION), so we don't want to delete it.
836 (defun flush-dest (cont)
837 (declare (type continuation cont))
839 (unless (eq (continuation-kind cont) :deleted)
840 (aver (continuation-dest cont))
841 (setf (continuation-dest cont) nil)
842 (setf (continuation-%externally-checkable-type cont) nil)
844 (let ((prev (node-prev use)))
845 (unless (eq (continuation-kind prev) :deleted)
846 (let ((block (continuation-block prev)))
847 (setf (component-reoptimize (block-component block)) t)
848 (setf (block-attributep (block-flags block) flush-p type-asserted)
851 (setf (continuation-%type-check cont) nil)
855 ;;; Do a graph walk backward from BLOCK, marking all predecessor
856 ;;; blocks with the DELETE-P flag.
857 (defun mark-for-deletion (block)
858 (declare (type cblock block))
859 (let* ((component (block-component block))
860 (head (component-head component)))
861 (labels ((helper (block)
862 (setf (block-delete-p block) t)
863 (dolist (pred (block-pred block))
864 (unless (or (block-delete-p pred)
867 (unless (block-delete-p block)
869 (setf (component-reanalyze component) t))))
872 ;;; Delete CONT, eliminating both control and value semantics. We set
873 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here
874 ;;; we must get the component from the use block, since the
875 ;;; continuation may be a :DELETED-BLOCK-START.
877 ;;; If CONT has DEST, then it must be the case that the DEST is
878 ;;; unreachable, since we can't compute the value desired. In this
879 ;;; case, we call MARK-FOR-DELETION to cause the DEST block and its
880 ;;; predecessors to tell people to ignore them, and to cause them to
881 ;;; be deleted eventually.
882 (defun delete-continuation (cont)
883 (declare (type continuation cont))
884 (aver (not (eq (continuation-kind cont) :deleted)))
887 (let ((prev (node-prev use)))
888 (unless (eq (continuation-kind prev) :deleted)
889 (let ((block (continuation-block prev)))
890 (setf (block-attributep (block-flags block) flush-p type-asserted) t)
891 (setf (component-reoptimize (block-component block)) t)))))
893 (let ((dest (continuation-dest cont)))
895 (let ((prev (node-prev dest)))
897 (not (eq (continuation-kind prev) :deleted)))
898 (let ((block (continuation-block prev)))
899 (unless (block-delete-p block)
900 (mark-for-deletion block)))))))
902 (setf (continuation-kind cont) :deleted)
903 (setf (continuation-dest cont) nil)
904 (setf (continuation-%externally-checkable-type cont) nil)
905 (setf (continuation-next cont) nil)
906 (setf (continuation-asserted-type cont) *empty-type*)
907 (setf (continuation-%derived-type cont) *empty-type*)
908 (setf (continuation-type-to-check cont) *empty-type*)
909 (setf (continuation-use cont) nil)
910 (setf (continuation-block cont) nil)
911 (setf (continuation-reoptimize cont) nil)
912 (setf (continuation-%type-check cont) nil)
913 (setf (continuation-info cont) nil)
917 ;;; This function does what is necessary to eliminate the code in it
918 ;;; from the IR1 representation. This involves unlinking it from its
919 ;;; predecessors and successors and deleting various node-specific
920 ;;; semantic information.
922 ;;; We mark the START as has having no next and remove the last node
923 ;;; from its CONT's uses. We also flush the DEST for all continuations
924 ;;; whose values are received by nodes in the block.
925 (defun delete-block (block)
926 (declare (type cblock block))
927 (aver (block-component block)) ; else block is already deleted!
928 (note-block-deletion block)
929 (setf (block-delete-p block) t)
931 (let* ((last (block-last block))
932 (cont (node-cont last)))
933 (delete-continuation-use last)
934 (if (eq (continuation-kind cont) :unused)
935 (delete-continuation cont)
936 (reoptimize-continuation cont)))
938 (dolist (b (block-pred block))
939 (unlink-blocks b block)
940 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
941 ;; broken when successors were deleted without setting the
942 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
943 ;; doesn't happen again.
944 (aver (not (and (null (block-succ b))
945 (not (block-delete-p b))
946 (not (eq b (component-head (block-component b))))))))
947 (dolist (b (block-succ block))
948 (unlink-blocks block b))
950 (do-nodes (node cont block)
952 (ref (delete-ref node))
954 (flush-dest (if-test node)))
955 ;; The next two cases serve to maintain the invariant that a LET
956 ;; always has a well-formed COMBINATION, REF and BIND. We delete
957 ;; the lambda whenever we delete any of these, but we must be
958 ;; careful that this LET has not already been partially deleted.
960 (when (and (eq (basic-combination-kind node) :local)
961 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
962 (continuation-use (basic-combination-fun node)))
963 (let ((fun (combination-lambda node)))
964 ;; If our REF was the second-to-last ref, and has been
965 ;; deleted, then FUN may be a LET for some other
967 (when (and (functional-letlike-p fun)
968 (eq (let-combination fun) node))
969 (delete-lambda fun))))
970 (flush-dest (basic-combination-fun node))
971 (dolist (arg (basic-combination-args node))
972 (when arg (flush-dest arg))))
974 (let ((lambda (bind-lambda node)))
975 (unless (eq (functional-kind lambda) :deleted)
976 (delete-lambda lambda))))
978 (let ((value (exit-value node))
979 (entry (exit-entry node)))
983 (setf (entry-exits entry)
984 (delete node (entry-exits entry))))))
986 (flush-dest (return-result node))
987 (delete-return node))
989 (flush-dest (set-value node))
990 (let ((var (set-var node)))
991 (setf (basic-var-sets var)
992 (delete node (basic-var-sets var))))))
994 (delete-continuation (node-prev node)))
996 (remove-from-dfo block)
999 ;;; Do stuff to indicate that the return node Node is being deleted.
1000 ;;; We set the RETURN to NIL.
1001 (defun delete-return (node)
1002 (declare (type creturn node))
1003 (let ((fun (return-lambda node)))
1004 (aver (lambda-return fun))
1005 (setf (lambda-return fun) nil))
1008 ;;; If any of the VARS in FUN was never referenced and was not
1009 ;;; declared IGNORE, then complain.
1010 (defun note-unreferenced-vars (fun)
1011 (declare (type clambda fun))
1012 (dolist (var (lambda-vars fun))
1013 (unless (or (leaf-ever-used var)
1014 (lambda-var-ignorep var))
1015 (let ((*compiler-error-context* (lambda-bind fun)))
1016 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1017 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1018 ;; requires this to be no more than a STYLE-WARNING.
1019 (compiler-style-warn "The variable ~S is defined but never used."
1020 (leaf-debug-name var)))
1021 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1024 (defvar *deletion-ignored-objects* '(t nil))
1026 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1027 ;;; our recursion so that we don't get lost in circular structures. We
1028 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1029 ;;; function referencess with variables), and we also ignore anything
1031 (defun present-in-form (obj form depth)
1032 (declare (type (integer 0 20) depth))
1033 (cond ((= depth 20) nil)
1037 (let ((first (car form))
1039 (if (member first '(quote function))
1041 (or (and (not (symbolp first))
1042 (present-in-form obj first depth))
1043 (do ((l (cdr form) (cdr l))
1045 ((or (atom l) (> n 100))
1047 (declare (fixnum n))
1048 (when (present-in-form obj (car l) depth)
1051 ;;; This function is called on a block immediately before we delete
1052 ;;; it. We check to see whether any of the code about to die appeared
1053 ;;; in the original source, and emit a note if so.
1055 ;;; If the block was in a lambda is now deleted, then we ignore the
1056 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1057 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1058 ;;; reasonable for a function to not return, and there is a different
1059 ;;; note for that case anyway.
1061 ;;; If the actual source is an atom, then we use a bunch of heuristics
1062 ;;; to guess whether this reference really appeared in the original
1064 ;;; -- If a symbol, it must be interned and not a keyword.
1065 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1066 ;;; or a character.)
1067 ;;; -- The atom must be "present" in the original source form, and
1068 ;;; present in all intervening actual source forms.
1069 (defun note-block-deletion (block)
1070 (let ((home (block-home-lambda block)))
1071 (unless (eq (functional-kind home) :deleted)
1072 (do-nodes (node cont block)
1073 (let* ((path (node-source-path node))
1074 (first (first path)))
1075 (when (or (eq first 'original-source-start)
1077 (or (not (symbolp first))
1078 (let ((pkg (symbol-package first)))
1080 (not (eq pkg (symbol-package :end))))))
1081 (not (member first *deletion-ignored-objects*))
1082 (not (typep first '(or fixnum character)))
1084 (present-in-form first x 0))
1085 (source-path-forms path))
1086 (present-in-form first (find-original-source path)
1088 (unless (return-p node)
1089 (let ((*compiler-error-context* node))
1090 (compiler-note "deleting unreachable code")))
1094 ;;; Delete a node from a block, deleting the block if there are no
1095 ;;; nodes left. We remove the node from the uses of its CONT, but we
1096 ;;; don't deal with cleaning up any type-specific semantic
1097 ;;; attachments. If the CONT is :UNUSED after deleting this use, then
1098 ;;; we delete CONT. (Note :UNUSED is not the same as no uses. A
1099 ;;; continuation will only become :UNUSED if it was :INSIDE-BLOCK
1102 ;;; If the node is the last node, there must be exactly one successor.
1103 ;;; We link all of our precedessors to the successor and unlink the
1104 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1105 ;;; left, and the block is a successor of itself, then we replace the
1106 ;;; only node with a degenerate exit node. This provides a way to
1107 ;;; represent the bodyless infinite loop, given the prohibition on
1108 ;;; empty blocks in IR1.
1109 (defun unlink-node (node)
1110 (declare (type node node))
1111 (let* ((cont (node-cont node))
1112 (next (continuation-next cont))
1113 (prev (node-prev node))
1114 (block (continuation-block prev))
1115 (prev-kind (continuation-kind prev))
1116 (last (block-last block)))
1118 (unless (eq (continuation-kind cont) :deleted)
1119 (delete-continuation-use node)
1120 (when (eq (continuation-kind cont) :unused)
1121 (aver (not (continuation-dest cont)))
1122 (delete-continuation cont)))
1124 (setf (block-type-asserted block) t)
1125 (setf (block-test-modified block) t)
1127 (cond ((or (eq prev-kind :inside-block)
1128 (and (eq prev-kind :block-start)
1129 (not (eq node last))))
1130 (cond ((eq node last)
1131 (setf (block-last block) (continuation-use prev))
1132 (setf (continuation-next prev) nil))
1134 (setf (continuation-next prev) next)
1135 (setf (node-prev next) prev)))
1136 (setf (node-prev node) nil)
1139 (aver (eq prev-kind :block-start))
1140 (aver (eq node last))
1141 (let* ((succ (block-succ block))
1142 (next (first succ)))
1143 (aver (and succ (null (cdr succ))))
1145 ((member block succ)
1146 (with-ir1-environment-from-node node
1147 (let ((exit (make-exit))
1148 (dummy (make-continuation)))
1149 (setf (continuation-next prev) nil)
1150 (link-node-to-previous-continuation exit prev)
1151 (add-continuation-use exit dummy)
1152 (setf (block-last block) exit)))
1153 (setf (node-prev node) nil)
1156 (aver (eq (block-start-cleanup block)
1157 (block-end-cleanup block)))
1158 (unlink-blocks block next)
1159 (dolist (pred (block-pred block))
1160 (change-block-successor pred block next))
1161 (remove-from-dfo block)
1162 (cond ((continuation-dest prev)
1163 (setf (continuation-next prev) nil)
1164 (setf (continuation-kind prev) :deleted-block-start))
1166 (delete-continuation prev)))
1167 (setf (node-prev node) nil)
1170 ;;; Return true if NODE has been deleted, false if it is still a valid
1172 (defun node-deleted (node)
1173 (declare (type node node))
1174 (let ((prev (node-prev node)))
1176 (not (eq (continuation-kind prev) :deleted))
1177 (let ((block (continuation-block prev)))
1178 (and (block-component block)
1179 (not (block-delete-p block))))))))
1181 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1182 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1183 ;;; triggered by deletion.
1184 (defun delete-component (component)
1185 (declare (type component component))
1186 (aver (null (component-new-functionals component)))
1187 (setf (component-kind component) :deleted)
1188 (do-blocks (block component)
1189 (setf (block-delete-p block) t))
1190 (dolist (fun (component-lambdas component))
1191 (setf (functional-kind fun) nil)
1192 (setf (functional-entry-fun fun) nil)
1193 (setf (leaf-refs fun) nil)
1194 (delete-functional fun))
1195 (do-blocks (block component)
1196 (delete-block block))
1199 ;;; Convert code of the form
1200 ;;; (FOO ... (FUN ...) ...)
1202 ;;; (FOO ... ... ...).
1203 ;;; In other words, replace the function combination FUN by its
1204 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1205 ;;; to blow out of whatever transform called this. Note, as the number
1206 ;;; of arguments changes, the transform must be prepared to return a
1207 ;;; lambda with a new lambda-list with the correct number of
1209 (defun extract-fun-args (cont fun num-args)
1211 "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1212 to feed directly to the continuation-dest of CONT, which must be
1214 (declare (type continuation cont)
1216 (type index num-args))
1217 (let ((outside (continuation-dest cont))
1218 (inside (continuation-use cont)))
1219 (aver (combination-p outside))
1220 (unless (combination-p inside)
1221 (give-up-ir1-transform))
1222 (let ((inside-fun (combination-fun inside)))
1223 (unless (eq (continuation-fun-name inside-fun) fun)
1224 (give-up-ir1-transform))
1225 (let ((inside-args (combination-args inside)))
1226 (unless (= (length inside-args) num-args)
1227 (give-up-ir1-transform))
1228 (let* ((outside-args (combination-args outside))
1229 (arg-position (position cont outside-args))
1230 (before-args (subseq outside-args 0 arg-position))
1231 (after-args (subseq outside-args (1+ arg-position))))
1232 (dolist (arg inside-args)
1233 (setf (continuation-dest arg) outside)
1234 (setf (continuation-%externally-checkable-type arg) nil))
1235 (setf (combination-args inside) nil)
1236 (setf (combination-args outside)
1237 (append before-args inside-args after-args))
1238 (change-ref-leaf (continuation-use inside-fun)
1239 (find-free-fun 'list "???"))
1240 (setf (combination-kind inside) :full)
1241 (setf (node-derived-type inside) *wild-type*)
1243 (setf (continuation-asserted-type cont) *wild-type*)
1244 (setf (continuation-type-to-check cont) *wild-type*)
1249 ;;; Change the LEAF that a REF refers to.
1250 (defun change-ref-leaf (ref leaf)
1251 (declare (type ref ref) (type leaf leaf))
1252 (unless (eq (ref-leaf ref) leaf)
1253 (push ref (leaf-refs leaf))
1255 (setf (ref-leaf ref) leaf)
1256 (setf (leaf-ever-used leaf) t)
1257 (let ((ltype (leaf-type leaf)))
1258 (if (fun-type-p ltype)
1259 (setf (node-derived-type ref) ltype)
1260 (derive-node-type ref ltype)))
1261 (reoptimize-continuation (node-cont ref)))
1264 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1265 (defun substitute-leaf (new-leaf old-leaf)
1266 (declare (type leaf new-leaf old-leaf))
1267 (dolist (ref (leaf-refs old-leaf))
1268 (change-ref-leaf ref new-leaf))
1271 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1272 ;;; whether to substitute
1273 (defun substitute-leaf-if (test new-leaf old-leaf)
1274 (declare (type leaf new-leaf old-leaf) (type function test))
1275 (dolist (ref (leaf-refs old-leaf))
1276 (when (funcall test ref)
1277 (change-ref-leaf ref new-leaf)))
1280 ;;; Return a LEAF which represents the specified constant object. If
1281 ;;; the object is not in *CONSTANTS*, then we create a new constant
1282 ;;; LEAF and enter it.
1283 (defun find-constant (object)
1285 ;; FIXME: What is the significance of this test? ("things
1286 ;; that are worth uniquifying"?)
1287 '(or symbol number character instance))
1288 (or (gethash object *constants*)
1289 (setf (gethash object *constants*)
1290 (make-constant :value object
1291 :%source-name '.anonymous.
1292 :type (ctype-of object)
1293 :where-from :defined)))
1294 (make-constant :value object
1295 :%source-name '.anonymous.
1296 :type (ctype-of object)
1297 :where-from :defined)))
1299 ;;; If there is a non-local exit noted in ENTRY's environment that
1300 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1301 (defun find-nlx-info (entry cont)
1302 (declare (type entry entry) (type continuation cont))
1303 (let ((entry-cleanup (entry-cleanup entry)))
1304 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1305 (when (and (eq (nlx-info-continuation nlx) cont)
1306 (eq (nlx-info-cleanup nlx) entry-cleanup))
1309 ;;;; functional hackery
1311 (declaim (ftype (function (functional) clambda) main-entry))
1312 (defun main-entry (functional)
1313 (etypecase functional
1314 (clambda functional)
1316 (optional-dispatch-main-entry functional))))
1318 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1319 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1320 ;;; optional with null default and no SUPPLIED-P. There must be a
1321 ;;; &REST arg with no references.
1322 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1323 (defun looks-like-an-mv-bind (functional)
1324 (and (optional-dispatch-p functional)
1325 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1327 (let ((info (lambda-var-arg-info (car arg))))
1328 (unless info (return nil))
1329 (case (arg-info-kind info)
1331 (when (or (arg-info-supplied-p info) (arg-info-default info))
1334 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1338 ;;; Return true if function is an external entry point. This is true
1339 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1340 ;;; (:TOPLEVEL kind.)
1342 (declare (type functional fun))
1343 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1345 ;;; If CONT's only use is a non-notinline global function reference,
1346 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1347 ;;; is true, then we don't care if the leaf is NOTINLINE.
1348 (defun continuation-fun-name (cont &optional notinline-ok)
1349 (declare (type continuation cont))
1350 (let ((use (continuation-use cont)))
1352 (let ((leaf (ref-leaf use)))
1353 (if (and (global-var-p leaf)
1354 (eq (global-var-kind leaf) :global-function)
1355 (or (not (defined-fun-p leaf))
1356 (not (eq (defined-fun-inlinep leaf) :notinline))
1358 (leaf-source-name leaf)
1362 ;;; Return the source name of a combination. (This is an idiom
1363 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1364 (defun combination-fun-source-name (combination)
1365 (let ((ref (continuation-use (combination-fun combination))))
1366 (leaf-source-name (ref-leaf ref))))
1368 ;;; Return the COMBINATION node that is the call to the LET FUN.
1369 (defun let-combination (fun)
1370 (declare (type clambda fun))
1371 (aver (functional-letlike-p fun))
1372 (continuation-dest (node-cont (first (leaf-refs fun)))))
1374 ;;; Return the initial value continuation for a LET variable, or NIL
1375 ;;; if there is none.
1376 (defun let-var-initial-value (var)
1377 (declare (type lambda-var var))
1378 (let ((fun (lambda-var-home var)))
1379 (elt (combination-args (let-combination fun))
1380 (position-or-lose var (lambda-vars fun)))))
1382 ;;; Return the LAMBDA that is called by the local CALL.
1383 (defun combination-lambda (call)
1384 (declare (type basic-combination call))
1385 (aver (eq (basic-combination-kind call) :local))
1386 (ref-leaf (continuation-use (basic-combination-fun call))))
1388 (defvar *inline-expansion-limit* 200
1390 "an upper limit on the number of inline function calls that will be expanded
1391 in any given code object (single function or block compilation)")
1393 ;;; Check whether NODE's component has exceeded its inline expansion
1394 ;;; limit, and warn if so, returning NIL.
1395 (defun inline-expansion-ok (node)
1396 (let ((expanded (incf (component-inline-expansions
1398 (node-block node))))))
1399 (cond ((> expanded *inline-expansion-limit*) nil)
1400 ((= expanded *inline-expansion-limit*)
1401 ;; FIXME: If the objective is to stop the recursive
1402 ;; expansion of inline functions, wouldn't it be more
1403 ;; correct to look back through surrounding expansions
1404 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1405 ;; possibly stored elsewhere too) and suppress expansion
1406 ;; and print this warning when the function being proposed
1407 ;; for inline expansion is found there? (I don't like the
1408 ;; arbitrary numerical limit in principle, and I think
1409 ;; it'll be a nuisance in practice if we ever want the
1410 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1411 ;; arbitrarily huge blocks of code. -- WHN)
1412 (let ((*compiler-error-context* node))
1413 (compiler-note "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1414 probably trying to~% ~
1415 inline a recursive function."
1416 *inline-expansion-limit*))
1422 ;;; Apply a function to some arguments, returning a list of the values
1423 ;;; resulting of the evaluation. If an error is signalled during the
1424 ;;; application, then we produce a warning message using WARN-FUN and
1425 ;;; return NIL as our second value to indicate this. NODE is used as
1426 ;;; the error context for any error message, and CONTEXT is a string
1427 ;;; that is spliced into the warning.
1428 (declaim (ftype (function ((or symbol function) list node function string)
1429 (values list boolean))
1431 (defun careful-call (function args node warn-fun context)
1433 (multiple-value-list
1434 (handler-case (apply function args)
1436 (let ((*compiler-error-context* node))
1437 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1438 (return-from careful-call (values nil nil))))))
1441 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1444 ((deffrob (basic careful compiler transform)
1446 (defun ,careful (specifier)
1447 (handler-case (,basic specifier)
1448 (simple-error (condition)
1449 (values nil (list* (simple-condition-format-control condition)
1450 (simple-condition-format-arguments condition))))))
1451 (defun ,compiler (specifier)
1452 (multiple-value-bind (type error-args) (,careful specifier)
1454 (apply #'compiler-error error-args))))
1455 (defun ,transform (specifier)
1456 (multiple-value-bind (type error-args) (,careful specifier)
1458 (apply #'give-up-ir1-transform
1460 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1461 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1464 ;;;; utilities used at run-time for parsing &KEY args in IR1
1466 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1467 ;;; the continuation for the value of the &KEY argument KEY in the
1468 ;;; list of continuations ARGS. It returns the continuation if the
1469 ;;; keyword is present, or NIL otherwise. The legality and
1470 ;;; constantness of the keywords should already have been checked.
1471 (declaim (ftype (function (list keyword) (or continuation null))
1472 find-keyword-continuation))
1473 (defun find-keyword-continuation (args key)
1474 (do ((arg args (cddr arg)))
1476 (when (eq (continuation-value (first arg)) key)
1477 (return (second arg)))))
1479 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1480 ;;; verify that alternating continuations in ARGS are constant and
1481 ;;; that there is an even number of args.
1482 (declaim (ftype (function (list) boolean) check-key-args-constant))
1483 (defun check-key-args-constant (args)
1484 (do ((arg args (cddr arg)))
1486 (unless (and (rest arg)
1487 (constant-continuation-p (first arg)))
1490 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1491 ;;; verify that the list of continuations ARGS is a well-formed &KEY
1492 ;;; arglist and that only keywords present in the list KEYS are
1494 (declaim (ftype (function (list list) boolean) check-transform-keys))
1495 (defun check-transform-keys (args keys)
1496 (and (check-key-args-constant args)
1497 (do ((arg args (cddr arg)))
1499 (unless (member (continuation-value (first arg)) keys)
1504 ;;; Called by the expansion of the EVENT macro.
1505 (declaim (ftype (function (event-info (or node null)) *) %event))
1506 (defun %event (info node)
1507 (incf (event-info-count info))
1508 (when (and (>= (event-info-level info) *event-note-threshold*)
1509 (policy (or node *lexenv*)
1510 (= inhibit-warnings 0)))
1511 (let ((*compiler-error-context* node))
1512 (compiler-note (event-info-description info))))
1514 (let ((action (event-info-action info)))
1515 (when action (funcall action node))))