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.
168 (defun substitute-lvar-uses (new old)
169 (declare (type lvar old)
170 (type (or lvar null) new))
172 (cond (new (do-uses (node old)
173 (%delete-lvar-use node)
174 (add-lvar-use node new))
175 (reoptimize-lvar new))
176 (t (flush-dest old)))
179 ;;;; block starting/creation
181 ;;; Return the block that CTRAN is the start of, making a block if
182 ;;; necessary. This function is called by IR1 translators which may
183 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
184 ;;; used more than once must start a block by the time that anyone
185 ;;; does a USE-CTRAN on it.
187 ;;; We also throw the block into the next/prev list for the
188 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
190 (defun ctran-starts-block (ctran)
191 (declare (type ctran ctran))
192 (ecase (ctran-kind ctran)
194 (aver (not (ctran-block ctran)))
195 (let* ((next (component-last-block *current-component*))
196 (prev (block-prev next))
197 (new-block (make-block ctran)))
198 (setf (block-next new-block) next
199 (block-prev new-block) prev
200 (block-prev next) new-block
201 (block-next prev) new-block
202 (ctran-block ctran) new-block
203 (ctran-kind ctran) :block-start)
204 (aver (not (ctran-use ctran)))
207 (ctran-block ctran))))
209 ;;; Ensure that CTRAN is the start of a block so that the use set can
210 ;;; be freely manipulated.
211 (defun ensure-block-start (ctran)
212 (declare (type ctran ctran))
213 (let ((kind (ctran-kind ctran)))
217 (setf (ctran-block ctran)
218 (make-block-key :start ctran))
219 (setf (ctran-kind ctran) :block-start))
221 (node-ends-block (ctran-use ctran)))))
226 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
227 ;;; call. First argument must be 'DUMMY, which will be replaced with
228 ;;; LVAR. In case of an ordinary call the function should not have
229 ;;; return type NIL. We create a new "filtered" lvar.
231 ;;; TODO: remove preconditions.
232 (defun filter-lvar (lvar form)
233 (declare (type lvar lvar) (type list form))
234 (let* ((dest (lvar-dest lvar))
235 (ctran (node-prev dest)))
236 (with-ir1-environment-from-node dest
238 (ensure-block-start ctran)
239 (let* ((old-block (ctran-block ctran))
240 (new-start (make-ctran))
241 (filtered-lvar (make-lvar))
242 (new-block (ctran-starts-block new-start)))
244 ;; Splice in the new block before DEST, giving the new block
245 ;; all of DEST's predecessors.
246 (dolist (block (block-pred old-block))
247 (change-block-successor block old-block new-block))
249 (ir1-convert new-start ctran filtered-lvar form)
251 ;; KLUDGE: Comments at the head of this function in CMU CL
252 ;; said that somewhere in here we
253 ;; Set the new block's start and end cleanups to the *start*
254 ;; cleanup of PREV's block. This overrides the incorrect
255 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
256 ;; Unfortunately I can't find any code which corresponds to this.
257 ;; Perhaps it was a stale comment? Or perhaps I just don't
258 ;; understand.. -- WHN 19990521
260 ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
261 ;; no LET conversion has been done yet.) The [mv-]combination
262 ;; code from the call in the form will be the use of the new
263 ;; check lvar. We substitute for the first argument of
265 (let* ((node (lvar-use filtered-lvar))
266 (args (basic-combination-args node))
267 (victim (first args)))
268 (aver (eq (constant-value (ref-leaf (lvar-use victim)))
271 (substitute-lvar filtered-lvar lvar)
272 (substitute-lvar lvar victim)
275 ;; Invoking local call analysis converts this call to a LET.
276 (locall-analyze-component *current-component*))))
279 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
280 (defun delete-filter (node lvar value)
281 (aver (eq (lvar-dest value) node))
282 (aver (eq (node-lvar node) lvar))
283 (cond (lvar (collect ((merges))
284 (when (return-p (lvar-dest lvar))
286 (when (and (basic-combination-p use)
287 (eq (basic-combination-kind use) :local))
289 (%delete-lvar-use node)
290 (substitute-lvar-uses lvar value)
293 (dolist (merge (merges))
294 (merge-tail-sets merge)))))
295 (t (flush-dest value)
296 (unlink-node node))))
298 ;;;; miscellaneous shorthand functions
300 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
301 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
302 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
303 ;;; deleted, and then return its home.
304 (defun node-home-lambda (node)
305 (declare (type node node))
306 (do ((fun (lexenv-lambda (node-lexenv node))
307 (lexenv-lambda (lambda-call-lexenv fun))))
308 ((not (eq (functional-kind fun) :deleted))
310 (when (eq (lambda-home fun) fun)
313 #!-sb-fluid (declaim (inline node-block))
314 (defun node-block (node)
315 (ctran-block (node-prev node)))
316 (declaim (ftype (sfunction (node) component) node-component))
317 (defun node-component (node)
318 (block-component (node-block node)))
319 (declaim (ftype (sfunction (node) physenv) node-physenv))
320 (defun node-physenv (node)
321 (lambda-physenv (node-home-lambda node)))
322 #!-sb-fluid (declaim (inline node-dest))
323 (defun node-dest (node)
324 (awhen (node-lvar node) (lvar-dest it)))
326 ;;; Checks whether NODE is in a block to be deleted
327 (declaim (inline node-to-be-deleted-p))
328 (defun node-to-be-deleted-p (node)
329 (let ((block (node-block node)))
330 (or (block-delete-p block)
331 (eq (functional-kind (block-home-lambda block)) :deleted))))
333 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
334 (defun lambda-block (clambda)
335 (node-block (lambda-bind clambda)))
336 (declaim (ftype (sfunction (clambda) component) lambda-component))
337 (defun lambda-component (clambda)
338 (block-component (lambda-block clambda)))
340 (declaim (ftype (sfunction (cblock) node) block-start-node))
341 (defun block-start-node (block)
342 (ctran-next (block-start block)))
344 ;;; Return the enclosing cleanup for environment of the first or last
346 (defun block-start-cleanup (block)
347 (node-enclosing-cleanup (block-start-node block)))
348 (defun block-end-cleanup (block)
349 (node-enclosing-cleanup (block-last block)))
351 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
352 ;;; if there is none.
354 ;;; There can legitimately be no home lambda in dead code early in the
355 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
356 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
357 ;;; where the block is just a placeholder during parsing and doesn't
358 ;;; actually correspond to code which will be written anywhere.
359 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
360 (defun block-home-lambda-or-null (block)
361 (if (node-p (block-last block))
362 ;; This is the old CMU CL way of doing it.
363 (node-home-lambda (block-last block))
364 ;; Now that SBCL uses this operation more aggressively than CMU
365 ;; CL did, the old CMU CL way of doing it can fail in two ways.
366 ;; 1. It can fail in a few cases even when a meaningful home
367 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
369 ;; 2. It can fail when converting a form which is born orphaned
370 ;; so that it never had a meaningful home lambda, e.g. a form
371 ;; which follows a RETURN-FROM or GO form.
372 (let ((pred-list (block-pred block)))
373 ;; To deal with case 1, we reason that
374 ;; previous-in-target-execution-order blocks should be in the
375 ;; same lambda, and that they seem in practice to be
376 ;; previous-in-compilation-order blocks too, so we look back
377 ;; to find one which is sufficiently initialized to tell us
378 ;; what the home lambda is.
380 ;; We could get fancy about this, flooding through the
381 ;; graph of all the previous blocks, but in practice it
382 ;; seems to work just to grab the first previous block and
384 (node-home-lambda (block-last (first pred-list)))
385 ;; In case 2, we end up with an empty PRED-LIST and
386 ;; have to punt: There's no home lambda.
389 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
390 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
391 (defun block-home-lambda (block)
392 (block-home-lambda-or-null block))
394 ;;; Return the IR1 physical environment for BLOCK.
395 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
396 (defun block-physenv (block)
397 (lambda-physenv (block-home-lambda block)))
399 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
400 ;;; of its original source's top level form in its compilation unit.
401 (defun source-path-tlf-number (path)
402 (declare (list path))
405 ;;; Return the (reversed) list for the PATH in the original source
406 ;;; (with the Top Level Form number last).
407 (defun source-path-original-source (path)
408 (declare (list path) (inline member))
409 (cddr (member 'original-source-start path :test #'eq)))
411 ;;; Return the Form Number of PATH's original source inside the Top
412 ;;; Level Form that contains it. This is determined by the order that
413 ;;; we walk the subforms of the top level source form.
414 (defun source-path-form-number (path)
415 (declare (list path) (inline member))
416 (cadr (member 'original-source-start path :test #'eq)))
418 ;;; Return a list of all the enclosing forms not in the original
419 ;;; source that converted to get to this form, with the immediate
420 ;;; source for node at the start of the list.
421 (defun source-path-forms (path)
422 (subseq path 0 (position 'original-source-start path)))
424 ;;; Return the innermost source form for NODE.
425 (defun node-source-form (node)
426 (declare (type node node))
427 (let* ((path (node-source-path node))
428 (forms (source-path-forms path)))
431 (values (find-original-source path)))))
433 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
435 (defun lvar-source (lvar)
436 (let ((use (lvar-uses lvar)))
439 (values (node-source-form use) t))))
441 ;;; Return the unique node, delivering a value to LVAR.
442 #!-sb-fluid (declaim (inline lvar-use))
443 (defun lvar-use (lvar)
444 (the (not list) (lvar-uses lvar)))
446 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
447 (defun lvar-has-single-use-p (lvar)
448 (typep (lvar-uses lvar) '(not list)))
450 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
451 (declaim (ftype (sfunction (ctran) (or clambda null))
452 ctran-home-lambda-or-null))
453 (defun ctran-home-lambda-or-null (ctran)
454 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
455 ;; implementation might not be quite right, or might be uglier than
456 ;; necessary. It appears that the original Python never found a need
457 ;; to do this operation. The obvious things based on
458 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
459 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
460 ;; generalize it enough to grovel harder when the simple CMU CL
461 ;; approach fails, and furthermore realize that in some exceptional
462 ;; cases it might return NIL. -- WHN 2001-12-04
463 (cond ((ctran-use ctran)
464 (node-home-lambda (ctran-use ctran)))
466 (block-home-lambda-or-null (ctran-block ctran)))
468 (bug "confused about home lambda for ~S" ctran))))
470 ;;; Return the LAMBDA that is CTRAN's home.
471 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
472 (defun ctran-home-lambda (ctran)
473 (ctran-home-lambda-or-null ctran))
475 #!-sb-fluid (declaim (inline lvar-single-value-p))
476 (defun lvar-single-value-p (lvar)
478 (let ((dest (lvar-dest lvar)))
483 (eq (basic-combination-fun dest) lvar))
486 (declare (notinline lvar-single-value-p))
487 (and (not (values-type-p (cast-asserted-type dest)))
488 (lvar-single-value-p (node-lvar dest)))))
492 (defun principal-lvar-end (lvar)
493 (loop for prev = lvar then (node-lvar dest)
494 for dest = (and prev (lvar-dest prev))
496 finally (return (values dest prev))))
498 (defun principal-lvar-single-valuify (lvar)
499 (loop for prev = lvar then (node-lvar dest)
500 for dest = (and prev (lvar-dest prev))
502 do (setf (node-derived-type dest)
503 (make-short-values-type (list (single-value-type
504 (node-derived-type dest)))))
505 (reoptimize-lvar prev)))
507 ;;; Return a new LEXENV just like DEFAULT except for the specified
508 ;;; slot values. Values for the alist slots are NCONCed to the
509 ;;; beginning of the current value, rather than replacing it entirely.
510 (defun make-lexenv (&key (default *lexenv*)
511 funs vars blocks tags
513 (lambda (lexenv-lambda default))
514 (cleanup (lexenv-cleanup default))
515 (policy (lexenv-policy default)))
516 (macrolet ((frob (var slot)
517 `(let ((old (,slot default)))
521 (internal-make-lexenv
522 (frob funs lexenv-funs)
523 (frob vars lexenv-vars)
524 (frob blocks lexenv-blocks)
525 (frob tags lexenv-tags)
526 (frob type-restrictions lexenv-type-restrictions)
527 lambda cleanup policy)))
529 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
531 (defun make-restricted-lexenv (lexenv)
532 (flet ((fun-good-p (fun)
533 (destructuring-bind (name . thing) fun
534 (declare (ignore name))
538 (cons (aver (eq (car thing) 'macro))
541 (destructuring-bind (name . thing) var
542 (declare (ignore name))
545 (cons (aver (eq (car thing) 'macro))
547 (heap-alien-info nil)))))
548 (internal-make-lexenv
549 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
550 (remove-if-not #'var-good-p (lexenv-vars lexenv))
553 (lexenv-type-restrictions lexenv) ; XXX
556 (lexenv-policy lexenv))))
558 ;;;; flow/DFO/component hackery
560 ;;; Join BLOCK1 and BLOCK2.
561 (defun link-blocks (block1 block2)
562 (declare (type cblock block1 block2))
563 (setf (block-succ block1)
564 (if (block-succ block1)
565 (%link-blocks block1 block2)
567 (push block1 (block-pred block2))
569 (defun %link-blocks (block1 block2)
570 (declare (type cblock block1 block2))
571 (let ((succ1 (block-succ block1)))
572 (aver (not (memq block2 succ1)))
573 (cons block2 succ1)))
575 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
576 ;;; this leaves a successor with a single predecessor that ends in an
577 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
578 ;;; now be able to be propagated to the successor.
579 (defun unlink-blocks (block1 block2)
580 (declare (type cblock block1 block2))
581 (let ((succ1 (block-succ block1)))
582 (if (eq block2 (car succ1))
583 (setf (block-succ block1) (cdr succ1))
584 (do ((succ (cdr succ1) (cdr succ))
586 ((eq (car succ) block2)
587 (setf (cdr prev) (cdr succ)))
590 (let ((new-pred (delq block1 (block-pred block2))))
591 (setf (block-pred block2) new-pred)
592 (when (singleton-p new-pred)
593 (let ((pred-block (first new-pred)))
594 (when (if-p (block-last pred-block))
595 (setf (block-test-modified pred-block) t)))))
598 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
599 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
600 ;;; consequent/alternative blocks to point to NEW. We also set
601 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
602 ;;; the new successor.
603 (defun change-block-successor (block old new)
604 (declare (type cblock new old block))
605 (unlink-blocks block old)
606 (let ((last (block-last block))
607 (comp (block-component block)))
608 (setf (component-reanalyze comp) t)
611 (setf (block-test-modified block) t)
612 (let* ((succ-left (block-succ block))
613 (new (if (and (eq new (component-tail comp))
617 (unless (memq new succ-left)
618 (link-blocks block new))
619 (macrolet ((frob (slot)
620 `(when (eq (,slot last) old)
621 (setf (,slot last) new))))
623 (frob if-alternative)
624 (when (eq (if-consequent last)
625 (if-alternative last))
626 (setf (component-reoptimize (block-component block)) t)))))
628 (unless (memq new (block-succ block))
629 (link-blocks block new)))))
633 ;;; Unlink a block from the next/prev chain. We also null out the
635 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
636 (defun remove-from-dfo (block)
637 (let ((next (block-next block))
638 (prev (block-prev block)))
639 (setf (block-component block) nil)
640 (setf (block-next prev) next)
641 (setf (block-prev next) prev))
644 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
645 ;;; COMPONENT to be the same as for AFTER.
646 (defun add-to-dfo (block after)
647 (declare (type cblock block after))
648 (let ((next (block-next after))
649 (comp (block-component after)))
650 (aver (not (eq (component-kind comp) :deleted)))
651 (setf (block-component block) comp)
652 (setf (block-next after) block)
653 (setf (block-prev block) after)
654 (setf (block-next block) next)
655 (setf (block-prev next) block))
658 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
659 ;;; the head and tail which are set to T.
660 (declaim (ftype (sfunction (component) (values)) clear-flags))
661 (defun clear-flags (component)
662 (let ((head (component-head component))
663 (tail (component-tail component)))
664 (setf (block-flag head) t)
665 (setf (block-flag tail) t)
666 (do-blocks (block component)
667 (setf (block-flag block) nil)))
670 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
671 ;;; true in the head and tail blocks.
672 (declaim (ftype (sfunction () component) make-empty-component))
673 (defun make-empty-component ()
674 (let* ((head (make-block-key :start nil :component nil))
675 (tail (make-block-key :start nil :component nil))
676 (res (make-component head tail)))
677 (setf (block-flag head) t)
678 (setf (block-flag tail) t)
679 (setf (block-component head) res)
680 (setf (block-component tail) res)
681 (setf (block-next head) tail)
682 (setf (block-prev tail) head)
685 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
686 ;;; The new block is added to the DFO immediately following NODE's block.
687 (defun node-ends-block (node)
688 (declare (type node node))
689 (let* ((block (node-block node))
690 (start (node-next node))
691 (last (block-last block)))
692 (unless (eq last node)
693 (aver (and (eq (ctran-kind start) :inside-block)
694 (not (block-delete-p block))))
695 (let* ((succ (block-succ block))
697 (make-block-key :start start
698 :component (block-component block)
699 :succ succ :last last)))
700 (setf (ctran-kind start) :block-start)
701 (setf (ctran-use start) nil)
702 (setf (block-last block) node)
703 (setf (node-next node) nil)
706 (cons new-block (remove block (block-pred b)))))
707 (setf (block-succ block) ())
708 (link-blocks block new-block)
709 (add-to-dfo new-block block)
710 (setf (component-reanalyze (block-component block)) t)
712 (do ((ctran start (node-next (ctran-next ctran))))
714 (setf (ctran-block ctran) new-block))
716 (setf (block-type-asserted block) t)
717 (setf (block-test-modified block) t))))
722 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
723 (defun delete-lambda-var (leaf)
724 (declare (type lambda-var leaf))
726 ;; Iterate over all local calls flushing the corresponding argument,
727 ;; allowing the computation of the argument to be deleted. We also
728 ;; mark the LET for reoptimization, since it may be that we have
729 ;; deleted its last variable.
730 (let* ((fun (lambda-var-home leaf))
731 (n (position leaf (lambda-vars fun))))
732 (dolist (ref (leaf-refs fun))
733 (let* ((lvar (node-lvar ref))
734 (dest (and lvar (lvar-dest lvar))))
735 (when (and (combination-p dest)
736 (eq (basic-combination-fun dest) lvar)
737 (eq (basic-combination-kind dest) :local))
738 (let* ((args (basic-combination-args dest))
740 (reoptimize-lvar arg)
742 (setf (elt args n) nil))))))
744 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
745 ;; too much difficulty, since we can efficiently implement
746 ;; write-only variables. We iterate over the SETs, marking their
747 ;; blocks for dead code flushing, since we can delete SETs whose
749 (dolist (set (lambda-var-sets leaf))
750 (setf (block-flush-p (node-block set)) t))
754 ;;; Note that something interesting has happened to VAR.
755 (defun reoptimize-lambda-var (var)
756 (declare (type lambda-var var))
757 (let ((fun (lambda-var-home var)))
758 ;; We only deal with LET variables, marking the corresponding
759 ;; initial value arg as needing to be reoptimized.
760 (when (and (eq (functional-kind fun) :let)
762 (do ((args (basic-combination-args
763 (lvar-dest (node-lvar (first (leaf-refs fun)))))
765 (vars (lambda-vars fun) (cdr vars)))
767 (reoptimize-lvar (car args))))))
770 ;;; Delete a function that has no references. This need only be called
771 ;;; on functions that never had any references, since otherwise
772 ;;; DELETE-REF will handle the deletion.
773 (defun delete-functional (fun)
774 (aver (and (null (leaf-refs fun))
775 (not (functional-entry-fun fun))))
777 (optional-dispatch (delete-optional-dispatch fun))
778 (clambda (delete-lambda fun)))
781 ;;; Deal with deleting the last reference to a CLAMBDA. It is called
782 ;;; in two situations: when the lambda is unreachable (so that its
783 ;;; body may be deleted), and when it is an effectless LET (in this
784 ;;; case its body is reachable and is not completely "its"). We set
785 ;;; FUNCTIONAL-KIND to :DELETED and rely on IR1-OPTIMIZE to delete its
787 (defun delete-lambda (clambda)
788 (declare (type clambda clambda))
789 (let ((original-kind (functional-kind clambda))
790 (bind (lambda-bind clambda)))
791 (aver (not (member original-kind '(:deleted :toplevel))))
792 (aver (not (functional-has-external-references-p clambda)))
793 (setf (functional-kind clambda) :deleted)
794 (setf (lambda-bind clambda) nil)
796 (when bind ; CLAMBDA is deleted due to unreachability
797 (labels ((delete-children (lambda)
798 (dolist (child (lambda-children lambda))
799 (cond ((eq (functional-kind child) :deleted)
800 (delete-children child))
802 (delete-lambda child))))
803 (setf (lambda-children lambda) nil)
804 (setf (lambda-parent lambda) nil)))
805 (delete-children clambda)))
806 (dolist (let (lambda-lets clambda))
807 (setf (lambda-bind let) nil)
808 (setf (functional-kind let) :deleted))
810 ;; LET may be deleted if its BIND is unreachable. Autonomous
811 ;; function may be deleted if it has no reachable references.
812 (unless (member original-kind '(:let :mv-let :assignment))
813 (dolist (ref (lambda-refs clambda))
814 (mark-for-deletion (node-block ref))))
816 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
817 ;; that we're using the old value of the KIND slot, not the
818 ;; current slot value, which has now been set to :DELETED.)
819 (if (member original-kind '(:let :mv-let :assignment))
820 (let ((home (lambda-home clambda)))
821 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
822 ;; If the function isn't a LET, we unlink the function head
823 ;; and tail from the component head and tail to indicate that
824 ;; the code is unreachable. We also delete the function from
825 ;; COMPONENT-LAMBDAS (it won't be there before local call
826 ;; analysis, but no matter.) If the lambda was never
827 ;; referenced, we give a note.
828 (let* ((bind-block (node-block bind))
829 (component (block-component bind-block))
830 (return (lambda-return clambda))
831 (return-block (and return (node-block return))))
832 (unless (leaf-ever-used clambda)
833 (let ((*compiler-error-context* bind))
834 (compiler-notify 'code-deletion-note
835 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
836 :format-arguments (list (leaf-debug-name clambda)))))
837 (unless (block-delete-p bind-block)
838 (unlink-blocks (component-head component) bind-block))
839 (when (and return-block (not (block-delete-p return-block)))
840 (mark-for-deletion return-block)
841 (unlink-blocks return-block (component-tail component)))
842 (setf (component-reanalyze component) t)
843 (let ((tails (lambda-tail-set clambda)))
844 (setf (tail-set-funs tails)
845 (delete clambda (tail-set-funs tails)))
846 (setf (lambda-tail-set clambda) nil))
847 (setf (component-lambdas component)
848 (delq clambda (component-lambdas component)))))
850 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
851 ;; ENTRY-FUN so that people will know that it is not an entry
853 (when (eq original-kind :external)
854 (let ((fun (functional-entry-fun clambda)))
855 (setf (functional-entry-fun fun) nil)
856 (when (optional-dispatch-p fun)
857 (delete-optional-dispatch fun)))))
861 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
862 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
863 ;;; is used both before and after local call analysis. Afterward, all
864 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
865 ;;; to the XEP, leaving it with no references at all. So we look at
866 ;;; the XEP to see whether an optional-dispatch is still really being
867 ;;; used. But before local call analysis, there are no XEPs, and all
868 ;;; references are direct.
870 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
871 ;;; entry-points, making them be normal lambdas, and then deleting the
872 ;;; ones with no references. This deletes any e-p lambdas that were
873 ;;; either never referenced, or couldn't be deleted when the last
874 ;;; reference was deleted (due to their :OPTIONAL kind.)
876 ;;; Note that the last optional entry point may alias the main entry,
877 ;;; so when we process the main entry, its KIND may have been changed
878 ;;; to NIL or even converted to a LETlike value.
879 (defun delete-optional-dispatch (leaf)
880 (declare (type optional-dispatch leaf))
881 (let ((entry (functional-entry-fun leaf)))
882 (unless (and entry (leaf-refs entry))
883 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
884 (setf (functional-kind leaf) :deleted)
887 (unless (eq (functional-kind fun) :deleted)
888 (aver (eq (functional-kind fun) :optional))
889 (setf (functional-kind fun) nil)
890 (let ((refs (leaf-refs fun)))
894 (or (maybe-let-convert fun)
895 (maybe-convert-to-assignment fun)))
897 (maybe-convert-to-assignment fun)))))))
899 (dolist (ep (optional-dispatch-entry-points leaf))
900 (when (promise-ready-p ep)
902 (when (optional-dispatch-more-entry leaf)
903 (frob (optional-dispatch-more-entry leaf)))
904 (let ((main (optional-dispatch-main-entry leaf)))
905 (when (eq (functional-kind main) :optional)
910 ;;; Do stuff to delete the semantic attachments of a REF node. When
911 ;;; this leaves zero or one reference, we do a type dispatch off of
912 ;;; the leaf to determine if a special action is appropriate.
913 (defun delete-ref (ref)
914 (declare (type ref ref))
915 (let* ((leaf (ref-leaf ref))
916 (refs (delq ref (leaf-refs leaf))))
917 (setf (leaf-refs leaf) refs)
922 (delete-lambda-var leaf))
924 (ecase (functional-kind leaf)
925 ((nil :let :mv-let :assignment :escape :cleanup)
926 (aver (null (functional-entry-fun leaf)))
927 (delete-lambda leaf))
929 (delete-lambda leaf))
930 ((:deleted :optional))))
932 (unless (eq (functional-kind leaf) :deleted)
933 (delete-optional-dispatch leaf)))))
936 (clambda (or (maybe-let-convert leaf)
937 (maybe-convert-to-assignment leaf)))
938 (lambda-var (reoptimize-lambda-var leaf))))
941 (clambda (maybe-convert-to-assignment leaf))))))
945 ;;; This function is called by people who delete nodes; it provides a
946 ;;; way to indicate that the value of a lvar is no longer used. We
947 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
948 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
949 (defun flush-dest (lvar)
950 (declare (type (or lvar null) lvar))
952 (setf (lvar-dest lvar) nil)
953 (flush-lvar-externally-checkable-type lvar)
955 (let ((prev (node-prev use)))
956 (let ((block (ctran-block prev)))
957 (setf (component-reoptimize (block-component block)) t)
958 (setf (block-attributep (block-flags block)
959 flush-p type-asserted type-check)
961 (setf (node-lvar use) nil))
962 (setf (lvar-uses lvar) nil))
965 (defun delete-dest (lvar)
967 (let* ((dest (lvar-dest lvar))
968 (prev (node-prev dest)))
969 (let ((block (ctran-block prev)))
970 (unless (block-delete-p block)
971 (mark-for-deletion block))))))
973 ;;; Queue the block for deletion
974 (defun delete-block-lazily (block)
975 (declare (type cblock block))
976 (unless (block-delete-p block)
977 (setf (block-delete-p block) t)
978 (push block (component-delete-blocks (block-component block)))))
980 ;;; Do a graph walk backward from BLOCK, marking all predecessor
981 ;;; blocks with the DELETE-P flag.
982 (defun mark-for-deletion (block)
983 (declare (type cblock block))
984 (let* ((component (block-component block))
985 (head (component-head component)))
986 (labels ((helper (block)
987 (delete-block-lazily block)
988 (dolist (pred (block-pred block))
989 (unless (or (block-delete-p pred)
992 (unless (block-delete-p block)
994 (setf (component-reanalyze component) t))))
997 ;;; This function does what is necessary to eliminate the code in it
998 ;;; from the IR1 representation. This involves unlinking it from its
999 ;;; predecessors and successors and deleting various node-specific
1000 ;;; semantic information. BLOCK must be already removed from
1001 ;;; COMPONENT-DELETE-BLOCKS.
1002 (defun delete-block (block &optional silent)
1003 (declare (type cblock block))
1004 (aver (block-component block)) ; else block is already deleted!
1005 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1007 (note-block-deletion block))
1008 (setf (block-delete-p block) t)
1010 (dolist (b (block-pred block))
1011 (unlink-blocks b block)
1012 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1013 ;; broken when successors were deleted without setting the
1014 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1015 ;; doesn't happen again.
1016 (aver (not (and (null (block-succ b))
1017 (not (block-delete-p b))
1018 (not (eq b (component-head (block-component b))))))))
1019 (dolist (b (block-succ block))
1020 (unlink-blocks block b))
1022 (do-nodes-carefully (node block)
1023 (when (valued-node-p node)
1024 (delete-lvar-use node))
1026 (ref (delete-ref node))
1027 (cif (flush-dest (if-test node)))
1028 ;; The next two cases serve to maintain the invariant that a LET
1029 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1030 ;; the lambda whenever we delete any of these, but we must be
1031 ;; careful that this LET has not already been partially deleted.
1033 (when (and (eq (basic-combination-kind node) :local)
1034 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1035 (lvar-uses (basic-combination-fun node)))
1036 (let ((fun (combination-lambda node)))
1037 ;; If our REF was the second-to-last ref, and has been
1038 ;; deleted, then FUN may be a LET for some other
1040 (when (and (functional-letlike-p fun)
1041 (eq (let-combination fun) node))
1042 (delete-lambda fun))))
1043 (flush-dest (basic-combination-fun node))
1044 (dolist (arg (basic-combination-args node))
1045 (when arg (flush-dest arg))))
1047 (let ((lambda (bind-lambda node)))
1048 (unless (eq (functional-kind lambda) :deleted)
1049 (delete-lambda lambda))))
1051 (let ((value (exit-value node))
1052 (entry (exit-entry node)))
1056 (setf (entry-exits entry)
1057 (delq node (entry-exits entry))))))
1059 (dolist (exit (entry-exits node))
1060 (mark-for-deletion (node-block exit)))
1061 (let ((home (node-home-lambda node)))
1062 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1064 (flush-dest (return-result node))
1065 (delete-return node))
1067 (flush-dest (set-value node))
1068 (let ((var (set-var node)))
1069 (setf (basic-var-sets var)
1070 (delete node (basic-var-sets var)))))
1072 (flush-dest (cast-value node)))))
1074 (remove-from-dfo block)
1077 ;;; Do stuff to indicate that the return node NODE is being deleted.
1078 (defun delete-return (node)
1079 (declare (type creturn node))
1080 (let* ((fun (return-lambda node))
1081 (tail-set (lambda-tail-set fun)))
1082 (aver (lambda-return fun))
1083 (setf (lambda-return fun) nil)
1084 (when (and tail-set (not (find-if #'lambda-return
1085 (tail-set-funs tail-set))))
1086 (setf (tail-set-type tail-set) *empty-type*)))
1089 ;;; If any of the VARS in FUN was never referenced and was not
1090 ;;; declared IGNORE, then complain.
1091 (defun note-unreferenced-vars (fun)
1092 (declare (type clambda fun))
1093 (dolist (var (lambda-vars fun))
1094 (unless (or (leaf-ever-used var)
1095 (lambda-var-ignorep var))
1096 (let ((*compiler-error-context* (lambda-bind fun)))
1097 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1098 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1099 ;; requires this to be no more than a STYLE-WARNING.
1100 (compiler-style-warn "The variable ~S is defined but never used."
1101 (leaf-debug-name var)))
1102 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1105 (defvar *deletion-ignored-objects* '(t nil))
1107 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1108 ;;; our recursion so that we don't get lost in circular structures. We
1109 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1110 ;;; function referencess with variables), and we also ignore anything
1112 (defun present-in-form (obj form depth)
1113 (declare (type (integer 0 20) depth))
1114 (cond ((= depth 20) nil)
1118 (let ((first (car form))
1120 (if (member first '(quote function))
1122 (or (and (not (symbolp first))
1123 (present-in-form obj first depth))
1124 (do ((l (cdr form) (cdr l))
1126 ((or (atom l) (> n 100))
1128 (declare (fixnum n))
1129 (when (present-in-form obj (car l) depth)
1132 ;;; This function is called on a block immediately before we delete
1133 ;;; it. We check to see whether any of the code about to die appeared
1134 ;;; in the original source, and emit a note if so.
1136 ;;; If the block was in a lambda is now deleted, then we ignore the
1137 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1138 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1139 ;;; reasonable for a function to not return, and there is a different
1140 ;;; note for that case anyway.
1142 ;;; If the actual source is an atom, then we use a bunch of heuristics
1143 ;;; to guess whether this reference really appeared in the original
1145 ;;; -- If a symbol, it must be interned and not a keyword.
1146 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1147 ;;; or a character.)
1148 ;;; -- The atom must be "present" in the original source form, and
1149 ;;; present in all intervening actual source forms.
1150 (defun note-block-deletion (block)
1151 (let ((home (block-home-lambda block)))
1152 (unless (eq (functional-kind home) :deleted)
1153 (do-nodes (node nil block)
1154 (let* ((path (node-source-path node))
1155 (first (first path)))
1156 (when (or (eq first 'original-source-start)
1158 (or (not (symbolp first))
1159 (let ((pkg (symbol-package first)))
1161 (not (eq pkg (symbol-package :end))))))
1162 (not (member first *deletion-ignored-objects*))
1163 (not (typep first '(or fixnum character)))
1165 (present-in-form first x 0))
1166 (source-path-forms path))
1167 (present-in-form first (find-original-source path)
1169 (unless (return-p node)
1170 (let ((*compiler-error-context* node))
1171 (compiler-notify 'code-deletion-note
1172 :format-control "deleting unreachable code"
1173 :format-arguments nil)))
1177 ;;; Delete a node from a block, deleting the block if there are no
1178 ;;; nodes left. We remove the node from the uses of its LVAR.
1180 ;;; If the node is the last node, there must be exactly one successor.
1181 ;;; We link all of our precedessors to the successor and unlink the
1182 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1183 ;;; left, and the block is a successor of itself, then we replace the
1184 ;;; only node with a degenerate exit node. This provides a way to
1185 ;;; represent the bodyless infinite loop, given the prohibition on
1186 ;;; empty blocks in IR1.
1187 (defun unlink-node (node)
1188 (declare (type node node))
1189 (when (valued-node-p node)
1190 (delete-lvar-use node))
1192 (let* ((ctran (node-next node))
1193 (next (and ctran (ctran-next ctran)))
1194 (prev (node-prev node))
1195 (block (ctran-block prev))
1196 (prev-kind (ctran-kind prev))
1197 (last (block-last block)))
1199 (setf (block-type-asserted block) t)
1200 (setf (block-test-modified block) t)
1202 (cond ((or (eq prev-kind :inside-block)
1203 (and (eq prev-kind :block-start)
1204 (not (eq node last))))
1205 (cond ((eq node last)
1206 (setf (block-last block) (ctran-use prev))
1207 (setf (node-next (ctran-use prev)) nil))
1209 (setf (ctran-next prev) next)
1210 (setf (node-prev next) prev)
1211 (when (if-p next) ; AOP wanted
1212 (reoptimize-lvar (if-test next)))))
1213 (setf (node-prev node) nil)
1216 (aver (eq prev-kind :block-start))
1217 (aver (eq node last))
1218 (let* ((succ (block-succ block))
1219 (next (first succ)))
1220 (aver (singleton-p succ))
1222 ((eq block (first succ))
1223 (with-ir1-environment-from-node node
1224 (let ((exit (make-exit)))
1225 (setf (ctran-next prev) nil)
1226 (link-node-to-previous-ctran exit prev)
1227 (setf (block-last block) exit)))
1228 (setf (node-prev node) nil)
1231 (aver (eq (block-start-cleanup block)
1232 (block-end-cleanup block)))
1233 (unlink-blocks block next)
1234 (dolist (pred (block-pred block))
1235 (change-block-successor pred block next))
1236 (when (block-delete-p block)
1237 (let ((component (block-component block)))
1238 (setf (component-delete-blocks component)
1239 (delq block (component-delete-blocks component)))))
1240 (remove-from-dfo block)
1241 (setf (block-delete-p block) t)
1242 (setf (node-prev node) nil)
1245 ;;; Return true if NODE has been deleted, false if it is still a valid
1247 (defun node-deleted (node)
1248 (declare (type node node))
1249 (let ((prev (node-prev node)))
1251 (let ((block (ctran-block prev)))
1252 (and (block-component block)
1253 (not (block-delete-p block))))))))
1255 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1256 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1257 ;;; triggered by deletion.
1258 (defun delete-component (component)
1259 (declare (type component component))
1260 (aver (null (component-new-functionals component)))
1261 (setf (component-kind component) :deleted)
1262 (do-blocks (block component)
1263 (delete-block-lazily block))
1264 (dolist (fun (component-lambdas component))
1265 (unless (eq (functional-kind fun) :deleted)
1266 (setf (functional-kind fun) nil)
1267 (setf (functional-entry-fun fun) nil)
1268 (setf (leaf-refs fun) nil)
1269 (delete-functional fun)))
1270 (clean-component component)
1273 ;;; Remove all pending blocks to be deleted. Return the nearest live
1274 ;;; block after or equal to BLOCK.
1275 (defun clean-component (component &optional block)
1276 (loop while (component-delete-blocks component)
1277 ;; actual deletion of a block may queue new blocks
1278 do (let ((current (pop (component-delete-blocks component))))
1279 (when (eq block current)
1280 (setq block (block-next block)))
1281 (delete-block current)))
1284 ;;; Convert code of the form
1285 ;;; (FOO ... (FUN ...) ...)
1287 ;;; (FOO ... ... ...).
1288 ;;; In other words, replace the function combination FUN by its
1289 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1290 ;;; to blow out of whatever transform called this. Note, as the number
1291 ;;; of arguments changes, the transform must be prepared to return a
1292 ;;; lambda with a new lambda-list with the correct number of
1294 (defun extract-fun-args (lvar fun num-args)
1296 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments
1297 to feed directly to the LVAR-DEST of LVAR, which must be a
1299 (declare (type lvar lvar)
1301 (type index num-args))
1302 (let ((outside (lvar-dest lvar))
1303 (inside (lvar-uses lvar)))
1304 (aver (combination-p outside))
1305 (unless (combination-p inside)
1306 (give-up-ir1-transform))
1307 (let ((inside-fun (combination-fun inside)))
1308 (unless (eq (lvar-fun-name inside-fun) fun)
1309 (give-up-ir1-transform))
1310 (let ((inside-args (combination-args inside)))
1311 (unless (= (length inside-args) num-args)
1312 (give-up-ir1-transform))
1313 (let* ((outside-args (combination-args outside))
1314 (arg-position (position lvar outside-args))
1315 (before-args (subseq outside-args 0 arg-position))
1316 (after-args (subseq outside-args (1+ arg-position))))
1317 (dolist (arg inside-args)
1318 (setf (lvar-dest arg) outside)
1319 (flush-lvar-externally-checkable-type arg))
1320 (setf (combination-args inside) nil)
1321 (setf (combination-args outside)
1322 (append before-args inside-args after-args))
1323 (change-ref-leaf (lvar-uses inside-fun)
1324 (find-free-fun 'list "???"))
1325 (setf (combination-kind inside)
1326 (info :function :info 'list))
1327 (setf (node-derived-type inside) *wild-type*)
1331 (defun flush-combination (combination)
1332 (declare (type combination combination))
1333 (flush-dest (combination-fun combination))
1334 (dolist (arg (combination-args combination))
1336 (unlink-node combination)
1342 ;;; Change the LEAF that a REF refers to.
1343 (defun change-ref-leaf (ref leaf)
1344 (declare (type ref ref) (type leaf leaf))
1345 (unless (eq (ref-leaf ref) leaf)
1346 (push ref (leaf-refs leaf))
1348 (setf (ref-leaf ref) leaf)
1349 (setf (leaf-ever-used leaf) t)
1350 (let* ((ltype (leaf-type leaf))
1351 (vltype (make-single-value-type ltype)))
1352 (if (let* ((lvar (node-lvar ref))
1353 (dest (and lvar (lvar-dest lvar))))
1354 (and (basic-combination-p dest)
1355 (eq lvar (basic-combination-fun dest))
1356 (csubtypep ltype (specifier-type 'function))))
1357 (setf (node-derived-type ref) vltype)
1358 (derive-node-type ref vltype)))
1359 (reoptimize-lvar (node-lvar ref)))
1362 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1363 (defun substitute-leaf (new-leaf old-leaf)
1364 (declare (type leaf new-leaf old-leaf))
1365 (dolist (ref (leaf-refs old-leaf))
1366 (change-ref-leaf ref new-leaf))
1369 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1370 ;;; whether to substitute
1371 (defun substitute-leaf-if (test new-leaf old-leaf)
1372 (declare (type leaf new-leaf old-leaf) (type function test))
1373 (dolist (ref (leaf-refs old-leaf))
1374 (when (funcall test ref)
1375 (change-ref-leaf ref new-leaf)))
1378 ;;; Return a LEAF which represents the specified constant object. If
1379 ;;; the object is not in *CONSTANTS*, then we create a new constant
1380 ;;; LEAF and enter it.
1381 (defun find-constant (object)
1383 ;; FIXME: What is the significance of this test? ("things
1384 ;; that are worth uniquifying"?)
1385 '(or symbol number character instance))
1386 (or (gethash object *constants*)
1387 (setf (gethash object *constants*)
1388 (make-constant :value object
1389 :%source-name '.anonymous.
1390 :type (ctype-of object)
1391 :where-from :defined)))
1392 (make-constant :value object
1393 :%source-name '.anonymous.
1394 :type (ctype-of object)
1395 :where-from :defined)))
1397 ;;; Return true if VAR would have to be closed over if environment
1398 ;;; analysis ran now (i.e. if there are any uses that have a different
1399 ;;; home lambda than VAR's home.)
1400 (defun closure-var-p (var)
1401 (declare (type lambda-var var))
1402 (let ((home (lambda-var-home var)))
1403 (cond ((eq (functional-kind home) :deleted)
1405 (t (let ((home (lambda-home home)))
1408 :key #'node-home-lambda
1410 (or (frob (leaf-refs var))
1411 (frob (basic-var-sets var)))))))))
1413 ;;; If there is a non-local exit noted in ENTRY's environment that
1414 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1415 (defun find-nlx-info (exit)
1416 (declare (type exit exit))
1417 (let* ((entry (exit-entry exit))
1418 (entry-cleanup (entry-cleanup entry)))
1419 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1420 (when (eq (nlx-info-exit nlx) exit)
1423 ;;;; functional hackery
1425 (declaim (ftype (sfunction (functional) clambda) main-entry))
1426 (defun main-entry (functional)
1427 (etypecase functional
1428 (clambda functional)
1430 (optional-dispatch-main-entry functional))))
1432 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1433 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1434 ;;; optional with null default and no SUPPLIED-P. There must be a
1435 ;;; &REST arg with no references.
1436 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
1437 (defun looks-like-an-mv-bind (functional)
1438 (and (optional-dispatch-p functional)
1439 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1441 (let ((info (lambda-var-arg-info (car arg))))
1442 (unless info (return nil))
1443 (case (arg-info-kind info)
1445 (when (or (arg-info-supplied-p info) (arg-info-default info))
1448 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1452 ;;; Return true if function is an external entry point. This is true
1453 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1454 ;;; (:TOPLEVEL kind.)
1456 (declare (type functional fun))
1457 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1459 ;;; If LVAR's only use is a non-notinline global function reference,
1460 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1461 ;;; is true, then we don't care if the leaf is NOTINLINE.
1462 (defun lvar-fun-name (lvar &optional notinline-ok)
1463 (declare (type lvar lvar))
1464 (let ((use (lvar-uses lvar)))
1466 (let ((leaf (ref-leaf use)))
1467 (if (and (global-var-p leaf)
1468 (eq (global-var-kind leaf) :global-function)
1469 (or (not (defined-fun-p leaf))
1470 (not (eq (defined-fun-inlinep leaf) :notinline))
1472 (leaf-source-name leaf)
1476 ;;; Return the source name of a combination. (This is an idiom
1477 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1478 (defun combination-fun-source-name (combination)
1479 (let ((ref (lvar-uses (combination-fun combination))))
1480 (leaf-source-name (ref-leaf ref))))
1482 ;;; Return the COMBINATION node that is the call to the LET FUN.
1483 (defun let-combination (fun)
1484 (declare (type clambda fun))
1485 (aver (functional-letlike-p fun))
1486 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1488 ;;; Return the initial value lvar for a LET variable, or NIL if there
1490 (defun let-var-initial-value (var)
1491 (declare (type lambda-var var))
1492 (let ((fun (lambda-var-home var)))
1493 (elt (combination-args (let-combination fun))
1494 (position-or-lose var (lambda-vars fun)))))
1496 ;;; Return the LAMBDA that is called by the local CALL.
1497 (defun combination-lambda (call)
1498 (declare (type basic-combination call))
1499 (aver (eq (basic-combination-kind call) :local))
1500 (ref-leaf (lvar-uses (basic-combination-fun call))))
1502 (defvar *inline-expansion-limit* 200
1504 "an upper limit on the number of inline function calls that will be expanded
1505 in any given code object (single function or block compilation)")
1507 ;;; Check whether NODE's component has exceeded its inline expansion
1508 ;;; limit, and warn if so, returning NIL.
1509 (defun inline-expansion-ok (node)
1510 (let ((expanded (incf (component-inline-expansions
1512 (node-block node))))))
1513 (cond ((> expanded *inline-expansion-limit*) nil)
1514 ((= expanded *inline-expansion-limit*)
1515 ;; FIXME: If the objective is to stop the recursive
1516 ;; expansion of inline functions, wouldn't it be more
1517 ;; correct to look back through surrounding expansions
1518 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1519 ;; possibly stored elsewhere too) and suppress expansion
1520 ;; and print this warning when the function being proposed
1521 ;; for inline expansion is found there? (I don't like the
1522 ;; arbitrary numerical limit in principle, and I think
1523 ;; it'll be a nuisance in practice if we ever want the
1524 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1525 ;; arbitrarily huge blocks of code. -- WHN)
1526 (let ((*compiler-error-context* node))
1527 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1528 probably trying to~% ~
1529 inline a recursive function."
1530 *inline-expansion-limit*))
1534 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
1535 (defun assure-functional-live-p (functional)
1536 (declare (type functional functional))
1538 ;; looks LET-converted
1539 (functional-somewhat-letlike-p functional)
1540 ;; It's possible for a LET-converted function to end up
1541 ;; deleted later. In that case, for the purposes of this
1542 ;; analysis, it is LET-converted: LET-converted functionals
1543 ;; are too badly trashed to expand them inline, and deleted
1544 ;; LET-converted functionals are even worse.
1545 (eql (functional-kind functional) :deleted)))
1546 (throw 'locall-already-let-converted functional)))
1548 (defun call-full-like-p (call)
1549 (declare (type combination call))
1550 (let ((kind (basic-combination-kind call)))
1552 (and (fun-info-p kind)
1553 (not (fun-info-ir2-convert kind))
1554 (dolist (template (fun-info-templates kind) t)
1555 (when (eq (template-ltn-policy template) :fast-safe)
1556 (multiple-value-bind (val win)
1557 (valid-fun-use call (template-type template))
1558 (when (or val (not win)) (return nil)))))))))
1562 ;;; Apply a function to some arguments, returning a list of the values
1563 ;;; resulting of the evaluation. If an error is signalled during the
1564 ;;; application, then we produce a warning message using WARN-FUN and
1565 ;;; return NIL as our second value to indicate this. NODE is used as
1566 ;;; the error context for any error message, and CONTEXT is a string
1567 ;;; that is spliced into the warning.
1568 (declaim (ftype (sfunction ((or symbol function) list node function string)
1569 (values list boolean))
1571 (defun careful-call (function args node warn-fun context)
1573 (multiple-value-list
1574 (handler-case (apply function args)
1576 (let ((*compiler-error-context* node))
1577 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1578 (return-from careful-call (values nil nil))))))
1581 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1584 ((deffrob (basic careful compiler transform)
1586 (defun ,careful (specifier)
1587 (handler-case (,basic specifier)
1588 (sb!kernel::arg-count-error (condition)
1589 (values nil (list (format nil "~A" condition))))
1590 (simple-error (condition)
1591 (values nil (list* (simple-condition-format-control condition)
1592 (simple-condition-format-arguments condition))))))
1593 (defun ,compiler (specifier)
1594 (multiple-value-bind (type error-args) (,careful specifier)
1596 (apply #'compiler-error error-args))))
1597 (defun ,transform (specifier)
1598 (multiple-value-bind (type error-args) (,careful specifier)
1600 (apply #'give-up-ir1-transform
1602 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1603 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1606 ;;;; utilities used at run-time for parsing &KEY args in IR1
1608 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1609 ;;; the lvar for the value of the &KEY argument KEY in the list of
1610 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
1611 ;;; otherwise. The legality and constantness of the keywords should
1612 ;;; already have been checked.
1613 (declaim (ftype (sfunction (list keyword) (or lvar null))
1615 (defun find-keyword-lvar (args key)
1616 (do ((arg args (cddr arg)))
1618 (when (eq (lvar-value (first arg)) key)
1619 (return (second arg)))))
1621 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1622 ;;; verify that alternating lvars in ARGS are constant and that there
1623 ;;; is an even number of args.
1624 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
1625 (defun check-key-args-constant (args)
1626 (do ((arg args (cddr arg)))
1628 (unless (and (rest arg)
1629 (constant-lvar-p (first arg)))
1632 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1633 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
1634 ;;; and that only keywords present in the list KEYS are supplied.
1635 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
1636 (defun check-transform-keys (args keys)
1637 (and (check-key-args-constant args)
1638 (do ((arg args (cddr arg)))
1640 (unless (member (lvar-value (first arg)) keys)
1645 ;;; Called by the expansion of the EVENT macro.
1646 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
1647 (defun %event (info node)
1648 (incf (event-info-count info))
1649 (when (and (>= (event-info-level info) *event-note-threshold*)
1650 (policy (or node *lexenv*)
1651 (= inhibit-warnings 0)))
1652 (let ((*compiler-error-context* node))
1653 (compiler-notify (event-info-description info))))
1655 (let ((action (event-info-action info)))
1656 (when action (funcall action node))))
1659 (defun make-cast (value type policy)
1660 (declare (type lvar value)
1662 (type policy policy))
1663 (%make-cast :asserted-type type
1664 :type-to-check (maybe-weaken-check type policy)
1666 :derived-type (coerce-to-values type)))
1668 (defun cast-type-check (cast)
1669 (declare (type cast cast))
1670 (when (cast-reoptimize cast)
1671 (ir1-optimize-cast cast t))
1672 (cast-%type-check cast))
1674 (defun note-single-valuified-lvar (lvar)
1675 (declare (type (or lvar null) lvar))
1677 (let ((use (lvar-uses lvar)))
1679 (let ((leaf (ref-leaf use)))
1680 (when (and (lambda-var-p leaf)
1681 (null (rest (leaf-refs leaf))))
1682 (reoptimize-lambda-var leaf))))
1683 ((or (listp use) (combination-p use))
1684 (do-uses (node lvar)
1685 (setf (node-reoptimize node) t)
1686 (setf (block-reoptimize (node-block node)) t)
1687 (setf (component-reoptimize (node-component node)) t)))))))