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)
67 (declare (type lvar lvar))
68 (let ((use (lvar-uses lvar)))
70 (plu (cast-value use))
74 ;;; Update lvar use information so that NODE is no longer a use of its
77 ;;; Note: if you call this function, you may have to do a
78 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
80 (declaim (ftype (sfunction (node) (values))
83 ;;; Just delete NODE from its LVAR uses; LVAR is preserved so it may
84 ;;; be given a new use.
85 (defun %delete-lvar-use (node)
86 (let ((lvar (node-lvar node)))
88 (if (listp (lvar-uses lvar))
89 (let ((new-uses (delq node (lvar-uses lvar))))
90 (setf (lvar-uses lvar)
91 (if (singleton-p new-uses)
94 (setf (lvar-uses lvar) nil))
95 (setf (node-lvar node) nil)))
97 ;;; Delete NODE from its LVAR uses; if LVAR has no other uses, delete
98 ;;; its DEST's block, which must be unreachable.
99 (defun delete-lvar-use (node)
100 (let ((lvar (node-lvar node)))
102 (%delete-lvar-use node)
103 (if (null (lvar-uses lvar))
104 (binding* ((dest (lvar-dest lvar) :exit-if-null)
105 (() (not (node-deleted dest)) :exit-if-null)
106 (block (node-block dest)))
107 (mark-for-deletion block))
108 (reoptimize-lvar lvar))))
111 ;;; Update lvar use information so that NODE uses LVAR.
113 ;;; Note: if you call this function, you may have to do a
114 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
116 (declaim (ftype (sfunction (node (or lvar null)) (values)) add-lvar-use))
117 (defun add-lvar-use (node lvar)
118 (aver (not (node-lvar node)))
120 (let ((uses (lvar-uses lvar)))
121 (setf (lvar-uses lvar)
128 (setf (node-lvar node) lvar)))
132 ;;; Return true if LVAR destination is executed immediately after
133 ;;; NODE. Cleanups are ignored.
134 (defun immediately-used-p (lvar node)
135 (declare (type lvar lvar) (type node node))
136 (aver (eq (node-lvar node) lvar))
137 (let ((dest (lvar-dest lvar)))
138 (acond ((node-next node)
139 (eq (ctran-next it) dest))
140 (t (eq (block-start (first (block-succ (node-block node))))
141 (node-prev dest))))))
143 ;;;; lvar substitution
145 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
146 ;;; NIL. We do not flush OLD's DEST.
147 (defun substitute-lvar (new old)
148 (declare (type lvar old new))
149 (aver (not (lvar-dest new)))
150 (let ((dest (lvar-dest old)))
153 (cif (setf (if-test dest) new))
154 (cset (setf (set-value dest) new))
155 (creturn (setf (return-result dest) new))
156 (exit (setf (exit-value dest) new))
158 (if (eq old (basic-combination-fun dest))
159 (setf (basic-combination-fun dest) new)
160 (setf (basic-combination-args dest)
161 (nsubst new old (basic-combination-args dest)))))
162 (cast (setf (cast-value dest) new)))
164 (setf (lvar-dest old) nil)
165 (setf (lvar-dest new) dest)
166 (flush-lvar-externally-checkable-type new))
169 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
170 ;;; arbitary number of uses. NEW is supposed to be "later" than OLD.
171 (defun substitute-lvar-uses (new old propagate-dx)
172 (declare (type lvar old)
173 (type (or lvar null) new)
174 (type boolean propagate-dx))
178 (%delete-lvar-use node)
179 (add-lvar-use node new))
180 (reoptimize-lvar new)
181 (awhen (and propagate-dx (lvar-dynamic-extent old))
182 (setf (lvar-dynamic-extent old) nil)
183 (unless (lvar-dynamic-extent new)
184 (setf (lvar-dynamic-extent new) it)
185 (setf (cleanup-info it) (substitute new old (cleanup-info it)))))
186 (when (lvar-dynamic-extent new)
188 (node-ends-block node))))
189 (t (flush-dest old)))
193 ;;;; block starting/creation
195 ;;; Return the block that CTRAN is the start of, making a block if
196 ;;; necessary. This function is called by IR1 translators which may
197 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
198 ;;; used more than once must start a block by the time that anyone
199 ;;; does a USE-CTRAN on it.
201 ;;; We also throw the block into the next/prev list for the
202 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
204 (defun ctran-starts-block (ctran)
205 (declare (type ctran ctran))
206 (ecase (ctran-kind ctran)
208 (aver (not (ctran-block ctran)))
209 (let* ((next (component-last-block *current-component*))
210 (prev (block-prev next))
211 (new-block (make-block ctran)))
212 (setf (block-next new-block) next
213 (block-prev new-block) prev
214 (block-prev next) new-block
215 (block-next prev) new-block
216 (ctran-block ctran) new-block
217 (ctran-kind ctran) :block-start)
218 (aver (not (ctran-use ctran)))
221 (ctran-block ctran))))
223 ;;; Ensure that CTRAN is the start of a block so that the use set can
224 ;;; be freely manipulated.
225 (defun ensure-block-start (ctran)
226 (declare (type ctran ctran))
227 (let ((kind (ctran-kind ctran)))
231 (setf (ctran-block ctran)
232 (make-block-key :start ctran))
233 (setf (ctran-kind ctran) :block-start))
235 (node-ends-block (ctran-use ctran)))))
238 ;;; CTRAN must be the last ctran in an incomplete block; finish the
239 ;;; block and start a new one if necessary.
240 (defun start-block (ctran)
241 (declare (type ctran ctran))
242 (aver (not (ctran-next ctran)))
243 (ecase (ctran-kind ctran)
245 (let ((block (ctran-block ctran))
246 (node (ctran-use ctran)))
247 (aver (not (block-last block)))
249 (setf (block-last block) node)
250 (setf (node-next node) nil)
251 (setf (ctran-use ctran) nil)
252 (setf (ctran-kind ctran) :unused)
253 (setf (ctran-block ctran) nil)
254 (link-blocks block (ctran-starts-block ctran))))
259 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
260 ;;; call. First argument must be 'DUMMY, which will be replaced with
261 ;;; LVAR. In case of an ordinary call the function should not have
262 ;;; return type NIL. We create a new "filtered" lvar.
264 ;;; TODO: remove preconditions.
265 (defun filter-lvar (lvar form)
266 (declare (type lvar lvar) (type list form))
267 (let* ((dest (lvar-dest lvar))
268 (ctran (node-prev dest)))
269 (with-ir1-environment-from-node dest
271 (ensure-block-start ctran)
272 (let* ((old-block (ctran-block ctran))
273 (new-start (make-ctran))
274 (filtered-lvar (make-lvar))
275 (new-block (ctran-starts-block new-start)))
277 ;; Splice in the new block before DEST, giving the new block
278 ;; all of DEST's predecessors.
279 (dolist (block (block-pred old-block))
280 (change-block-successor block old-block new-block))
282 (ir1-convert new-start ctran filtered-lvar form)
284 ;; KLUDGE: Comments at the head of this function in CMU CL
285 ;; said that somewhere in here we
286 ;; Set the new block's start and end cleanups to the *start*
287 ;; cleanup of PREV's block. This overrides the incorrect
288 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
289 ;; Unfortunately I can't find any code which corresponds to this.
290 ;; Perhaps it was a stale comment? Or perhaps I just don't
291 ;; understand.. -- WHN 19990521
293 ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
294 ;; no LET conversion has been done yet.) The [mv-]combination
295 ;; code from the call in the form will be the use of the new
296 ;; check lvar. We substitute for the first argument of
298 (let* ((node (lvar-use filtered-lvar))
299 (args (basic-combination-args node))
300 (victim (first args)))
301 (aver (eq (constant-value (ref-leaf (lvar-use victim)))
304 (substitute-lvar filtered-lvar lvar)
305 (substitute-lvar lvar victim)
308 ;; Invoking local call analysis converts this call to a LET.
309 (locall-analyze-component *current-component*))))
312 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
313 (defun delete-filter (node lvar value)
314 (aver (eq (lvar-dest value) node))
315 (aver (eq (node-lvar node) lvar))
316 (cond (lvar (collect ((merges))
317 (when (return-p (lvar-dest lvar))
319 (when (and (basic-combination-p use)
320 (eq (basic-combination-kind use) :local))
322 (substitute-lvar-uses lvar value
323 (and lvar (eq (lvar-uses lvar) node)))
324 (%delete-lvar-use node)
327 (dolist (merge (merges))
328 (merge-tail-sets merge)))))
329 (t (flush-dest value)
330 (unlink-node node))))
332 ;;; Make a CAST and insert it into IR1 before node NEXT.
333 (defun insert-cast-before (next lvar type policy)
334 (declare (type node next) (type lvar lvar) (type ctype type))
335 (with-ir1-environment-from-node next
336 (let* ((ctran (node-prev next))
337 (cast (make-cast lvar type policy))
338 (internal-ctran (make-ctran)))
339 (setf (ctran-next ctran) cast
340 (node-prev cast) ctran)
341 (use-ctran cast internal-ctran)
342 (link-node-to-previous-ctran next internal-ctran)
343 (setf (lvar-dest lvar) cast)
344 (reoptimize-lvar lvar)
345 (when (return-p next)
346 (node-ends-block cast))
347 (setf (block-attributep (block-flags (node-block cast))
348 type-check type-asserted)
352 ;;;; miscellaneous shorthand functions
354 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
355 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
356 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
357 ;;; deleted, and then return its home.
358 (defun node-home-lambda (node)
359 (declare (type node node))
360 (do ((fun (lexenv-lambda (node-lexenv node))
361 (lexenv-lambda (lambda-call-lexenv fun))))
362 ((not (memq (functional-kind fun) '(:deleted :zombie)))
364 (when (eq (lambda-home fun) fun)
367 #!-sb-fluid (declaim (inline node-block))
368 (defun node-block (node)
369 (ctran-block (node-prev node)))
370 (declaim (ftype (sfunction (node) component) node-component))
371 (defun node-component (node)
372 (block-component (node-block node)))
373 (declaim (ftype (sfunction (node) physenv) node-physenv))
374 (defun node-physenv (node)
375 (lambda-physenv (node-home-lambda node)))
376 #!-sb-fluid (declaim (inline node-dest))
377 (defun node-dest (node)
378 (awhen (node-lvar node) (lvar-dest it)))
380 #!-sb-fluid (declaim (inline node-stack-allocate-p))
381 (defun node-stack-allocate-p (node)
382 (awhen (node-lvar node)
383 (lvar-dynamic-extent it)))
385 (defun use-good-for-dx-p (use)
386 (and (combination-p use)
387 (eq (combination-kind use) :known)
388 (awhen (fun-info-stack-allocate-result
389 (combination-fun-info use))
392 (defun lvar-good-for-dx-p (lvar)
393 (let ((uses (lvar-uses lvar)))
395 (every #'use-good-for-dx-p uses)
396 (use-good-for-dx-p uses))))
398 (declaim (inline block-to-be-deleted-p))
399 (defun block-to-be-deleted-p (block)
400 (or (block-delete-p block)
401 (eq (functional-kind (block-home-lambda block)) :deleted)))
403 ;;; Checks whether NODE is in a block to be deleted
404 (declaim (inline node-to-be-deleted-p))
405 (defun node-to-be-deleted-p (node)
406 (block-to-be-deleted-p (node-block node)))
408 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
409 (defun lambda-block (clambda)
410 (node-block (lambda-bind clambda)))
411 (declaim (ftype (sfunction (clambda) component) lambda-component))
412 (defun lambda-component (clambda)
413 (block-component (lambda-block clambda)))
415 (declaim (ftype (sfunction (cblock) node) block-start-node))
416 (defun block-start-node (block)
417 (ctran-next (block-start block)))
419 ;;; Return the enclosing cleanup for environment of the first or last
421 (defun block-start-cleanup (block)
422 (node-enclosing-cleanup (block-start-node block)))
423 (defun block-end-cleanup (block)
424 (node-enclosing-cleanup (block-last block)))
426 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
427 ;;; if there is none.
429 ;;; There can legitimately be no home lambda in dead code early in the
430 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
431 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
432 ;;; where the block is just a placeholder during parsing and doesn't
433 ;;; actually correspond to code which will be written anywhere.
434 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
435 (defun block-home-lambda-or-null (block)
436 (if (node-p (block-last block))
437 ;; This is the old CMU CL way of doing it.
438 (node-home-lambda (block-last block))
439 ;; Now that SBCL uses this operation more aggressively than CMU
440 ;; CL did, the old CMU CL way of doing it can fail in two ways.
441 ;; 1. It can fail in a few cases even when a meaningful home
442 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
444 ;; 2. It can fail when converting a form which is born orphaned
445 ;; so that it never had a meaningful home lambda, e.g. a form
446 ;; which follows a RETURN-FROM or GO form.
447 (let ((pred-list (block-pred block)))
448 ;; To deal with case 1, we reason that
449 ;; previous-in-target-execution-order blocks should be in the
450 ;; same lambda, and that they seem in practice to be
451 ;; previous-in-compilation-order blocks too, so we look back
452 ;; to find one which is sufficiently initialized to tell us
453 ;; what the home lambda is.
455 ;; We could get fancy about this, flooding through the
456 ;; graph of all the previous blocks, but in practice it
457 ;; seems to work just to grab the first previous block and
459 (node-home-lambda (block-last (first pred-list)))
460 ;; In case 2, we end up with an empty PRED-LIST and
461 ;; have to punt: There's no home lambda.
464 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
465 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
466 (defun block-home-lambda (block)
467 (block-home-lambda-or-null block))
469 ;;; Return the IR1 physical environment for BLOCK.
470 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
471 (defun block-physenv (block)
472 (lambda-physenv (block-home-lambda block)))
474 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
475 ;;; of its original source's top level form in its compilation unit.
476 (defun source-path-tlf-number (path)
477 (declare (list path))
480 ;;; Return the (reversed) list for the PATH in the original source
481 ;;; (with the Top Level Form number last).
482 (defun source-path-original-source (path)
483 (declare (list path) (inline member))
484 (cddr (member 'original-source-start path :test #'eq)))
486 ;;; Return the Form Number of PATH's original source inside the Top
487 ;;; Level Form that contains it. This is determined by the order that
488 ;;; we walk the subforms of the top level source form.
489 (defun source-path-form-number (path)
490 (declare (list path) (inline member))
491 (cadr (member 'original-source-start path :test #'eq)))
493 ;;; Return a list of all the enclosing forms not in the original
494 ;;; source that converted to get to this form, with the immediate
495 ;;; source for node at the start of the list.
496 (defun source-path-forms (path)
497 (subseq path 0 (position 'original-source-start path)))
499 ;;; Return the innermost source form for NODE.
500 (defun node-source-form (node)
501 (declare (type node node))
502 (let* ((path (node-source-path node))
503 (forms (source-path-forms path)))
506 (values (find-original-source path)))))
508 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
510 (defun lvar-source (lvar)
511 (let ((use (lvar-uses lvar)))
514 (values (node-source-form use) t))))
516 ;;; Return the unique node, delivering a value to LVAR.
517 #!-sb-fluid (declaim (inline lvar-use))
518 (defun lvar-use (lvar)
519 (the (not list) (lvar-uses lvar)))
521 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
522 (defun lvar-has-single-use-p (lvar)
523 (typep (lvar-uses lvar) '(not list)))
525 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
526 (declaim (ftype (sfunction (ctran) (or clambda null))
527 ctran-home-lambda-or-null))
528 (defun ctran-home-lambda-or-null (ctran)
529 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
530 ;; implementation might not be quite right, or might be uglier than
531 ;; necessary. It appears that the original Python never found a need
532 ;; to do this operation. The obvious things based on
533 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
534 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
535 ;; generalize it enough to grovel harder when the simple CMU CL
536 ;; approach fails, and furthermore realize that in some exceptional
537 ;; cases it might return NIL. -- WHN 2001-12-04
538 (cond ((ctran-use ctran)
539 (node-home-lambda (ctran-use ctran)))
541 (block-home-lambda-or-null (ctran-block ctran)))
543 (bug "confused about home lambda for ~S" ctran))))
545 ;;; Return the LAMBDA that is CTRAN's home.
546 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
547 (defun ctran-home-lambda (ctran)
548 (ctran-home-lambda-or-null ctran))
550 (declaim (inline cast-single-value-p))
551 (defun cast-single-value-p (cast)
552 (not (values-type-p (cast-asserted-type cast))))
554 #!-sb-fluid (declaim (inline lvar-single-value-p))
555 (defun lvar-single-value-p (lvar)
557 (let ((dest (lvar-dest lvar)))
562 (eq (basic-combination-fun dest) lvar))
565 (declare (notinline lvar-single-value-p))
566 (and (cast-single-value-p dest)
567 (lvar-single-value-p (node-lvar dest)))))
571 (defun principal-lvar-end (lvar)
572 (loop for prev = lvar then (node-lvar dest)
573 for dest = (and prev (lvar-dest prev))
575 finally (return (values dest prev))))
577 (defun principal-lvar-single-valuify (lvar)
578 (loop for prev = lvar then (node-lvar dest)
579 for dest = (and prev (lvar-dest prev))
581 do (setf (node-derived-type dest)
582 (make-short-values-type (list (single-value-type
583 (node-derived-type dest)))))
584 (reoptimize-lvar prev)))
586 ;;; Return a new LEXENV just like DEFAULT except for the specified
587 ;;; slot values. Values for the alist slots are NCONCed to the
588 ;;; beginning of the current value, rather than replacing it entirely.
589 (defun make-lexenv (&key (default *lexenv*)
590 funs vars blocks tags
592 (lambda (lexenv-lambda default))
593 (cleanup (lexenv-cleanup default))
594 (handled-conditions (lexenv-handled-conditions default))
595 (disabled-package-locks
596 (lexenv-disabled-package-locks default))
597 (policy (lexenv-policy default)))
598 (macrolet ((frob (var slot)
599 `(let ((old (,slot default)))
603 (internal-make-lexenv
604 (frob funs lexenv-funs)
605 (frob vars lexenv-vars)
606 (frob blocks lexenv-blocks)
607 (frob tags lexenv-tags)
608 (frob type-restrictions lexenv-type-restrictions)
609 lambda cleanup handled-conditions
610 disabled-package-locks policy)))
612 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
614 (defun make-restricted-lexenv (lexenv)
615 (flet ((fun-good-p (fun)
616 (destructuring-bind (name . thing) fun
617 (declare (ignore name))
621 (cons (aver (eq (car thing) 'macro))
624 (destructuring-bind (name . thing) var
625 (declare (ignore name))
627 ;; The evaluator will mark lexicals with :BOGUS when it
628 ;; translates an interpreter lexenv to a compiler
630 ((or leaf #!+sb-eval (member :bogus)) nil)
631 (cons (aver (eq (car thing) 'macro))
633 (heap-alien-info nil)))))
634 (internal-make-lexenv
635 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
636 (remove-if-not #'var-good-p (lexenv-vars lexenv))
639 (lexenv-type-restrictions lexenv) ; XXX
642 (lexenv-handled-conditions lexenv)
643 (lexenv-disabled-package-locks lexenv)
644 (lexenv-policy lexenv))))
646 ;;;; flow/DFO/component hackery
648 ;;; Join BLOCK1 and BLOCK2.
649 (defun link-blocks (block1 block2)
650 (declare (type cblock block1 block2))
651 (setf (block-succ block1)
652 (if (block-succ block1)
653 (%link-blocks block1 block2)
655 (push block1 (block-pred block2))
657 (defun %link-blocks (block1 block2)
658 (declare (type cblock block1 block2))
659 (let ((succ1 (block-succ block1)))
660 (aver (not (memq block2 succ1)))
661 (cons block2 succ1)))
663 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
664 ;;; this leaves a successor with a single predecessor that ends in an
665 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
666 ;;; now be able to be propagated to the successor.
667 (defun unlink-blocks (block1 block2)
668 (declare (type cblock block1 block2))
669 (let ((succ1 (block-succ block1)))
670 (if (eq block2 (car succ1))
671 (setf (block-succ block1) (cdr succ1))
672 (do ((succ (cdr succ1) (cdr succ))
674 ((eq (car succ) block2)
675 (setf (cdr prev) (cdr succ)))
678 (let ((new-pred (delq block1 (block-pred block2))))
679 (setf (block-pred block2) new-pred)
680 (when (singleton-p new-pred)
681 (let ((pred-block (first new-pred)))
682 (when (if-p (block-last pred-block))
683 (setf (block-test-modified pred-block) t)))))
686 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
687 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
688 ;;; consequent/alternative blocks to point to NEW. We also set
689 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
690 ;;; the new successor.
691 (defun change-block-successor (block old new)
692 (declare (type cblock new old block))
693 (unlink-blocks block old)
694 (let ((last (block-last block))
695 (comp (block-component block)))
696 (setf (component-reanalyze comp) t)
699 (setf (block-test-modified block) t)
700 (let* ((succ-left (block-succ block))
701 (new (if (and (eq new (component-tail comp))
705 (unless (memq new succ-left)
706 (link-blocks block new))
707 (macrolet ((frob (slot)
708 `(when (eq (,slot last) old)
709 (setf (,slot last) new))))
711 (frob if-alternative)
712 (when (eq (if-consequent last)
713 (if-alternative last))
714 (reoptimize-component (block-component block) :maybe)))))
716 (unless (memq new (block-succ block))
717 (link-blocks block new)))))
721 ;;; Unlink a block from the next/prev chain. We also null out the
723 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
724 (defun remove-from-dfo (block)
725 (let ((next (block-next block))
726 (prev (block-prev block)))
727 (setf (block-component block) nil)
728 (setf (block-next prev) next)
729 (setf (block-prev next) prev))
732 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
733 ;;; COMPONENT to be the same as for AFTER.
734 (defun add-to-dfo (block after)
735 (declare (type cblock block after))
736 (let ((next (block-next after))
737 (comp (block-component after)))
738 (aver (not (eq (component-kind comp) :deleted)))
739 (setf (block-component block) comp)
740 (setf (block-next after) block)
741 (setf (block-prev block) after)
742 (setf (block-next block) next)
743 (setf (block-prev next) block))
746 ;;; List all NLX-INFOs which BLOCK can exit to.
748 ;;; We hope that no cleanup actions are performed in the middle of
749 ;;; BLOCK, so it is enough to look only at cleanups in the block
750 ;;; end. The tricky thing is a special cleanup block; all its nodes
751 ;;; have the same cleanup info, corresponding to the start, so the
752 ;;; same approach returns safe result.
753 (defun map-block-nlxes (fun block &optional dx-cleanup-fun)
754 (loop for cleanup = (block-end-cleanup block)
755 then (node-enclosing-cleanup (cleanup-mess-up cleanup))
757 do (let ((mess-up (cleanup-mess-up cleanup)))
758 (case (cleanup-kind cleanup)
760 (aver (entry-p mess-up))
761 (loop for exit in (entry-exits mess-up)
762 for nlx-info = (exit-nlx-info exit)
763 do (funcall fun nlx-info)))
764 ((:catch :unwind-protect)
765 (aver (combination-p mess-up))
766 (let* ((arg-lvar (first (basic-combination-args mess-up)))
767 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
768 (funcall fun nlx-info)))
771 (funcall dx-cleanup-fun cleanup)))))))
773 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
774 ;;; the head and tail which are set to T.
775 (declaim (ftype (sfunction (component) (values)) clear-flags))
776 (defun clear-flags (component)
777 (let ((head (component-head component))
778 (tail (component-tail component)))
779 (setf (block-flag head) t)
780 (setf (block-flag tail) t)
781 (do-blocks (block component)
782 (setf (block-flag block) nil)))
785 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
786 ;;; true in the head and tail blocks.
787 (declaim (ftype (sfunction () component) make-empty-component))
788 (defun make-empty-component ()
789 (let* ((head (make-block-key :start nil :component nil))
790 (tail (make-block-key :start nil :component nil))
791 (res (make-component head tail)))
792 (setf (block-flag head) t)
793 (setf (block-flag tail) t)
794 (setf (block-component head) res)
795 (setf (block-component tail) res)
796 (setf (block-next head) tail)
797 (setf (block-prev tail) head)
800 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
801 ;;; The new block is added to the DFO immediately following NODE's block.
802 (defun node-ends-block (node)
803 (declare (type node node))
804 (let* ((block (node-block node))
805 (start (node-next node))
806 (last (block-last block)))
807 (unless (eq last node)
808 (aver (and (eq (ctran-kind start) :inside-block)
809 (not (block-delete-p block))))
810 (let* ((succ (block-succ block))
812 (make-block-key :start start
813 :component (block-component block)
814 :succ succ :last last)))
815 (setf (ctran-kind start) :block-start)
816 (setf (ctran-use start) nil)
817 (setf (block-last block) node)
818 (setf (node-next node) nil)
821 (cons new-block (remove block (block-pred b)))))
822 (setf (block-succ block) ())
823 (link-blocks block new-block)
824 (add-to-dfo new-block block)
825 (setf (component-reanalyze (block-component block)) t)
827 (do ((ctran start (node-next (ctran-next ctran))))
829 (setf (ctran-block ctran) new-block))
831 (setf (block-type-asserted block) t)
832 (setf (block-test-modified block) t))))
837 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
838 (defun delete-lambda-var (leaf)
839 (declare (type lambda-var leaf))
841 ;; Iterate over all local calls flushing the corresponding argument,
842 ;; allowing the computation of the argument to be deleted. We also
843 ;; mark the LET for reoptimization, since it may be that we have
844 ;; deleted its last variable.
845 (let* ((fun (lambda-var-home leaf))
846 (n (position leaf (lambda-vars fun))))
847 (dolist (ref (leaf-refs fun))
848 (let* ((lvar (node-lvar ref))
849 (dest (and lvar (lvar-dest lvar))))
850 (when (and (combination-p dest)
851 (eq (basic-combination-fun dest) lvar)
852 (eq (basic-combination-kind dest) :local))
853 (let* ((args (basic-combination-args dest))
855 (reoptimize-lvar arg)
857 (setf (elt args n) nil))))))
859 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
860 ;; too much difficulty, since we can efficiently implement
861 ;; write-only variables. We iterate over the SETs, marking their
862 ;; blocks for dead code flushing, since we can delete SETs whose
864 (dolist (set (lambda-var-sets leaf))
865 (setf (block-flush-p (node-block set)) t))
869 ;;; Note that something interesting has happened to VAR.
870 (defun reoptimize-lambda-var (var)
871 (declare (type lambda-var var))
872 (let ((fun (lambda-var-home var)))
873 ;; We only deal with LET variables, marking the corresponding
874 ;; initial value arg as needing to be reoptimized.
875 (when (and (eq (functional-kind fun) :let)
877 (do ((args (basic-combination-args
878 (lvar-dest (node-lvar (first (leaf-refs fun)))))
880 (vars (lambda-vars fun) (cdr vars)))
882 (reoptimize-lvar (car args))))))
885 ;;; Delete a function that has no references. This need only be called
886 ;;; on functions that never had any references, since otherwise
887 ;;; DELETE-REF will handle the deletion.
888 (defun delete-functional (fun)
889 (aver (and (null (leaf-refs fun))
890 (not (functional-entry-fun fun))))
892 (optional-dispatch (delete-optional-dispatch fun))
893 (clambda (delete-lambda fun)))
896 ;;; Deal with deleting the last reference to a CLAMBDA, which means
897 ;;; that the lambda is unreachable, so that its body may be
898 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
899 ;;; IR1-OPTIMIZE to delete its blocks.
900 (defun delete-lambda (clambda)
901 (declare (type clambda clambda))
902 (let ((original-kind (functional-kind clambda))
903 (bind (lambda-bind clambda)))
904 (aver (not (member original-kind '(:deleted :toplevel))))
905 (aver (not (functional-has-external-references-p clambda)))
906 (aver (or (eq original-kind :zombie) bind))
907 (setf (functional-kind clambda) :deleted)
908 (setf (lambda-bind clambda) nil)
910 (labels ((delete-children (lambda)
911 (dolist (child (lambda-children lambda))
912 (cond ((eq (functional-kind child) :deleted)
913 (delete-children child))
915 (delete-lambda child))))
916 (setf (lambda-children lambda) nil)
917 (setf (lambda-parent lambda) nil)))
918 (delete-children clambda))
920 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
921 ;; that we're using the old value of the KIND slot, not the
922 ;; current slot value, which has now been set to :DELETED.)
925 ((:let :mv-let :assignment)
926 (let ((bind-block (node-block bind)))
927 (mark-for-deletion bind-block))
928 (let ((home (lambda-home clambda)))
929 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
930 ;; KLUDGE: In presence of NLEs we cannot always understand that
931 ;; LET's BIND dominates its body [for a LET "its" body is not
932 ;; quite its]; let's delete too dangerous for IR2 stuff. --
934 (dolist (var (lambda-vars clambda))
935 (flet ((delete-node (node)
936 (mark-for-deletion (node-block node))))
937 (mapc #'delete-node (leaf-refs var))
938 (mapc #'delete-node (lambda-var-sets var)))))
940 ;; Function has no reachable references.
941 (dolist (ref (lambda-refs clambda))
942 (mark-for-deletion (node-block ref)))
943 ;; If the function isn't a LET, we unlink the function head
944 ;; and tail from the component head and tail to indicate that
945 ;; the code is unreachable. We also delete the function from
946 ;; COMPONENT-LAMBDAS (it won't be there before local call
947 ;; analysis, but no matter.) If the lambda was never
948 ;; referenced, we give a note.
949 (let* ((bind-block (node-block bind))
950 (component (block-component bind-block))
951 (return (lambda-return clambda))
952 (return-block (and return (node-block return))))
953 (unless (leaf-ever-used clambda)
954 (let ((*compiler-error-context* bind))
955 (compiler-notify 'code-deletion-note
956 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
957 :format-arguments (list (leaf-debug-name clambda)))))
958 (unless (block-delete-p bind-block)
959 (unlink-blocks (component-head component) bind-block))
960 (when (and return-block (not (block-delete-p return-block)))
961 (mark-for-deletion return-block)
962 (unlink-blocks return-block (component-tail component)))
963 (setf (component-reanalyze component) t)
964 (let ((tails (lambda-tail-set clambda)))
965 (setf (tail-set-funs tails)
966 (delete clambda (tail-set-funs tails)))
967 (setf (lambda-tail-set clambda) nil))
968 (setf (component-lambdas component)
969 (delq clambda (component-lambdas component))))))
971 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
972 ;; ENTRY-FUN so that people will know that it is not an entry
974 (when (eq original-kind :external)
975 (let ((fun (functional-entry-fun clambda)))
976 (setf (functional-entry-fun fun) nil)
977 (when (optional-dispatch-p fun)
978 (delete-optional-dispatch fun)))))
982 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
983 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
984 ;;; is used both before and after local call analysis. Afterward, all
985 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
986 ;;; to the XEP, leaving it with no references at all. So we look at
987 ;;; the XEP to see whether an optional-dispatch is still really being
988 ;;; used. But before local call analysis, there are no XEPs, and all
989 ;;; references are direct.
991 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
992 ;;; entry-points, making them be normal lambdas, and then deleting the
993 ;;; ones with no references. This deletes any e-p lambdas that were
994 ;;; either never referenced, or couldn't be deleted when the last
995 ;;; reference was deleted (due to their :OPTIONAL kind.)
997 ;;; Note that the last optional entry point may alias the main entry,
998 ;;; so when we process the main entry, its KIND may have been changed
999 ;;; to NIL or even converted to a LETlike value.
1000 (defun delete-optional-dispatch (leaf)
1001 (declare (type optional-dispatch leaf))
1002 (let ((entry (functional-entry-fun leaf)))
1003 (unless (and entry (leaf-refs entry))
1004 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
1005 (setf (functional-kind leaf) :deleted)
1008 (unless (eq (functional-kind fun) :deleted)
1009 (aver (eq (functional-kind fun) :optional))
1010 (setf (functional-kind fun) nil)
1011 (let ((refs (leaf-refs fun)))
1013 (delete-lambda fun))
1015 (or (maybe-let-convert fun)
1016 (maybe-convert-to-assignment fun)))
1018 (maybe-convert-to-assignment fun)))))))
1020 (dolist (ep (optional-dispatch-entry-points leaf))
1021 (when (promise-ready-p ep)
1023 (when (optional-dispatch-more-entry leaf)
1024 (frob (optional-dispatch-more-entry leaf)))
1025 (let ((main (optional-dispatch-main-entry leaf)))
1027 (setf (functional-entry-fun entry) main)
1028 (setf (functional-entry-fun main) entry))
1029 (when (eq (functional-kind main) :optional)
1034 (defun note-local-functional (fun)
1035 (declare (type functional fun))
1036 (when (and (leaf-has-source-name-p fun)
1037 (eq (leaf-source-name fun) (functional-debug-name fun)))
1038 (let ((name (leaf-source-name fun)))
1039 (let ((defined-fun (gethash name *free-funs*)))
1040 (when (and defined-fun
1041 (defined-fun-p defined-fun)
1042 (eq (defined-fun-functional defined-fun) fun))
1043 (remhash name *free-funs*))))))
1045 ;;; Do stuff to delete the semantic attachments of a REF node. When
1046 ;;; this leaves zero or one reference, we do a type dispatch off of
1047 ;;; the leaf to determine if a special action is appropriate.
1048 (defun delete-ref (ref)
1049 (declare (type ref ref))
1050 (let* ((leaf (ref-leaf ref))
1051 (refs (delq ref (leaf-refs leaf))))
1052 (setf (leaf-refs leaf) refs)
1057 (delete-lambda-var leaf))
1059 (ecase (functional-kind leaf)
1060 ((nil :let :mv-let :assignment :escape :cleanup)
1061 (aver (null (functional-entry-fun leaf)))
1062 (delete-lambda leaf))
1064 (delete-lambda leaf))
1065 ((:deleted :zombie :optional))))
1067 (unless (eq (functional-kind leaf) :deleted)
1068 (delete-optional-dispatch leaf)))))
1071 (clambda (or (maybe-let-convert leaf)
1072 (maybe-convert-to-assignment leaf)))
1073 (lambda-var (reoptimize-lambda-var leaf))))
1076 (clambda (maybe-convert-to-assignment leaf))))))
1080 ;;; This function is called by people who delete nodes; it provides a
1081 ;;; way to indicate that the value of a lvar is no longer used. We
1082 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1083 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1084 (defun flush-dest (lvar)
1085 (declare (type (or lvar null) lvar))
1087 (setf (lvar-dest lvar) nil)
1088 (flush-lvar-externally-checkable-type lvar)
1090 (let ((prev (node-prev use)))
1091 (let ((block (ctran-block prev)))
1092 (reoptimize-component (block-component block) t)
1093 (setf (block-attributep (block-flags block)
1094 flush-p type-asserted type-check)
1096 (setf (node-lvar use) nil))
1097 (setf (lvar-uses lvar) nil))
1100 (defun delete-dest (lvar)
1102 (let* ((dest (lvar-dest lvar))
1103 (prev (node-prev dest)))
1104 (let ((block (ctran-block prev)))
1105 (unless (block-delete-p block)
1106 (mark-for-deletion block))))))
1108 ;;; Queue the block for deletion
1109 (defun delete-block-lazily (block)
1110 (declare (type cblock block))
1111 (unless (block-delete-p block)
1112 (setf (block-delete-p block) t)
1113 (push block (component-delete-blocks (block-component block)))))
1115 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1116 ;;; blocks with the DELETE-P flag.
1117 (defun mark-for-deletion (block)
1118 (declare (type cblock block))
1119 (let* ((component (block-component block))
1120 (head (component-head component)))
1121 (labels ((helper (block)
1122 (delete-block-lazily block)
1123 (dolist (pred (block-pred block))
1124 (unless (or (block-delete-p pred)
1127 (unless (block-delete-p block)
1129 (setf (component-reanalyze component) t))))
1132 ;;; This function does what is necessary to eliminate the code in it
1133 ;;; from the IR1 representation. This involves unlinking it from its
1134 ;;; predecessors and successors and deleting various node-specific
1135 ;;; semantic information. BLOCK must be already removed from
1136 ;;; COMPONENT-DELETE-BLOCKS.
1137 (defun delete-block (block &optional silent)
1138 (declare (type cblock block))
1139 (aver (block-component block)) ; else block is already deleted!
1140 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1142 (note-block-deletion block))
1143 (setf (block-delete-p block) t)
1145 (dolist (b (block-pred block))
1146 (unlink-blocks b block)
1147 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1148 ;; broken when successors were deleted without setting the
1149 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1150 ;; doesn't happen again.
1151 (aver (not (and (null (block-succ b))
1152 (not (block-delete-p b))
1153 (not (eq b (component-head (block-component b))))))))
1154 (dolist (b (block-succ block))
1155 (unlink-blocks block b))
1157 (do-nodes-carefully (node block)
1158 (when (valued-node-p node)
1159 (delete-lvar-use node))
1161 (ref (delete-ref node))
1162 (cif (flush-dest (if-test node)))
1163 ;; The next two cases serve to maintain the invariant that a LET
1164 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1165 ;; the lambda whenever we delete any of these, but we must be
1166 ;; careful that this LET has not already been partially deleted.
1168 (when (and (eq (basic-combination-kind node) :local)
1169 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1170 (lvar-uses (basic-combination-fun node)))
1171 (let ((fun (combination-lambda node)))
1172 ;; If our REF was the second-to-last ref, and has been
1173 ;; deleted, then FUN may be a LET for some other
1175 (when (and (functional-letlike-p fun)
1176 (eq (let-combination fun) node))
1177 (delete-lambda fun))))
1178 (flush-dest (basic-combination-fun node))
1179 (dolist (arg (basic-combination-args node))
1180 (when arg (flush-dest arg))))
1182 (let ((lambda (bind-lambda node)))
1183 (unless (eq (functional-kind lambda) :deleted)
1184 (delete-lambda lambda))))
1186 (let ((value (exit-value node))
1187 (entry (exit-entry node)))
1191 (setf (entry-exits entry)
1192 (delq node (entry-exits entry))))))
1194 (dolist (exit (entry-exits node))
1195 (mark-for-deletion (node-block exit)))
1196 (let ((home (node-home-lambda node)))
1197 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1199 (flush-dest (return-result node))
1200 (delete-return node))
1202 (flush-dest (set-value node))
1203 (let ((var (set-var node)))
1204 (setf (basic-var-sets var)
1205 (delete node (basic-var-sets var)))))
1207 (flush-dest (cast-value node)))))
1209 (remove-from-dfo block)
1212 ;;; Do stuff to indicate that the return node NODE is being deleted.
1213 (defun delete-return (node)
1214 (declare (type creturn node))
1215 (let* ((fun (return-lambda node))
1216 (tail-set (lambda-tail-set fun)))
1217 (aver (lambda-return fun))
1218 (setf (lambda-return fun) nil)
1219 (when (and tail-set (not (find-if #'lambda-return
1220 (tail-set-funs tail-set))))
1221 (setf (tail-set-type tail-set) *empty-type*)))
1224 ;;; If any of the VARS in FUN was never referenced and was not
1225 ;;; declared IGNORE, then complain.
1226 (defun note-unreferenced-vars (fun)
1227 (declare (type clambda fun))
1228 (dolist (var (lambda-vars fun))
1229 (unless (or (leaf-ever-used var)
1230 (lambda-var-ignorep var))
1231 (let ((*compiler-error-context* (lambda-bind fun)))
1232 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1233 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1234 ;; requires this to be no more than a STYLE-WARNING.
1236 (compiler-style-warn "The variable ~S is defined but never used."
1237 (leaf-debug-name var))
1238 ;; There's no reason to accept this kind of equivocation
1239 ;; when compiling our own code, though.
1241 (warn "The variable ~S is defined but never used."
1242 (leaf-debug-name var)))
1243 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1246 (defvar *deletion-ignored-objects* '(t nil))
1248 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1249 ;;; our recursion so that we don't get lost in circular structures. We
1250 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1251 ;;; function referencess with variables), and we also ignore anything
1253 (defun present-in-form (obj form depth)
1254 (declare (type (integer 0 20) depth))
1255 (cond ((= depth 20) nil)
1259 (let ((first (car form))
1261 (if (member first '(quote function))
1263 (or (and (not (symbolp first))
1264 (present-in-form obj first depth))
1265 (do ((l (cdr form) (cdr l))
1267 ((or (atom l) (> n 100))
1269 (declare (fixnum n))
1270 (when (present-in-form obj (car l) depth)
1273 ;;; This function is called on a block immediately before we delete
1274 ;;; it. We check to see whether any of the code about to die appeared
1275 ;;; in the original source, and emit a note if so.
1277 ;;; If the block was in a lambda is now deleted, then we ignore the
1278 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1279 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1280 ;;; reasonable for a function to not return, and there is a different
1281 ;;; note for that case anyway.
1283 ;;; If the actual source is an atom, then we use a bunch of heuristics
1284 ;;; to guess whether this reference really appeared in the original
1286 ;;; -- If a symbol, it must be interned and not a keyword.
1287 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1288 ;;; or a character.)
1289 ;;; -- The atom must be "present" in the original source form, and
1290 ;;; present in all intervening actual source forms.
1291 (defun note-block-deletion (block)
1292 (let ((home (block-home-lambda block)))
1293 (unless (eq (functional-kind home) :deleted)
1294 (do-nodes (node nil block)
1295 (let* ((path (node-source-path node))
1296 (first (first path)))
1297 (when (or (eq first 'original-source-start)
1299 (or (not (symbolp first))
1300 (let ((pkg (symbol-package first)))
1302 (not (eq pkg (symbol-package :end))))))
1303 (not (member first *deletion-ignored-objects*))
1304 (not (typep first '(or fixnum character)))
1306 (present-in-form first x 0))
1307 (source-path-forms path))
1308 (present-in-form first (find-original-source path)
1310 (unless (return-p node)
1311 (let ((*compiler-error-context* node))
1312 (compiler-notify 'code-deletion-note
1313 :format-control "deleting unreachable code"
1314 :format-arguments nil)))
1318 ;;; Delete a node from a block, deleting the block if there are no
1319 ;;; nodes left. We remove the node from the uses of its LVAR.
1321 ;;; If the node is the last node, there must be exactly one successor.
1322 ;;; We link all of our precedessors to the successor and unlink the
1323 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1324 ;;; left, and the block is a successor of itself, then we replace the
1325 ;;; only node with a degenerate exit node. This provides a way to
1326 ;;; represent the bodyless infinite loop, given the prohibition on
1327 ;;; empty blocks in IR1.
1328 (defun unlink-node (node)
1329 (declare (type node node))
1330 (when (valued-node-p node)
1331 (delete-lvar-use node))
1333 (let* ((ctran (node-next node))
1334 (next (and ctran (ctran-next ctran)))
1335 (prev (node-prev node))
1336 (block (ctran-block prev))
1337 (prev-kind (ctran-kind prev))
1338 (last (block-last block)))
1340 (setf (block-type-asserted block) t)
1341 (setf (block-test-modified block) t)
1343 (cond ((or (eq prev-kind :inside-block)
1344 (and (eq prev-kind :block-start)
1345 (not (eq node last))))
1346 (cond ((eq node last)
1347 (setf (block-last block) (ctran-use prev))
1348 (setf (node-next (ctran-use prev)) nil))
1350 (setf (ctran-next prev) next)
1351 (setf (node-prev next) prev)
1352 (when (if-p next) ; AOP wanted
1353 (reoptimize-lvar (if-test next)))))
1354 (setf (node-prev node) nil)
1357 (aver (eq prev-kind :block-start))
1358 (aver (eq node last))
1359 (let* ((succ (block-succ block))
1360 (next (first succ)))
1361 (aver (singleton-p succ))
1363 ((eq block (first succ))
1364 (with-ir1-environment-from-node node
1365 (let ((exit (make-exit)))
1366 (setf (ctran-next prev) nil)
1367 (link-node-to-previous-ctran exit prev)
1368 (setf (block-last block) exit)))
1369 (setf (node-prev node) nil)
1372 (aver (eq (block-start-cleanup block)
1373 (block-end-cleanup block)))
1374 (unlink-blocks block next)
1375 (dolist (pred (block-pred block))
1376 (change-block-successor pred block next))
1377 (when (block-delete-p block)
1378 (let ((component (block-component block)))
1379 (setf (component-delete-blocks component)
1380 (delq block (component-delete-blocks component)))))
1381 (remove-from-dfo block)
1382 (setf (block-delete-p block) t)
1383 (setf (node-prev node) nil)
1386 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1388 (defun ctran-deleted-p (ctran)
1389 (declare (type ctran ctran))
1390 (let ((block (ctran-block ctran)))
1391 (or (not (block-component block))
1392 (block-delete-p block))))
1394 ;;; Return true if NODE has been deleted, false if it is still a valid
1396 (defun node-deleted (node)
1397 (declare (type node node))
1398 (let ((prev (node-prev node)))
1400 (ctran-deleted-p prev))))
1402 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1403 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1404 ;;; triggered by deletion.
1405 (defun delete-component (component)
1406 (declare (type component component))
1407 (aver (null (component-new-functionals component)))
1408 (setf (component-kind component) :deleted)
1409 (do-blocks (block component)
1410 (delete-block-lazily block))
1411 (dolist (fun (component-lambdas component))
1412 (unless (eq (functional-kind fun) :deleted)
1413 (setf (functional-kind fun) nil)
1414 (setf (functional-entry-fun fun) nil)
1415 (setf (leaf-refs fun) nil)
1416 (delete-functional fun)))
1417 (clean-component component)
1420 ;;; Remove all pending blocks to be deleted. Return the nearest live
1421 ;;; block after or equal to BLOCK.
1422 (defun clean-component (component &optional block)
1423 (loop while (component-delete-blocks component)
1424 ;; actual deletion of a block may queue new blocks
1425 do (let ((current (pop (component-delete-blocks component))))
1426 (when (eq block current)
1427 (setq block (block-next block)))
1428 (delete-block current)))
1431 ;;; Convert code of the form
1432 ;;; (FOO ... (FUN ...) ...)
1434 ;;; (FOO ... ... ...).
1435 ;;; In other words, replace the function combination FUN by its
1436 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1437 ;;; to blow out of whatever transform called this. Note, as the number
1438 ;;; of arguments changes, the transform must be prepared to return a
1439 ;;; lambda with a new lambda-list with the correct number of
1441 (defun splice-fun-args (lvar fun num-args)
1443 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments
1444 to feed directly to the LVAR-DEST of LVAR, which must be a
1446 (declare (type lvar lvar)
1448 (type index num-args))
1449 (let ((outside (lvar-dest lvar))
1450 (inside (lvar-uses lvar)))
1451 (aver (combination-p outside))
1452 (unless (combination-p inside)
1453 (give-up-ir1-transform))
1454 (let ((inside-fun (combination-fun inside)))
1455 (unless (eq (lvar-fun-name inside-fun) fun)
1456 (give-up-ir1-transform))
1457 (let ((inside-args (combination-args inside)))
1458 (unless (= (length inside-args) num-args)
1459 (give-up-ir1-transform))
1460 (let* ((outside-args (combination-args outside))
1461 (arg-position (position lvar outside-args))
1462 (before-args (subseq outside-args 0 arg-position))
1463 (after-args (subseq outside-args (1+ arg-position))))
1464 (dolist (arg inside-args)
1465 (setf (lvar-dest arg) outside)
1466 (flush-lvar-externally-checkable-type arg))
1467 (setf (combination-args inside) nil)
1468 (setf (combination-args outside)
1469 (append before-args inside-args after-args))
1470 (change-ref-leaf (lvar-uses inside-fun)
1471 (find-free-fun 'list "???"))
1472 (setf (combination-fun-info inside) (info :function :info 'list)
1473 (combination-kind inside) :known)
1474 (setf (node-derived-type inside) *wild-type*)
1478 (defun extract-fun-args (lvar fun num-args)
1479 (declare (type lvar lvar)
1480 (type (or symbol list) fun)
1481 (type index num-args))
1482 (let ((fun (if (listp fun) fun (list fun))))
1483 (let ((inside (lvar-uses lvar)))
1484 (unless (combination-p inside)
1485 (give-up-ir1-transform))
1486 (let ((inside-fun (combination-fun inside)))
1487 (unless (member (lvar-fun-name inside-fun) fun)
1488 (give-up-ir1-transform))
1489 (let ((inside-args (combination-args inside)))
1490 (unless (= (length inside-args) num-args)
1491 (give-up-ir1-transform))
1492 (values (lvar-fun-name inside-fun) inside-args))))))
1494 (defun flush-combination (combination)
1495 (declare (type combination combination))
1496 (flush-dest (combination-fun combination))
1497 (dolist (arg (combination-args combination))
1499 (unlink-node combination)
1505 ;;; Change the LEAF that a REF refers to.
1506 (defun change-ref-leaf (ref leaf)
1507 (declare (type ref ref) (type leaf leaf))
1508 (unless (eq (ref-leaf ref) leaf)
1509 (push ref (leaf-refs leaf))
1511 (setf (ref-leaf ref) leaf)
1512 (setf (leaf-ever-used leaf) t)
1513 (let* ((ltype (leaf-type leaf))
1514 (vltype (make-single-value-type ltype)))
1515 (if (let* ((lvar (node-lvar ref))
1516 (dest (and lvar (lvar-dest lvar))))
1517 (and (basic-combination-p dest)
1518 (eq lvar (basic-combination-fun dest))
1519 (csubtypep ltype (specifier-type 'function))))
1520 (setf (node-derived-type ref) vltype)
1521 (derive-node-type ref vltype)))
1522 (reoptimize-lvar (node-lvar ref)))
1525 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1526 (defun substitute-leaf (new-leaf old-leaf)
1527 (declare (type leaf new-leaf old-leaf))
1528 (dolist (ref (leaf-refs old-leaf))
1529 (change-ref-leaf ref new-leaf))
1532 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1533 ;;; whether to substitute
1534 (defun substitute-leaf-if (test new-leaf old-leaf)
1535 (declare (type leaf new-leaf old-leaf) (type function test))
1536 (dolist (ref (leaf-refs old-leaf))
1537 (when (funcall test ref)
1538 (change-ref-leaf ref new-leaf)))
1541 ;;; Return a LEAF which represents the specified constant object. If
1542 ;;; the object is not in *CONSTANTS*, then we create a new constant
1543 ;;; LEAF and enter it.
1544 (defun find-constant (object)
1546 ;; FIXME: What is the significance of this test? ("things
1547 ;; that are worth uniquifying"?)
1548 '(or symbol number character instance))
1549 (or (gethash object *constants*)
1550 (setf (gethash object *constants*)
1551 (make-constant :value object
1552 :%source-name '.anonymous.
1553 :type (ctype-of object)
1554 :where-from :defined)))
1555 (make-constant :value object
1556 :%source-name '.anonymous.
1557 :type (ctype-of object)
1558 :where-from :defined)))
1560 ;;; Return true if VAR would have to be closed over if environment
1561 ;;; analysis ran now (i.e. if there are any uses that have a different
1562 ;;; home lambda than VAR's home.)
1563 (defun closure-var-p (var)
1564 (declare (type lambda-var var))
1565 (let ((home (lambda-var-home var)))
1566 (cond ((eq (functional-kind home) :deleted)
1568 (t (let ((home (lambda-home home)))
1571 :key #'node-home-lambda
1573 (or (frob (leaf-refs var))
1574 (frob (basic-var-sets var)))))))))
1576 ;;; If there is a non-local exit noted in ENTRY's environment that
1577 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1578 (defun find-nlx-info (exit)
1579 (declare (type exit exit))
1580 (let* ((entry (exit-entry exit))
1581 (cleanup (entry-cleanup entry))
1582 (block (first (block-succ (node-block exit)))))
1583 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1584 (when (and (eq (nlx-info-block nlx) block)
1585 (eq (nlx-info-cleanup nlx) cleanup))
1588 (defun nlx-info-lvar (nlx)
1589 (declare (type nlx-info nlx))
1590 (node-lvar (block-last (nlx-info-target nlx))))
1592 ;;;; functional hackery
1594 (declaim (ftype (sfunction (functional) clambda) main-entry))
1595 (defun main-entry (functional)
1596 (etypecase functional
1597 (clambda functional)
1599 (optional-dispatch-main-entry functional))))
1601 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1602 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1603 ;;; optional with null default and no SUPPLIED-P. There must be a
1604 ;;; &REST arg with no references.
1605 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
1606 (defun looks-like-an-mv-bind (functional)
1607 (and (optional-dispatch-p functional)
1608 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1610 (let ((info (lambda-var-arg-info (car arg))))
1611 (unless info (return nil))
1612 (case (arg-info-kind info)
1614 (when (or (arg-info-supplied-p info) (arg-info-default info))
1617 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1621 ;;; Return true if function is an external entry point. This is true
1622 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1623 ;;; (:TOPLEVEL kind.)
1625 (declare (type functional fun))
1626 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1628 ;;; If LVAR's only use is a non-notinline global function reference,
1629 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1630 ;;; is true, then we don't care if the leaf is NOTINLINE.
1631 (defun lvar-fun-name (lvar &optional notinline-ok)
1632 (declare (type lvar lvar))
1633 (let ((use (lvar-uses lvar)))
1635 (let ((leaf (ref-leaf use)))
1636 (if (and (global-var-p leaf)
1637 (eq (global-var-kind leaf) :global-function)
1638 (or (not (defined-fun-p leaf))
1639 (not (eq (defined-fun-inlinep leaf) :notinline))
1641 (leaf-source-name leaf)
1645 (defun lvar-fun-debug-name (lvar)
1646 (declare (type lvar lvar))
1647 (let ((uses (lvar-uses lvar)))
1649 (leaf-debug-name (ref-leaf use))))
1652 (mapcar #'name1 uses)))))
1654 ;;; Return the source name of a combination. (This is an idiom
1655 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1656 (defun combination-fun-source-name (combination)
1657 (let ((ref (lvar-uses (combination-fun combination))))
1658 (leaf-source-name (ref-leaf ref))))
1660 ;;; Return the COMBINATION node that is the call to the LET FUN.
1661 (defun let-combination (fun)
1662 (declare (type clambda fun))
1663 (aver (functional-letlike-p fun))
1664 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1666 ;;; Return the initial value lvar for a LET variable, or NIL if there
1668 (defun let-var-initial-value (var)
1669 (declare (type lambda-var var))
1670 (let ((fun (lambda-var-home var)))
1671 (elt (combination-args (let-combination fun))
1672 (position-or-lose var (lambda-vars fun)))))
1674 ;;; Return the LAMBDA that is called by the local CALL.
1675 (defun combination-lambda (call)
1676 (declare (type basic-combination call))
1677 (aver (eq (basic-combination-kind call) :local))
1678 (ref-leaf (lvar-uses (basic-combination-fun call))))
1680 (defvar *inline-expansion-limit* 200
1682 "an upper limit on the number of inline function calls that will be expanded
1683 in any given code object (single function or block compilation)")
1685 ;;; Check whether NODE's component has exceeded its inline expansion
1686 ;;; limit, and warn if so, returning NIL.
1687 (defun inline-expansion-ok (node)
1688 (let ((expanded (incf (component-inline-expansions
1690 (node-block node))))))
1691 (cond ((> expanded *inline-expansion-limit*) nil)
1692 ((= expanded *inline-expansion-limit*)
1693 ;; FIXME: If the objective is to stop the recursive
1694 ;; expansion of inline functions, wouldn't it be more
1695 ;; correct to look back through surrounding expansions
1696 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1697 ;; possibly stored elsewhere too) and suppress expansion
1698 ;; and print this warning when the function being proposed
1699 ;; for inline expansion is found there? (I don't like the
1700 ;; arbitrary numerical limit in principle, and I think
1701 ;; it'll be a nuisance in practice if we ever want the
1702 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1703 ;; arbitrarily huge blocks of code. -- WHN)
1704 (let ((*compiler-error-context* node))
1705 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1706 probably trying to~% ~
1707 inline a recursive function."
1708 *inline-expansion-limit*))
1712 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
1713 (defun assure-functional-live-p (functional)
1714 (declare (type functional functional))
1716 ;; looks LET-converted
1717 (functional-somewhat-letlike-p functional)
1718 ;; It's possible for a LET-converted function to end up
1719 ;; deleted later. In that case, for the purposes of this
1720 ;; analysis, it is LET-converted: LET-converted functionals
1721 ;; are too badly trashed to expand them inline, and deleted
1722 ;; LET-converted functionals are even worse.
1723 (memq (functional-kind functional) '(:deleted :zombie))))
1724 (throw 'locall-already-let-converted functional)))
1726 (defun call-full-like-p (call)
1727 (declare (type combination call))
1728 (let ((kind (basic-combination-kind call)))
1730 (and (eq kind :known)
1731 (let ((info (basic-combination-fun-info call)))
1733 (not (fun-info-ir2-convert info))
1734 (dolist (template (fun-info-templates info) t)
1735 (when (eq (template-ltn-policy template) :fast-safe)
1736 (multiple-value-bind (val win)
1737 (valid-fun-use call (template-type template))
1738 (when (or val (not win)) (return nil)))))))))))
1742 ;;; Apply a function to some arguments, returning a list of the values
1743 ;;; resulting of the evaluation. If an error is signalled during the
1744 ;;; application, then we produce a warning message using WARN-FUN and
1745 ;;; return NIL as our second value to indicate this. NODE is used as
1746 ;;; the error context for any error message, and CONTEXT is a string
1747 ;;; that is spliced into the warning.
1748 (declaim (ftype (sfunction ((or symbol function) list node function string)
1749 (values list boolean))
1751 (defun careful-call (function args node warn-fun context)
1753 (multiple-value-list
1754 (handler-case (apply function args)
1756 (let ((*compiler-error-context* node))
1757 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1758 (return-from careful-call (values nil nil))))))
1761 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1764 ((deffrob (basic careful compiler transform)
1766 (defun ,careful (specifier)
1767 (handler-case (,basic specifier)
1768 (sb!kernel::arg-count-error (condition)
1769 (values nil (list (format nil "~A" condition))))
1770 (simple-error (condition)
1771 (values nil (list* (simple-condition-format-control condition)
1772 (simple-condition-format-arguments condition))))))
1773 (defun ,compiler (specifier)
1774 (multiple-value-bind (type error-args) (,careful specifier)
1776 (apply #'compiler-error error-args))))
1777 (defun ,transform (specifier)
1778 (multiple-value-bind (type error-args) (,careful specifier)
1780 (apply #'give-up-ir1-transform
1782 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1783 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1786 ;;;; utilities used at run-time for parsing &KEY args in IR1
1788 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1789 ;;; the lvar for the value of the &KEY argument KEY in the list of
1790 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
1791 ;;; otherwise. The legality and constantness of the keywords should
1792 ;;; already have been checked.
1793 (declaim (ftype (sfunction (list keyword) (or lvar null))
1795 (defun find-keyword-lvar (args key)
1796 (do ((arg args (cddr arg)))
1798 (when (eq (lvar-value (first arg)) key)
1799 (return (second arg)))))
1801 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1802 ;;; verify that alternating lvars in ARGS are constant and that there
1803 ;;; is an even number of args.
1804 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
1805 (defun check-key-args-constant (args)
1806 (do ((arg args (cddr arg)))
1808 (unless (and (rest arg)
1809 (constant-lvar-p (first arg)))
1812 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1813 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
1814 ;;; and that only keywords present in the list KEYS are supplied.
1815 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
1816 (defun check-transform-keys (args keys)
1817 (and (check-key-args-constant args)
1818 (do ((arg args (cddr arg)))
1820 (unless (member (lvar-value (first arg)) keys)
1825 ;;; Called by the expansion of the EVENT macro.
1826 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
1827 (defun %event (info node)
1828 (incf (event-info-count info))
1829 (when (and (>= (event-info-level info) *event-note-threshold*)
1830 (policy (or node *lexenv*)
1831 (= inhibit-warnings 0)))
1832 (let ((*compiler-error-context* node))
1833 (compiler-notify (event-info-description info))))
1835 (let ((action (event-info-action info)))
1836 (when action (funcall action node))))
1839 (defun make-cast (value type policy)
1840 (declare (type lvar value)
1842 (type policy policy))
1843 (%make-cast :asserted-type type
1844 :type-to-check (maybe-weaken-check type policy)
1846 :derived-type (coerce-to-values type)))
1848 (defun cast-type-check (cast)
1849 (declare (type cast cast))
1850 (when (cast-reoptimize cast)
1851 (ir1-optimize-cast cast t))
1852 (cast-%type-check cast))
1854 (defun note-single-valuified-lvar (lvar)
1855 (declare (type (or lvar null) lvar))
1857 (let ((use (lvar-uses lvar)))
1859 (let ((leaf (ref-leaf use)))
1860 (when (and (lambda-var-p leaf)
1861 (null (rest (leaf-refs leaf))))
1862 (reoptimize-lambda-var leaf))))
1863 ((or (listp use) (combination-p use))
1864 (do-uses (node lvar)
1865 (setf (node-reoptimize node) t)
1866 (setf (block-reoptimize (node-block node)) t)
1867 (reoptimize-component (node-component node) :maybe)))))))
1869 ;;; True if LVAR is for 'NAME, or #'NAME (global, not local)
1870 (defun lvar-for-named-function (lvar name)
1871 (if (constant-lvar-p lvar)
1872 (eq name (lvar-value lvar))
1873 (let ((use (lvar-uses lvar)))
1874 (and (not (listp use))
1876 (let ((leaf (ref-leaf use)))
1877 (and (global-var-p leaf)
1878 (eq :global-function (global-var-kind leaf))
1879 (eq name (leaf-source-name leaf))))))))