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