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 ;;; Make a CAST and insert it into IR1 before node NEXT.
330 (defun insert-cast-before (next lvar type policy)
331 (declare (type node next) (type lvar lvar) (type ctype type))
332 (with-ir1-environment-from-node next
333 (let* ((ctran (node-prev next))
334 (cast (make-cast lvar type policy))
335 (internal-ctran (make-ctran)))
336 (setf (ctran-next ctran) cast
337 (node-prev cast) ctran)
338 (use-ctran cast internal-ctran)
339 (link-node-to-previous-ctran next internal-ctran)
340 (setf (lvar-dest lvar) cast)
341 (reoptimize-lvar lvar)
342 (when (return-p next)
343 (node-ends-block cast))
344 (setf (block-attributep (block-flags (node-block cast))
345 type-check type-asserted)
349 ;;;; miscellaneous shorthand functions
351 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
352 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
353 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
354 ;;; deleted, and then return its home.
355 (defun node-home-lambda (node)
356 (declare (type node node))
357 (do ((fun (lexenv-lambda (node-lexenv node))
358 (lexenv-lambda (lambda-call-lexenv fun))))
359 ((not (memq (functional-kind fun) '(:deleted :zombie)))
361 (when (eq (lambda-home fun) fun)
364 #!-sb-fluid (declaim (inline node-block))
365 (defun node-block (node)
366 (ctran-block (node-prev node)))
367 (declaim (ftype (sfunction (node) component) node-component))
368 (defun node-component (node)
369 (block-component (node-block node)))
370 (declaim (ftype (sfunction (node) physenv) node-physenv))
371 (defun node-physenv (node)
372 (lambda-physenv (node-home-lambda node)))
373 #!-sb-fluid (declaim (inline node-dest))
374 (defun node-dest (node)
375 (awhen (node-lvar node) (lvar-dest it)))
377 #!-sb-fluid (declaim (inline node-stack-allocate-p))
378 (defun node-stack-allocate-p (node)
379 (awhen (node-lvar node)
380 (lvar-dynamic-extent it)))
382 (declaim (inline block-to-be-deleted-p))
383 (defun block-to-be-deleted-p (block)
384 (or (block-delete-p block)
385 (eq (functional-kind (block-home-lambda block)) :deleted)))
387 ;;; Checks whether NODE is in a block to be deleted
388 (declaim (inline node-to-be-deleted-p))
389 (defun node-to-be-deleted-p (node)
390 (block-to-be-deleted-p (node-block node)))
392 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
393 (defun lambda-block (clambda)
394 (node-block (lambda-bind clambda)))
395 (declaim (ftype (sfunction (clambda) component) lambda-component))
396 (defun lambda-component (clambda)
397 (block-component (lambda-block clambda)))
399 (declaim (ftype (sfunction (cblock) node) block-start-node))
400 (defun block-start-node (block)
401 (ctran-next (block-start block)))
403 ;;; Return the enclosing cleanup for environment of the first or last
405 (defun block-start-cleanup (block)
406 (node-enclosing-cleanup (block-start-node block)))
407 (defun block-end-cleanup (block)
408 (node-enclosing-cleanup (block-last block)))
410 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
411 ;;; if there is none.
413 ;;; There can legitimately be no home lambda in dead code early in the
414 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
415 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
416 ;;; where the block is just a placeholder during parsing and doesn't
417 ;;; actually correspond to code which will be written anywhere.
418 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
419 (defun block-home-lambda-or-null (block)
420 (if (node-p (block-last block))
421 ;; This is the old CMU CL way of doing it.
422 (node-home-lambda (block-last block))
423 ;; Now that SBCL uses this operation more aggressively than CMU
424 ;; CL did, the old CMU CL way of doing it can fail in two ways.
425 ;; 1. It can fail in a few cases even when a meaningful home
426 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
428 ;; 2. It can fail when converting a form which is born orphaned
429 ;; so that it never had a meaningful home lambda, e.g. a form
430 ;; which follows a RETURN-FROM or GO form.
431 (let ((pred-list (block-pred block)))
432 ;; To deal with case 1, we reason that
433 ;; previous-in-target-execution-order blocks should be in the
434 ;; same lambda, and that they seem in practice to be
435 ;; previous-in-compilation-order blocks too, so we look back
436 ;; to find one which is sufficiently initialized to tell us
437 ;; what the home lambda is.
439 ;; We could get fancy about this, flooding through the
440 ;; graph of all the previous blocks, but in practice it
441 ;; seems to work just to grab the first previous block and
443 (node-home-lambda (block-last (first pred-list)))
444 ;; In case 2, we end up with an empty PRED-LIST and
445 ;; have to punt: There's no home lambda.
448 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
449 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
450 (defun block-home-lambda (block)
451 (block-home-lambda-or-null block))
453 ;;; Return the IR1 physical environment for BLOCK.
454 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
455 (defun block-physenv (block)
456 (lambda-physenv (block-home-lambda block)))
458 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
459 ;;; of its original source's top level form in its compilation unit.
460 (defun source-path-tlf-number (path)
461 (declare (list path))
464 ;;; Return the (reversed) list for the PATH in the original source
465 ;;; (with the Top Level Form number last).
466 (defun source-path-original-source (path)
467 (declare (list path) (inline member))
468 (cddr (member 'original-source-start path :test #'eq)))
470 ;;; Return the Form Number of PATH's original source inside the Top
471 ;;; Level Form that contains it. This is determined by the order that
472 ;;; we walk the subforms of the top level source form.
473 (defun source-path-form-number (path)
474 (declare (list path) (inline member))
475 (cadr (member 'original-source-start path :test #'eq)))
477 ;;; Return a list of all the enclosing forms not in the original
478 ;;; source that converted to get to this form, with the immediate
479 ;;; source for node at the start of the list.
480 (defun source-path-forms (path)
481 (subseq path 0 (position 'original-source-start path)))
483 ;;; Return the innermost source form for NODE.
484 (defun node-source-form (node)
485 (declare (type node node))
486 (let* ((path (node-source-path node))
487 (forms (source-path-forms path)))
490 (values (find-original-source path)))))
492 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
494 (defun lvar-source (lvar)
495 (let ((use (lvar-uses lvar)))
498 (values (node-source-form use) t))))
500 ;;; Return the unique node, delivering a value to LVAR.
501 #!-sb-fluid (declaim (inline lvar-use))
502 (defun lvar-use (lvar)
503 (the (not list) (lvar-uses lvar)))
505 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
506 (defun lvar-has-single-use-p (lvar)
507 (typep (lvar-uses lvar) '(not list)))
509 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
510 (declaim (ftype (sfunction (ctran) (or clambda null))
511 ctran-home-lambda-or-null))
512 (defun ctran-home-lambda-or-null (ctran)
513 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
514 ;; implementation might not be quite right, or might be uglier than
515 ;; necessary. It appears that the original Python never found a need
516 ;; to do this operation. The obvious things based on
517 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
518 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
519 ;; generalize it enough to grovel harder when the simple CMU CL
520 ;; approach fails, and furthermore realize that in some exceptional
521 ;; cases it might return NIL. -- WHN 2001-12-04
522 (cond ((ctran-use ctran)
523 (node-home-lambda (ctran-use ctran)))
525 (block-home-lambda-or-null (ctran-block ctran)))
527 (bug "confused about home lambda for ~S" ctran))))
529 ;;; Return the LAMBDA that is CTRAN's home.
530 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
531 (defun ctran-home-lambda (ctran)
532 (ctran-home-lambda-or-null ctran))
534 (declaim (inline cast-single-value-p))
535 (defun cast-single-value-p (cast)
536 (not (values-type-p (cast-asserted-type cast))))
538 #!-sb-fluid (declaim (inline lvar-single-value-p))
539 (defun lvar-single-value-p (lvar)
541 (let ((dest (lvar-dest lvar)))
546 (eq (basic-combination-fun dest) lvar))
549 (declare (notinline lvar-single-value-p))
550 (and (cast-single-value-p dest)
551 (lvar-single-value-p (node-lvar dest)))))
555 (defun principal-lvar-end (lvar)
556 (loop for prev = lvar then (node-lvar dest)
557 for dest = (and prev (lvar-dest prev))
559 finally (return (values dest prev))))
561 (defun principal-lvar-single-valuify (lvar)
562 (loop for prev = lvar then (node-lvar dest)
563 for dest = (and prev (lvar-dest prev))
565 do (setf (node-derived-type dest)
566 (make-short-values-type (list (single-value-type
567 (node-derived-type dest)))))
568 (reoptimize-lvar prev)))
570 ;;; Return a new LEXENV just like DEFAULT except for the specified
571 ;;; slot values. Values for the alist slots are NCONCed to the
572 ;;; beginning of the current value, rather than replacing it entirely.
573 (defun make-lexenv (&key (default *lexenv*)
574 funs vars blocks tags
576 (lambda (lexenv-lambda default))
577 (cleanup (lexenv-cleanup default))
578 (handled-conditions (lexenv-handled-conditions default))
579 (disabled-package-locks
580 (lexenv-disabled-package-locks default))
581 (policy (lexenv-policy default)))
582 (macrolet ((frob (var slot)
583 `(let ((old (,slot default)))
587 (internal-make-lexenv
588 (frob funs lexenv-funs)
589 (frob vars lexenv-vars)
590 (frob blocks lexenv-blocks)
591 (frob tags lexenv-tags)
592 (frob type-restrictions lexenv-type-restrictions)
593 lambda cleanup handled-conditions
594 disabled-package-locks policy)))
596 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
598 (defun make-restricted-lexenv (lexenv)
599 (flet ((fun-good-p (fun)
600 (destructuring-bind (name . thing) fun
601 (declare (ignore name))
605 (cons (aver (eq (car thing) 'macro))
608 (destructuring-bind (name . thing) var
609 (declare (ignore name))
612 (cons (aver (eq (car thing) 'macro))
614 (heap-alien-info nil)))))
615 (internal-make-lexenv
616 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
617 (remove-if-not #'var-good-p (lexenv-vars lexenv))
620 (lexenv-type-restrictions lexenv) ; XXX
623 (lexenv-handled-conditions lexenv)
624 (lexenv-disabled-package-locks lexenv)
625 (lexenv-policy lexenv))))
627 ;;;; flow/DFO/component hackery
629 ;;; Join BLOCK1 and BLOCK2.
630 (defun link-blocks (block1 block2)
631 (declare (type cblock block1 block2))
632 (setf (block-succ block1)
633 (if (block-succ block1)
634 (%link-blocks block1 block2)
636 (push block1 (block-pred block2))
638 (defun %link-blocks (block1 block2)
639 (declare (type cblock block1 block2))
640 (let ((succ1 (block-succ block1)))
641 (aver (not (memq block2 succ1)))
642 (cons block2 succ1)))
644 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
645 ;;; this leaves a successor with a single predecessor that ends in an
646 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
647 ;;; now be able to be propagated to the successor.
648 (defun unlink-blocks (block1 block2)
649 (declare (type cblock block1 block2))
650 (let ((succ1 (block-succ block1)))
651 (if (eq block2 (car succ1))
652 (setf (block-succ block1) (cdr succ1))
653 (do ((succ (cdr succ1) (cdr succ))
655 ((eq (car succ) block2)
656 (setf (cdr prev) (cdr succ)))
659 (let ((new-pred (delq block1 (block-pred block2))))
660 (setf (block-pred block2) new-pred)
661 (when (singleton-p new-pred)
662 (let ((pred-block (first new-pred)))
663 (when (if-p (block-last pred-block))
664 (setf (block-test-modified pred-block) t)))))
667 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
668 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
669 ;;; consequent/alternative blocks to point to NEW. We also set
670 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
671 ;;; the new successor.
672 (defun change-block-successor (block old new)
673 (declare (type cblock new old block))
674 (unlink-blocks block old)
675 (let ((last (block-last block))
676 (comp (block-component block)))
677 (setf (component-reanalyze comp) t)
680 (setf (block-test-modified block) t)
681 (let* ((succ-left (block-succ block))
682 (new (if (and (eq new (component-tail comp))
686 (unless (memq new succ-left)
687 (link-blocks block new))
688 (macrolet ((frob (slot)
689 `(when (eq (,slot last) old)
690 (setf (,slot last) new))))
692 (frob if-alternative)
693 (when (eq (if-consequent last)
694 (if-alternative last))
695 (reoptimize-component (block-component block) :maybe)))))
697 (unless (memq new (block-succ block))
698 (link-blocks block new)))))
702 ;;; Unlink a block from the next/prev chain. We also null out the
704 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
705 (defun remove-from-dfo (block)
706 (let ((next (block-next block))
707 (prev (block-prev block)))
708 (setf (block-component block) nil)
709 (setf (block-next prev) next)
710 (setf (block-prev next) prev))
713 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
714 ;;; COMPONENT to be the same as for AFTER.
715 (defun add-to-dfo (block after)
716 (declare (type cblock block after))
717 (let ((next (block-next after))
718 (comp (block-component after)))
719 (aver (not (eq (component-kind comp) :deleted)))
720 (setf (block-component block) comp)
721 (setf (block-next after) block)
722 (setf (block-prev block) after)
723 (setf (block-next block) next)
724 (setf (block-prev next) block))
727 ;;; List all NLX-INFOs which BLOCK can exit to.
729 ;;; We hope that no cleanup actions are performed in the middle of
730 ;;; BLOCK, so it is enough to look only at cleanups in the block
731 ;;; end. The tricky thing is a special cleanup block; all its nodes
732 ;;; have the same cleanup info, corresponding to the start, so the
733 ;;; same approach returns safe result.
734 (defun map-block-nlxes (fun block &optional dx-cleanup-fun)
735 (loop for cleanup = (block-end-cleanup block)
736 then (node-enclosing-cleanup (cleanup-mess-up cleanup))
738 do (let ((mess-up (cleanup-mess-up cleanup)))
739 (case (cleanup-kind cleanup)
741 (aver (entry-p mess-up))
742 (loop for exit in (entry-exits mess-up)
743 for nlx-info = (exit-nlx-info exit)
744 do (funcall fun nlx-info)))
745 ((:catch :unwind-protect)
746 (aver (combination-p mess-up))
747 (let* ((arg-lvar (first (basic-combination-args mess-up)))
748 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
749 (funcall fun nlx-info)))
752 (funcall dx-cleanup-fun cleanup)))))))
754 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
755 ;;; the head and tail which are set to T.
756 (declaim (ftype (sfunction (component) (values)) clear-flags))
757 (defun clear-flags (component)
758 (let ((head (component-head component))
759 (tail (component-tail component)))
760 (setf (block-flag head) t)
761 (setf (block-flag tail) t)
762 (do-blocks (block component)
763 (setf (block-flag block) nil)))
766 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
767 ;;; true in the head and tail blocks.
768 (declaim (ftype (sfunction () component) make-empty-component))
769 (defun make-empty-component ()
770 (let* ((head (make-block-key :start nil :component nil))
771 (tail (make-block-key :start nil :component nil))
772 (res (make-component head tail)))
773 (setf (block-flag head) t)
774 (setf (block-flag tail) t)
775 (setf (block-component head) res)
776 (setf (block-component tail) res)
777 (setf (block-next head) tail)
778 (setf (block-prev tail) head)
781 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
782 ;;; The new block is added to the DFO immediately following NODE's block.
783 (defun node-ends-block (node)
784 (declare (type node node))
785 (let* ((block (node-block node))
786 (start (node-next node))
787 (last (block-last block)))
788 (unless (eq last node)
789 (aver (and (eq (ctran-kind start) :inside-block)
790 (not (block-delete-p block))))
791 (let* ((succ (block-succ block))
793 (make-block-key :start start
794 :component (block-component block)
795 :succ succ :last last)))
796 (setf (ctran-kind start) :block-start)
797 (setf (ctran-use start) nil)
798 (setf (block-last block) node)
799 (setf (node-next node) nil)
802 (cons new-block (remove block (block-pred b)))))
803 (setf (block-succ block) ())
804 (link-blocks block new-block)
805 (add-to-dfo new-block block)
806 (setf (component-reanalyze (block-component block)) t)
808 (do ((ctran start (node-next (ctran-next ctran))))
810 (setf (ctran-block ctran) new-block))
812 (setf (block-type-asserted block) t)
813 (setf (block-test-modified block) t))))
818 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
819 (defun delete-lambda-var (leaf)
820 (declare (type lambda-var leaf))
822 ;; Iterate over all local calls flushing the corresponding argument,
823 ;; allowing the computation of the argument to be deleted. We also
824 ;; mark the LET for reoptimization, since it may be that we have
825 ;; deleted its last variable.
826 (let* ((fun (lambda-var-home leaf))
827 (n (position leaf (lambda-vars fun))))
828 (dolist (ref (leaf-refs fun))
829 (let* ((lvar (node-lvar ref))
830 (dest (and lvar (lvar-dest lvar))))
831 (when (and (combination-p dest)
832 (eq (basic-combination-fun dest) lvar)
833 (eq (basic-combination-kind dest) :local))
834 (let* ((args (basic-combination-args dest))
836 (reoptimize-lvar arg)
838 (setf (elt args n) nil))))))
840 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
841 ;; too much difficulty, since we can efficiently implement
842 ;; write-only variables. We iterate over the SETs, marking their
843 ;; blocks for dead code flushing, since we can delete SETs whose
845 (dolist (set (lambda-var-sets leaf))
846 (setf (block-flush-p (node-block set)) t))
850 ;;; Note that something interesting has happened to VAR.
851 (defun reoptimize-lambda-var (var)
852 (declare (type lambda-var var))
853 (let ((fun (lambda-var-home var)))
854 ;; We only deal with LET variables, marking the corresponding
855 ;; initial value arg as needing to be reoptimized.
856 (when (and (eq (functional-kind fun) :let)
858 (do ((args (basic-combination-args
859 (lvar-dest (node-lvar (first (leaf-refs fun)))))
861 (vars (lambda-vars fun) (cdr vars)))
863 (reoptimize-lvar (car args))))))
866 ;;; Delete a function that has no references. This need only be called
867 ;;; on functions that never had any references, since otherwise
868 ;;; DELETE-REF will handle the deletion.
869 (defun delete-functional (fun)
870 (aver (and (null (leaf-refs fun))
871 (not (functional-entry-fun fun))))
873 (optional-dispatch (delete-optional-dispatch fun))
874 (clambda (delete-lambda fun)))
877 ;;; Deal with deleting the last reference to a CLAMBDA, which means
878 ;;; that the lambda is unreachable, so that its body may be
879 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
880 ;;; IR1-OPTIMIZE to delete its blocks.
881 (defun delete-lambda (clambda)
882 (declare (type clambda clambda))
883 (let ((original-kind (functional-kind clambda))
884 (bind (lambda-bind clambda)))
885 (aver (not (member original-kind '(:deleted :toplevel))))
886 (aver (not (functional-has-external-references-p clambda)))
887 (aver (or (eq original-kind :zombie) bind))
888 (setf (functional-kind clambda) :deleted)
889 (setf (lambda-bind clambda) nil)
891 (labels ((delete-children (lambda)
892 (dolist (child (lambda-children lambda))
893 (cond ((eq (functional-kind child) :deleted)
894 (delete-children child))
896 (delete-lambda child))))
897 (setf (lambda-children lambda) nil)
898 (setf (lambda-parent lambda) nil)))
899 (delete-children clambda))
901 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
902 ;; that we're using the old value of the KIND slot, not the
903 ;; current slot value, which has now been set to :DELETED.)
906 ((:let :mv-let :assignment)
907 (let ((bind-block (node-block bind)))
908 (mark-for-deletion bind-block))
909 (let ((home (lambda-home clambda)))
910 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
911 ;; KLUDGE: In presence of NLEs we cannot always understand that
912 ;; LET's BIND dominates its body [for a LET "its" body is not
913 ;; quite its]; let's delete too dangerous for IR2 stuff. --
915 (dolist (var (lambda-vars clambda))
916 (flet ((delete-node (node)
917 (mark-for-deletion (node-block node))))
918 (mapc #'delete-node (leaf-refs var))
919 (mapc #'delete-node (lambda-var-sets var)))))
921 ;; Function has no reachable references.
922 (dolist (ref (lambda-refs clambda))
923 (mark-for-deletion (node-block ref)))
924 ;; If the function isn't a LET, we unlink the function head
925 ;; and tail from the component head and tail to indicate that
926 ;; the code is unreachable. We also delete the function from
927 ;; COMPONENT-LAMBDAS (it won't be there before local call
928 ;; analysis, but no matter.) If the lambda was never
929 ;; referenced, we give a note.
930 (let* ((bind-block (node-block bind))
931 (component (block-component bind-block))
932 (return (lambda-return clambda))
933 (return-block (and return (node-block return))))
934 (unless (leaf-ever-used clambda)
935 (let ((*compiler-error-context* bind))
936 (compiler-notify 'code-deletion-note
937 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
938 :format-arguments (list (leaf-debug-name clambda)))))
939 (unless (block-delete-p bind-block)
940 (unlink-blocks (component-head component) bind-block))
941 (when (and return-block (not (block-delete-p return-block)))
942 (mark-for-deletion return-block)
943 (unlink-blocks return-block (component-tail component)))
944 (setf (component-reanalyze component) t)
945 (let ((tails (lambda-tail-set clambda)))
946 (setf (tail-set-funs tails)
947 (delete clambda (tail-set-funs tails)))
948 (setf (lambda-tail-set clambda) nil))
949 (setf (component-lambdas component)
950 (delq clambda (component-lambdas component))))))
952 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
953 ;; ENTRY-FUN so that people will know that it is not an entry
955 (when (eq original-kind :external)
956 (let ((fun (functional-entry-fun clambda)))
957 (setf (functional-entry-fun fun) nil)
958 (when (optional-dispatch-p fun)
959 (delete-optional-dispatch fun)))))
963 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
964 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
965 ;;; is used both before and after local call analysis. Afterward, all
966 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
967 ;;; to the XEP, leaving it with no references at all. So we look at
968 ;;; the XEP to see whether an optional-dispatch is still really being
969 ;;; used. But before local call analysis, there are no XEPs, and all
970 ;;; references are direct.
972 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
973 ;;; entry-points, making them be normal lambdas, and then deleting the
974 ;;; ones with no references. This deletes any e-p lambdas that were
975 ;;; either never referenced, or couldn't be deleted when the last
976 ;;; reference was deleted (due to their :OPTIONAL kind.)
978 ;;; Note that the last optional entry point may alias the main entry,
979 ;;; so when we process the main entry, its KIND may have been changed
980 ;;; to NIL or even converted to a LETlike value.
981 (defun delete-optional-dispatch (leaf)
982 (declare (type optional-dispatch leaf))
983 (let ((entry (functional-entry-fun leaf)))
984 (unless (and entry (leaf-refs entry))
985 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
986 (setf (functional-kind leaf) :deleted)
989 (unless (eq (functional-kind fun) :deleted)
990 (aver (eq (functional-kind fun) :optional))
991 (setf (functional-kind fun) nil)
992 (let ((refs (leaf-refs fun)))
996 (or (maybe-let-convert fun)
997 (maybe-convert-to-assignment fun)))
999 (maybe-convert-to-assignment fun)))))))
1001 (dolist (ep (optional-dispatch-entry-points leaf))
1002 (when (promise-ready-p ep)
1004 (when (optional-dispatch-more-entry leaf)
1005 (frob (optional-dispatch-more-entry leaf)))
1006 (let ((main (optional-dispatch-main-entry leaf)))
1008 (setf (functional-entry-fun entry) main)
1009 (setf (functional-entry-fun main) entry))
1010 (when (eq (functional-kind main) :optional)
1015 ;;; Do stuff to delete the semantic attachments of a REF node. When
1016 ;;; this leaves zero or one reference, we do a type dispatch off of
1017 ;;; the leaf to determine if a special action is appropriate.
1018 (defun delete-ref (ref)
1019 (declare (type ref ref))
1020 (let* ((leaf (ref-leaf ref))
1021 (refs (delq ref (leaf-refs leaf))))
1022 (setf (leaf-refs leaf) refs)
1027 (delete-lambda-var leaf))
1029 (ecase (functional-kind leaf)
1030 ((nil :let :mv-let :assignment :escape :cleanup)
1031 (aver (null (functional-entry-fun leaf)))
1032 (delete-lambda leaf))
1034 (delete-lambda leaf))
1035 ((:deleted :zombie :optional))))
1037 (unless (eq (functional-kind leaf) :deleted)
1038 (delete-optional-dispatch leaf)))))
1041 (clambda (or (maybe-let-convert leaf)
1042 (maybe-convert-to-assignment leaf)))
1043 (lambda-var (reoptimize-lambda-var leaf))))
1046 (clambda (maybe-convert-to-assignment leaf))))))
1050 ;;; This function is called by people who delete nodes; it provides a
1051 ;;; way to indicate that the value of a lvar is no longer used. We
1052 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1053 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1054 (defun flush-dest (lvar)
1055 (declare (type (or lvar null) lvar))
1057 (setf (lvar-dest lvar) nil)
1058 (flush-lvar-externally-checkable-type lvar)
1060 (let ((prev (node-prev use)))
1061 (let ((block (ctran-block prev)))
1062 (reoptimize-component (block-component block) t)
1063 (setf (block-attributep (block-flags block)
1064 flush-p type-asserted type-check)
1066 (setf (node-lvar use) nil))
1067 (setf (lvar-uses lvar) nil))
1070 (defun delete-dest (lvar)
1072 (let* ((dest (lvar-dest lvar))
1073 (prev (node-prev dest)))
1074 (let ((block (ctran-block prev)))
1075 (unless (block-delete-p block)
1076 (mark-for-deletion block))))))
1078 ;;; Queue the block for deletion
1079 (defun delete-block-lazily (block)
1080 (declare (type cblock block))
1081 (unless (block-delete-p block)
1082 (setf (block-delete-p block) t)
1083 (push block (component-delete-blocks (block-component block)))))
1085 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1086 ;;; blocks with the DELETE-P flag.
1087 (defun mark-for-deletion (block)
1088 (declare (type cblock block))
1089 (let* ((component (block-component block))
1090 (head (component-head component)))
1091 (labels ((helper (block)
1092 (delete-block-lazily block)
1093 (dolist (pred (block-pred block))
1094 (unless (or (block-delete-p pred)
1097 (unless (block-delete-p block)
1099 (setf (component-reanalyze component) t))))
1102 ;;; This function does what is necessary to eliminate the code in it
1103 ;;; from the IR1 representation. This involves unlinking it from its
1104 ;;; predecessors and successors and deleting various node-specific
1105 ;;; semantic information. BLOCK must be already removed from
1106 ;;; COMPONENT-DELETE-BLOCKS.
1107 (defun delete-block (block &optional silent)
1108 (declare (type cblock block))
1109 (aver (block-component block)) ; else block is already deleted!
1110 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1112 (note-block-deletion block))
1113 (setf (block-delete-p block) t)
1115 (dolist (b (block-pred block))
1116 (unlink-blocks b block)
1117 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1118 ;; broken when successors were deleted without setting the
1119 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1120 ;; doesn't happen again.
1121 (aver (not (and (null (block-succ b))
1122 (not (block-delete-p b))
1123 (not (eq b (component-head (block-component b))))))))
1124 (dolist (b (block-succ block))
1125 (unlink-blocks block b))
1127 (do-nodes-carefully (node block)
1128 (when (valued-node-p node)
1129 (delete-lvar-use node))
1131 (ref (delete-ref node))
1132 (cif (flush-dest (if-test node)))
1133 ;; The next two cases serve to maintain the invariant that a LET
1134 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1135 ;; the lambda whenever we delete any of these, but we must be
1136 ;; careful that this LET has not already been partially deleted.
1138 (when (and (eq (basic-combination-kind node) :local)
1139 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1140 (lvar-uses (basic-combination-fun node)))
1141 (let ((fun (combination-lambda node)))
1142 ;; If our REF was the second-to-last ref, and has been
1143 ;; deleted, then FUN may be a LET for some other
1145 (when (and (functional-letlike-p fun)
1146 (eq (let-combination fun) node))
1147 (delete-lambda fun))))
1148 (flush-dest (basic-combination-fun node))
1149 (dolist (arg (basic-combination-args node))
1150 (when arg (flush-dest arg))))
1152 (let ((lambda (bind-lambda node)))
1153 (unless (eq (functional-kind lambda) :deleted)
1154 (delete-lambda lambda))))
1156 (let ((value (exit-value node))
1157 (entry (exit-entry node)))
1161 (setf (entry-exits entry)
1162 (delq node (entry-exits entry))))))
1164 (dolist (exit (entry-exits node))
1165 (mark-for-deletion (node-block exit)))
1166 (let ((home (node-home-lambda node)))
1167 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1169 (flush-dest (return-result node))
1170 (delete-return node))
1172 (flush-dest (set-value node))
1173 (let ((var (set-var node)))
1174 (setf (basic-var-sets var)
1175 (delete node (basic-var-sets var)))))
1177 (flush-dest (cast-value node)))))
1179 (remove-from-dfo block)
1182 ;;; Do stuff to indicate that the return node NODE is being deleted.
1183 (defun delete-return (node)
1184 (declare (type creturn node))
1185 (let* ((fun (return-lambda node))
1186 (tail-set (lambda-tail-set fun)))
1187 (aver (lambda-return fun))
1188 (setf (lambda-return fun) nil)
1189 (when (and tail-set (not (find-if #'lambda-return
1190 (tail-set-funs tail-set))))
1191 (setf (tail-set-type tail-set) *empty-type*)))
1194 ;;; If any of the VARS in FUN was never referenced and was not
1195 ;;; declared IGNORE, then complain.
1196 (defun note-unreferenced-vars (fun)
1197 (declare (type clambda fun))
1198 (dolist (var (lambda-vars fun))
1199 (unless (or (leaf-ever-used var)
1200 (lambda-var-ignorep var))
1201 (let ((*compiler-error-context* (lambda-bind fun)))
1202 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1203 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1204 ;; requires this to be no more than a STYLE-WARNING.
1206 (compiler-style-warn "The variable ~S is defined but never used."
1207 (leaf-debug-name var))
1208 ;; There's no reason to accept this kind of equivocation
1209 ;; when compiling our own code, though.
1211 (warn "The variable ~S is defined but never used."
1212 (leaf-debug-name var)))
1213 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1216 (defvar *deletion-ignored-objects* '(t nil))
1218 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1219 ;;; our recursion so that we don't get lost in circular structures. We
1220 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1221 ;;; function referencess with variables), and we also ignore anything
1223 (defun present-in-form (obj form depth)
1224 (declare (type (integer 0 20) depth))
1225 (cond ((= depth 20) nil)
1229 (let ((first (car form))
1231 (if (member first '(quote function))
1233 (or (and (not (symbolp first))
1234 (present-in-form obj first depth))
1235 (do ((l (cdr form) (cdr l))
1237 ((or (atom l) (> n 100))
1239 (declare (fixnum n))
1240 (when (present-in-form obj (car l) depth)
1243 ;;; This function is called on a block immediately before we delete
1244 ;;; it. We check to see whether any of the code about to die appeared
1245 ;;; in the original source, and emit a note if so.
1247 ;;; If the block was in a lambda is now deleted, then we ignore the
1248 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1249 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1250 ;;; reasonable for a function to not return, and there is a different
1251 ;;; note for that case anyway.
1253 ;;; If the actual source is an atom, then we use a bunch of heuristics
1254 ;;; to guess whether this reference really appeared in the original
1256 ;;; -- If a symbol, it must be interned and not a keyword.
1257 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1258 ;;; or a character.)
1259 ;;; -- The atom must be "present" in the original source form, and
1260 ;;; present in all intervening actual source forms.
1261 (defun note-block-deletion (block)
1262 (let ((home (block-home-lambda block)))
1263 (unless (eq (functional-kind home) :deleted)
1264 (do-nodes (node nil block)
1265 (let* ((path (node-source-path node))
1266 (first (first path)))
1267 (when (or (eq first 'original-source-start)
1269 (or (not (symbolp first))
1270 (let ((pkg (symbol-package first)))
1272 (not (eq pkg (symbol-package :end))))))
1273 (not (member first *deletion-ignored-objects*))
1274 (not (typep first '(or fixnum character)))
1276 (present-in-form first x 0))
1277 (source-path-forms path))
1278 (present-in-form first (find-original-source path)
1280 (unless (return-p node)
1281 (let ((*compiler-error-context* node))
1282 (compiler-notify 'code-deletion-note
1283 :format-control "deleting unreachable code"
1284 :format-arguments nil)))
1288 ;;; Delete a node from a block, deleting the block if there are no
1289 ;;; nodes left. We remove the node from the uses of its LVAR.
1291 ;;; If the node is the last node, there must be exactly one successor.
1292 ;;; We link all of our precedessors to the successor and unlink the
1293 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1294 ;;; left, and the block is a successor of itself, then we replace the
1295 ;;; only node with a degenerate exit node. This provides a way to
1296 ;;; represent the bodyless infinite loop, given the prohibition on
1297 ;;; empty blocks in IR1.
1298 (defun unlink-node (node)
1299 (declare (type node node))
1300 (when (valued-node-p node)
1301 (delete-lvar-use node))
1303 (let* ((ctran (node-next node))
1304 (next (and ctran (ctran-next ctran)))
1305 (prev (node-prev node))
1306 (block (ctran-block prev))
1307 (prev-kind (ctran-kind prev))
1308 (last (block-last block)))
1310 (setf (block-type-asserted block) t)
1311 (setf (block-test-modified block) t)
1313 (cond ((or (eq prev-kind :inside-block)
1314 (and (eq prev-kind :block-start)
1315 (not (eq node last))))
1316 (cond ((eq node last)
1317 (setf (block-last block) (ctran-use prev))
1318 (setf (node-next (ctran-use prev)) nil))
1320 (setf (ctran-next prev) next)
1321 (setf (node-prev next) prev)
1322 (when (if-p next) ; AOP wanted
1323 (reoptimize-lvar (if-test next)))))
1324 (setf (node-prev node) nil)
1327 (aver (eq prev-kind :block-start))
1328 (aver (eq node last))
1329 (let* ((succ (block-succ block))
1330 (next (first succ)))
1331 (aver (singleton-p succ))
1333 ((eq block (first succ))
1334 (with-ir1-environment-from-node node
1335 (let ((exit (make-exit)))
1336 (setf (ctran-next prev) nil)
1337 (link-node-to-previous-ctran exit prev)
1338 (setf (block-last block) exit)))
1339 (setf (node-prev node) nil)
1342 (aver (eq (block-start-cleanup block)
1343 (block-end-cleanup block)))
1344 (unlink-blocks block next)
1345 (dolist (pred (block-pred block))
1346 (change-block-successor pred block next))
1347 (when (block-delete-p block)
1348 (let ((component (block-component block)))
1349 (setf (component-delete-blocks component)
1350 (delq block (component-delete-blocks component)))))
1351 (remove-from-dfo block)
1352 (setf (block-delete-p block) t)
1353 (setf (node-prev node) nil)
1356 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1358 (defun ctran-deleted-p (ctran)
1359 (declare (type ctran ctran))
1360 (let ((block (ctran-block ctran)))
1361 (or (not (block-component block))
1362 (block-delete-p block))))
1364 ;;; Return true if NODE has been deleted, false if it is still a valid
1366 (defun node-deleted (node)
1367 (declare (type node node))
1368 (let ((prev (node-prev node)))
1370 (ctran-deleted-p prev))))
1372 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1373 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1374 ;;; triggered by deletion.
1375 (defun delete-component (component)
1376 (declare (type component component))
1377 (aver (null (component-new-functionals component)))
1378 (setf (component-kind component) :deleted)
1379 (do-blocks (block component)
1380 (delete-block-lazily block))
1381 (dolist (fun (component-lambdas component))
1382 (unless (eq (functional-kind fun) :deleted)
1383 (setf (functional-kind fun) nil)
1384 (setf (functional-entry-fun fun) nil)
1385 (setf (leaf-refs fun) nil)
1386 (delete-functional fun)))
1387 (clean-component component)
1390 ;;; Remove all pending blocks to be deleted. Return the nearest live
1391 ;;; block after or equal to BLOCK.
1392 (defun clean-component (component &optional block)
1393 (loop while (component-delete-blocks component)
1394 ;; actual deletion of a block may queue new blocks
1395 do (let ((current (pop (component-delete-blocks component))))
1396 (when (eq block current)
1397 (setq block (block-next block)))
1398 (delete-block current)))
1401 ;;; Convert code of the form
1402 ;;; (FOO ... (FUN ...) ...)
1404 ;;; (FOO ... ... ...).
1405 ;;; In other words, replace the function combination FUN by its
1406 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1407 ;;; to blow out of whatever transform called this. Note, as the number
1408 ;;; of arguments changes, the transform must be prepared to return a
1409 ;;; lambda with a new lambda-list with the correct number of
1411 (defun extract-fun-args (lvar fun num-args)
1413 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments
1414 to feed directly to the LVAR-DEST of LVAR, which must be a
1416 (declare (type lvar lvar)
1418 (type index num-args))
1419 (let ((outside (lvar-dest lvar))
1420 (inside (lvar-uses lvar)))
1421 (aver (combination-p outside))
1422 (unless (combination-p inside)
1423 (give-up-ir1-transform))
1424 (let ((inside-fun (combination-fun inside)))
1425 (unless (eq (lvar-fun-name inside-fun) fun)
1426 (give-up-ir1-transform))
1427 (let ((inside-args (combination-args inside)))
1428 (unless (= (length inside-args) num-args)
1429 (give-up-ir1-transform))
1430 (let* ((outside-args (combination-args outside))
1431 (arg-position (position lvar outside-args))
1432 (before-args (subseq outside-args 0 arg-position))
1433 (after-args (subseq outside-args (1+ arg-position))))
1434 (dolist (arg inside-args)
1435 (setf (lvar-dest arg) outside)
1436 (flush-lvar-externally-checkable-type arg))
1437 (setf (combination-args inside) nil)
1438 (setf (combination-args outside)
1439 (append before-args inside-args after-args))
1440 (change-ref-leaf (lvar-uses inside-fun)
1441 (find-free-fun 'list "???"))
1442 (setf (combination-fun-info inside) (info :function :info 'list)
1443 (combination-kind inside) :known)
1444 (setf (node-derived-type inside) *wild-type*)
1448 (defun flush-combination (combination)
1449 (declare (type combination combination))
1450 (flush-dest (combination-fun combination))
1451 (dolist (arg (combination-args combination))
1453 (unlink-node combination)
1459 ;;; Change the LEAF that a REF refers to.
1460 (defun change-ref-leaf (ref leaf)
1461 (declare (type ref ref) (type leaf leaf))
1462 (unless (eq (ref-leaf ref) leaf)
1463 (push ref (leaf-refs leaf))
1465 (setf (ref-leaf ref) leaf)
1466 (setf (leaf-ever-used leaf) t)
1467 (let* ((ltype (leaf-type leaf))
1468 (vltype (make-single-value-type ltype)))
1469 (if (let* ((lvar (node-lvar ref))
1470 (dest (and lvar (lvar-dest lvar))))
1471 (and (basic-combination-p dest)
1472 (eq lvar (basic-combination-fun dest))
1473 (csubtypep ltype (specifier-type 'function))))
1474 (setf (node-derived-type ref) vltype)
1475 (derive-node-type ref vltype)))
1476 (reoptimize-lvar (node-lvar ref)))
1479 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1480 (defun substitute-leaf (new-leaf old-leaf)
1481 (declare (type leaf new-leaf old-leaf))
1482 (dolist (ref (leaf-refs old-leaf))
1483 (change-ref-leaf ref new-leaf))
1486 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1487 ;;; whether to substitute
1488 (defun substitute-leaf-if (test new-leaf old-leaf)
1489 (declare (type leaf new-leaf old-leaf) (type function test))
1490 (dolist (ref (leaf-refs old-leaf))
1491 (when (funcall test ref)
1492 (change-ref-leaf ref new-leaf)))
1495 ;;; Return a LEAF which represents the specified constant object. If
1496 ;;; the object is not in *CONSTANTS*, then we create a new constant
1497 ;;; LEAF and enter it.
1498 (defun find-constant (object)
1500 ;; FIXME: What is the significance of this test? ("things
1501 ;; that are worth uniquifying"?)
1502 '(or symbol number character instance))
1503 (or (gethash object *constants*)
1504 (setf (gethash object *constants*)
1505 (make-constant :value object
1506 :%source-name '.anonymous.
1507 :type (ctype-of object)
1508 :where-from :defined)))
1509 (make-constant :value object
1510 :%source-name '.anonymous.
1511 :type (ctype-of object)
1512 :where-from :defined)))
1514 ;;; Return true if VAR would have to be closed over if environment
1515 ;;; analysis ran now (i.e. if there are any uses that have a different
1516 ;;; home lambda than VAR's home.)
1517 (defun closure-var-p (var)
1518 (declare (type lambda-var var))
1519 (let ((home (lambda-var-home var)))
1520 (cond ((eq (functional-kind home) :deleted)
1522 (t (let ((home (lambda-home home)))
1525 :key #'node-home-lambda
1527 (or (frob (leaf-refs var))
1528 (frob (basic-var-sets var)))))))))
1530 ;;; If there is a non-local exit noted in ENTRY's environment that
1531 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1532 (defun find-nlx-info (exit)
1533 (declare (type exit exit))
1534 (let* ((entry (exit-entry exit))
1535 (cleanup (entry-cleanup entry))
1536 (block (first (block-succ (node-block exit)))))
1537 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1538 (when (and (eq (nlx-info-block nlx) block)
1539 (eq (nlx-info-cleanup nlx) cleanup))
1542 (defun nlx-info-lvar (nlx)
1543 (declare (type nlx-info nlx))
1544 (node-lvar (block-last (nlx-info-target nlx))))
1546 ;;;; functional hackery
1548 (declaim (ftype (sfunction (functional) clambda) main-entry))
1549 (defun main-entry (functional)
1550 (etypecase functional
1551 (clambda functional)
1553 (optional-dispatch-main-entry functional))))
1555 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1556 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1557 ;;; optional with null default and no SUPPLIED-P. There must be a
1558 ;;; &REST arg with no references.
1559 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
1560 (defun looks-like-an-mv-bind (functional)
1561 (and (optional-dispatch-p functional)
1562 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1564 (let ((info (lambda-var-arg-info (car arg))))
1565 (unless info (return nil))
1566 (case (arg-info-kind info)
1568 (when (or (arg-info-supplied-p info) (arg-info-default info))
1571 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1575 ;;; Return true if function is an external entry point. This is true
1576 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1577 ;;; (:TOPLEVEL kind.)
1579 (declare (type functional fun))
1580 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1582 ;;; If LVAR's only use is a non-notinline global function reference,
1583 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1584 ;;; is true, then we don't care if the leaf is NOTINLINE.
1585 (defun lvar-fun-name (lvar &optional notinline-ok)
1586 (declare (type lvar lvar))
1587 (let ((use (lvar-uses lvar)))
1589 (let ((leaf (ref-leaf use)))
1590 (if (and (global-var-p leaf)
1591 (eq (global-var-kind leaf) :global-function)
1592 (or (not (defined-fun-p leaf))
1593 (not (eq (defined-fun-inlinep leaf) :notinline))
1595 (leaf-source-name leaf)
1599 ;;; Return the source name of a combination. (This is an idiom
1600 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1601 (defun combination-fun-source-name (combination)
1602 (let ((ref (lvar-uses (combination-fun combination))))
1603 (leaf-source-name (ref-leaf ref))))
1605 ;;; Return the COMBINATION node that is the call to the LET FUN.
1606 (defun let-combination (fun)
1607 (declare (type clambda fun))
1608 (aver (functional-letlike-p fun))
1609 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1611 ;;; Return the initial value lvar for a LET variable, or NIL if there
1613 (defun let-var-initial-value (var)
1614 (declare (type lambda-var var))
1615 (let ((fun (lambda-var-home var)))
1616 (elt (combination-args (let-combination fun))
1617 (position-or-lose var (lambda-vars fun)))))
1619 ;;; Return the LAMBDA that is called by the local CALL.
1620 (defun combination-lambda (call)
1621 (declare (type basic-combination call))
1622 (aver (eq (basic-combination-kind call) :local))
1623 (ref-leaf (lvar-uses (basic-combination-fun call))))
1625 (defvar *inline-expansion-limit* 200
1627 "an upper limit on the number of inline function calls that will be expanded
1628 in any given code object (single function or block compilation)")
1630 ;;; Check whether NODE's component has exceeded its inline expansion
1631 ;;; limit, and warn if so, returning NIL.
1632 (defun inline-expansion-ok (node)
1633 (let ((expanded (incf (component-inline-expansions
1635 (node-block node))))))
1636 (cond ((> expanded *inline-expansion-limit*) nil)
1637 ((= expanded *inline-expansion-limit*)
1638 ;; FIXME: If the objective is to stop the recursive
1639 ;; expansion of inline functions, wouldn't it be more
1640 ;; correct to look back through surrounding expansions
1641 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1642 ;; possibly stored elsewhere too) and suppress expansion
1643 ;; and print this warning when the function being proposed
1644 ;; for inline expansion is found there? (I don't like the
1645 ;; arbitrary numerical limit in principle, and I think
1646 ;; it'll be a nuisance in practice if we ever want the
1647 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1648 ;; arbitrarily huge blocks of code. -- WHN)
1649 (let ((*compiler-error-context* node))
1650 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1651 probably trying to~% ~
1652 inline a recursive function."
1653 *inline-expansion-limit*))
1657 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
1658 (defun assure-functional-live-p (functional)
1659 (declare (type functional functional))
1661 ;; looks LET-converted
1662 (functional-somewhat-letlike-p functional)
1663 ;; It's possible for a LET-converted function to end up
1664 ;; deleted later. In that case, for the purposes of this
1665 ;; analysis, it is LET-converted: LET-converted functionals
1666 ;; are too badly trashed to expand them inline, and deleted
1667 ;; LET-converted functionals are even worse.
1668 (memq (functional-kind functional) '(:deleted :zombie))))
1669 (throw 'locall-already-let-converted functional)))
1671 (defun call-full-like-p (call)
1672 (declare (type combination call))
1673 (let ((kind (basic-combination-kind call)))
1675 (and (eq kind :known)
1676 (let ((info (basic-combination-fun-info call)))
1678 (not (fun-info-ir2-convert info))
1679 (dolist (template (fun-info-templates info) t)
1680 (when (eq (template-ltn-policy template) :fast-safe)
1681 (multiple-value-bind (val win)
1682 (valid-fun-use call (template-type template))
1683 (when (or val (not win)) (return nil)))))))))))
1687 ;;; Apply a function to some arguments, returning a list of the values
1688 ;;; resulting of the evaluation. If an error is signalled during the
1689 ;;; application, then we produce a warning message using WARN-FUN and
1690 ;;; return NIL as our second value to indicate this. NODE is used as
1691 ;;; the error context for any error message, and CONTEXT is a string
1692 ;;; that is spliced into the warning.
1693 (declaim (ftype (sfunction ((or symbol function) list node function string)
1694 (values list boolean))
1696 (defun careful-call (function args node warn-fun context)
1698 (multiple-value-list
1699 (handler-case (apply function args)
1701 (let ((*compiler-error-context* node))
1702 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1703 (return-from careful-call (values nil nil))))))
1706 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1709 ((deffrob (basic careful compiler transform)
1711 (defun ,careful (specifier)
1712 (handler-case (,basic specifier)
1713 (sb!kernel::arg-count-error (condition)
1714 (values nil (list (format nil "~A" condition))))
1715 (simple-error (condition)
1716 (values nil (list* (simple-condition-format-control condition)
1717 (simple-condition-format-arguments condition))))))
1718 (defun ,compiler (specifier)
1719 (multiple-value-bind (type error-args) (,careful specifier)
1721 (apply #'compiler-error error-args))))
1722 (defun ,transform (specifier)
1723 (multiple-value-bind (type error-args) (,careful specifier)
1725 (apply #'give-up-ir1-transform
1727 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1728 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1731 ;;;; utilities used at run-time for parsing &KEY args in IR1
1733 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1734 ;;; the lvar for the value of the &KEY argument KEY in the list of
1735 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
1736 ;;; otherwise. The legality and constantness of the keywords should
1737 ;;; already have been checked.
1738 (declaim (ftype (sfunction (list keyword) (or lvar null))
1740 (defun find-keyword-lvar (args key)
1741 (do ((arg args (cddr arg)))
1743 (when (eq (lvar-value (first arg)) key)
1744 (return (second arg)))))
1746 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1747 ;;; verify that alternating lvars in ARGS are constant and that there
1748 ;;; is an even number of args.
1749 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
1750 (defun check-key-args-constant (args)
1751 (do ((arg args (cddr arg)))
1753 (unless (and (rest arg)
1754 (constant-lvar-p (first arg)))
1757 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1758 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
1759 ;;; and that only keywords present in the list KEYS are supplied.
1760 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
1761 (defun check-transform-keys (args keys)
1762 (and (check-key-args-constant args)
1763 (do ((arg args (cddr arg)))
1765 (unless (member (lvar-value (first arg)) keys)
1770 ;;; Called by the expansion of the EVENT macro.
1771 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
1772 (defun %event (info node)
1773 (incf (event-info-count info))
1774 (when (and (>= (event-info-level info) *event-note-threshold*)
1775 (policy (or node *lexenv*)
1776 (= inhibit-warnings 0)))
1777 (let ((*compiler-error-context* node))
1778 (compiler-notify (event-info-description info))))
1780 (let ((action (event-info-action info)))
1781 (when action (funcall action node))))
1784 (defun make-cast (value type policy)
1785 (declare (type lvar value)
1787 (type policy policy))
1788 (%make-cast :asserted-type type
1789 :type-to-check (maybe-weaken-check type policy)
1791 :derived-type (coerce-to-values type)))
1793 (defun cast-type-check (cast)
1794 (declare (type cast cast))
1795 (when (cast-reoptimize cast)
1796 (ir1-optimize-cast cast t))
1797 (cast-%type-check cast))
1799 (defun note-single-valuified-lvar (lvar)
1800 (declare (type (or lvar null) lvar))
1802 (let ((use (lvar-uses lvar)))
1804 (let ((leaf (ref-leaf use)))
1805 (when (and (lambda-var-p leaf)
1806 (null (rest (leaf-refs leaf))))
1807 (reoptimize-lambda-var leaf))))
1808 ((or (listp use) (combination-p use))
1809 (do-uses (node lvar)
1810 (setf (node-reoptimize node) t)
1811 (setf (block-reoptimize (node-block node)) t)
1812 (reoptimize-component (node-component node) :maybe)))))))