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 (defun lexenv-enclosing-cleanup (lexenv)
18 (declare (type lexenv lexenv
))
20 (lambda-call-lexenv (lexenv-lambda lexenv2
))))
22 (awhen (lexenv-cleanup lexenv2
)
25 ;;; Return the innermost cleanup enclosing NODE, or NIL if there is
26 ;;; none in its function. If NODE has no cleanup, but is in a LET,
27 ;;; then we must still check the environment that the call is in.
28 (defun node-enclosing-cleanup (node)
29 (declare (type node node
))
30 (lexenv-enclosing-cleanup (node-lexenv node
)))
32 (defun map-nested-cleanups (function lexenv
&optional return-value
)
33 (declare (type lexenv lexenv
))
34 (do ((cleanup (lexenv-enclosing-cleanup lexenv
)
35 (node-enclosing-cleanup (cleanup-mess-up cleanup
))))
36 ((not cleanup
) return-value
)
37 (funcall function cleanup
)))
39 ;;; Convert the FORM in a block inserted between BLOCK1 and BLOCK2 as
40 ;;; an implicit MV-PROG1. The inserted block is returned. NODE is used
41 ;;; for IR1 context when converting the form. Note that the block is
42 ;;; not assigned a number, and is linked into the DFO at the
43 ;;; beginning. We indicate that we have trashed the DFO by setting
44 ;;; COMPONENT-REANALYZE. If CLEANUP is supplied, then convert with
46 (defun insert-cleanup-code (block1 block2 node form
&optional cleanup
)
47 (declare (type cblock block1 block2
) (type node node
)
48 (type (or cleanup null
) cleanup
))
49 (setf (component-reanalyze (block-component block1
)) t
)
50 (with-ir1-environment-from-node node
51 (with-component-last-block (*current-component
*
52 (block-next (component-head *current-component
*)))
53 (let* ((start (make-ctran))
54 (block (ctran-starts-block start
))
57 (make-lexenv :cleanup cleanup
)
59 (change-block-successor block1 block2 block
)
60 (link-blocks block block2
)
61 (ir1-convert start next nil form
)
62 (setf (block-last block
) (ctran-use next
))
63 (setf (node-next (block-last block
)) nil
)
68 ;;; Return a list of all the nodes which use LVAR.
69 (declaim (ftype (sfunction (lvar) list
) find-uses
))
70 (defun find-uses (lvar)
71 (ensure-list (lvar-uses lvar
)))
73 (declaim (ftype (sfunction (lvar) lvar
) principal-lvar
))
74 (defun principal-lvar (lvar)
76 (let ((use (lvar-uses lvar
)))
82 (defun principal-lvar-use (lvar)
84 (declare (type lvar lvar
))
85 (let ((use (lvar-uses lvar
)))
87 (plu (cast-value use
))
91 (defun principal-lvar-dest (lvar)
93 (declare (type lvar lvar
))
94 (let ((dest (lvar-dest lvar
)))
96 (pld (cast-lvar dest
))
100 ;;; Update lvar use information so that NODE is no longer a use of its
103 ;;; Note: if you call this function, you may have to do a
104 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
106 (declaim (ftype (sfunction (node) (values))
109 ;;; Just delete NODE from its LVAR uses; LVAR is preserved so it may
110 ;;; be given a new use.
111 (defun %delete-lvar-use
(node)
112 (let ((lvar (node-lvar node
)))
114 (if (listp (lvar-uses lvar
))
115 (let ((new-uses (delq node
(lvar-uses lvar
))))
116 (setf (lvar-uses lvar
)
117 (if (singleton-p new-uses
)
120 (setf (lvar-uses lvar
) nil
))
123 ;;; Delete NODE from its LVAR uses; if LVAR has no other uses, delete
124 ;;; its DEST's block, which must be unreachable.
125 (defun delete-lvar-use (node)
126 (let ((lvar (node-lvar node
)))
128 (%delete-lvar-use node
)
129 (if (null (lvar-uses lvar
))
130 (binding* ((dest (lvar-dest lvar
) :exit-if-null
)
131 (() (not (node-deleted dest
)) :exit-if-null
)
132 (block (node-block dest
)))
133 (mark-for-deletion block
))
134 (reoptimize-lvar lvar
))))
137 ;;; Update lvar use information so that NODE uses LVAR.
139 ;;; Note: if you call this function, you may have to do a
140 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
142 (declaim (ftype (sfunction (node (or lvar null
)) (values)) add-lvar-use
))
143 (defun add-lvar-use (node lvar
)
144 (aver (not (node-lvar node
)))
146 (let ((uses (lvar-uses lvar
)))
147 (setf (lvar-uses lvar
)
154 (setf (node-lvar node
) lvar
)))
158 ;;; Return true if LVAR destination is executed immediately after
159 ;;; NODE. Cleanups are ignored.
160 (defun immediately-used-p (lvar node
)
161 (declare (type lvar lvar
) (type node node
))
162 (aver (eq (node-lvar node
) lvar
))
163 (let ((dest (lvar-dest lvar
)))
164 (acond ((node-next node
)
165 (eq (ctran-next it
) dest
))
166 (t (eq (block-start (first (block-succ (node-block node
))))
167 (node-prev dest
))))))
169 ;;; Returns the defined (usually untrusted) type of the combination,
170 ;;; or NIL if we couldn't figure it out.
171 (defun combination-defined-type (combination)
172 (let ((use (principal-lvar-use (basic-combination-fun combination
))))
173 (or (when (ref-p use
)
174 (let ((type (leaf-defined-type (ref-leaf use
))))
175 (when (fun-type-p type
)
176 (fun-type-returns type
))))
179 ;;; Return true if LVAR destination is executed after node with only
180 ;;; uninteresting nodes intervening.
182 ;;; Uninteresting nodes are nodes in the same block which are either
183 ;;; REFs, external CASTs to the same destination, or known combinations
184 ;;; that never unwind.
185 (defun almost-immediately-used-p (lvar node
)
186 (declare (type lvar lvar
)
188 (aver (eq (node-lvar node
) lvar
))
189 (let ((dest (lvar-dest lvar
)))
192 (let ((ctran (node-next node
)))
194 (setf node
(ctran-next ctran
))
196 (return-from almost-immediately-used-p t
)
201 (when (and (eq :external
(cast-type-check node
))
202 (eq dest
(node-dest node
)))
205 ;; KLUDGE: Unfortunately we don't have an attribute for
206 ;; "never unwinds", so we just special case
207 ;; %ALLOCATE-CLOSURES: it is easy to run into with eg.
208 ;; FORMAT and a non-constant first argument.
209 (when (eq '%allocate-closures
(combination-fun-source-name node nil
))
212 (when (eq (block-start (first (block-succ (node-block node
))))
214 (return-from almost-immediately-used-p t
))))))))
216 ;;;; lvar substitution
218 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
219 ;;; NIL. We do not flush OLD's DEST.
220 (defun substitute-lvar (new old
)
221 (declare (type lvar old new
))
222 (aver (not (lvar-dest new
)))
223 (let ((dest (lvar-dest old
)))
226 (cif (setf (if-test dest
) new
))
227 (cset (setf (set-value dest
) new
))
228 (creturn (setf (return-result dest
) new
))
229 (exit (setf (exit-value dest
) new
))
231 (if (eq old
(basic-combination-fun dest
))
232 (setf (basic-combination-fun dest
) new
)
233 (setf (basic-combination-args dest
)
234 (nsubst new old
(basic-combination-args dest
)))))
235 (cast (setf (cast-value dest
) new
)))
237 (setf (lvar-dest old
) nil
)
238 (setf (lvar-dest new
) dest
)
239 (flush-lvar-externally-checkable-type new
))
242 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
243 ;;; arbitary number of uses. NEW is supposed to be "later" than OLD.
244 (defun substitute-lvar-uses (new old propagate-dx
)
245 (declare (type lvar old
)
246 (type (or lvar null
) new
)
247 (type boolean propagate-dx
))
251 (%delete-lvar-use node
)
252 (add-lvar-use node new
))
253 (reoptimize-lvar new
)
254 (awhen (and propagate-dx
(lvar-dynamic-extent old
))
255 (setf (lvar-dynamic-extent old
) nil
)
256 (unless (lvar-dynamic-extent new
)
257 (setf (lvar-dynamic-extent new
) it
)
258 (setf (cleanup-info it
) (subst new old
(cleanup-info it
)))))
259 (when (lvar-dynamic-extent new
)
261 (node-ends-block node
))))
262 (t (flush-dest old
)))
266 ;;;; block starting/creation
268 ;;; Return the block that CTRAN is the start of, making a block if
269 ;;; necessary. This function is called by IR1 translators which may
270 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
271 ;;; used more than once must start a block by the time that anyone
272 ;;; does a USE-CTRAN on it.
274 ;;; We also throw the block into the next/prev list for the
275 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
277 (defun ctran-starts-block (ctran)
278 (declare (type ctran ctran
))
279 (ecase (ctran-kind ctran
)
281 (aver (not (ctran-block ctran
)))
282 (let* ((next (component-last-block *current-component
*))
283 (prev (block-prev next
))
284 (new-block (make-block ctran
)))
285 (setf (block-next new-block
) next
286 (block-prev new-block
) prev
287 (block-prev next
) new-block
288 (block-next prev
) new-block
289 (ctran-block ctran
) new-block
290 (ctran-kind ctran
) :block-start
)
291 (aver (not (ctran-use ctran
)))
294 (ctran-block ctran
))))
296 ;;; Ensure that CTRAN is the start of a block so that the use set can
297 ;;; be freely manipulated.
298 (defun ensure-block-start (ctran)
299 (declare (type ctran ctran
))
300 (let ((kind (ctran-kind ctran
)))
304 (setf (ctran-block ctran
)
305 (make-block-key :start ctran
))
306 (setf (ctran-kind ctran
) :block-start
))
308 (node-ends-block (ctran-use ctran
)))))
311 ;;; CTRAN must be the last ctran in an incomplete block; finish the
312 ;;; block and start a new one if necessary.
313 (defun start-block (ctran)
314 (declare (type ctran ctran
))
315 (aver (not (ctran-next ctran
)))
316 (ecase (ctran-kind ctran
)
318 (let ((block (ctran-block ctran
))
319 (node (ctran-use ctran
)))
320 (aver (not (block-last block
)))
322 (setf (block-last block
) node
)
323 (setf (node-next node
) nil
)
324 (setf (ctran-use ctran
) nil
)
325 (setf (ctran-kind ctran
) :unused
)
326 (setf (ctran-block ctran
) nil
)
327 (link-blocks block
(ctran-starts-block ctran
))))
332 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
333 ;;; call. Exactly one argument must be 'DUMMY, which will be replaced
334 ;;; with LVAR. In case of an ordinary call the function should not
335 ;;; have return type NIL. We create a new "filtered" lvar.
337 ;;; TODO: remove preconditions.
338 (defun filter-lvar (lvar form
)
339 (declare (type lvar lvar
) (type list form
))
340 (let* ((dest (lvar-dest lvar
))
341 (ctran (node-prev dest
)))
342 (with-ir1-environment-from-node dest
344 (ensure-block-start ctran
)
345 (let* ((old-block (ctran-block ctran
))
346 (new-start (make-ctran))
347 (filtered-lvar (make-lvar))
348 (new-block (ctran-starts-block new-start
)))
350 ;; Splice in the new block before DEST, giving the new block
351 ;; all of DEST's predecessors.
352 (dolist (block (block-pred old-block
))
353 (change-block-successor block old-block new-block
))
355 (ir1-convert new-start ctran filtered-lvar form
)
357 ;; KLUDGE: Comments at the head of this function in CMU CL
358 ;; said that somewhere in here we
359 ;; Set the new block's start and end cleanups to the *start*
360 ;; cleanup of PREV's block. This overrides the incorrect
361 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
362 ;; Unfortunately I can't find any code which corresponds to this.
363 ;; Perhaps it was a stale comment? Or perhaps I just don't
364 ;; understand.. -- WHN 19990521
366 ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
367 ;; no LET conversion has been done yet.) The [mv-]combination
368 ;; code from the call in the form will be the use of the new
369 ;; check lvar. We substitute exactly one argument.
370 (let* ((node (lvar-use filtered-lvar
))
372 (dolist (arg (basic-combination-args node
) (aver victim
))
373 (let* ((arg (principal-lvar arg
))
376 (when (and (ref-p use
)
377 (constant-p (setf leaf
(ref-leaf use
)))
378 (eql (constant-value leaf
) 'dummy
))
381 (aver (eq (constant-value (ref-leaf (lvar-use victim
)))
384 (substitute-lvar filtered-lvar lvar
)
385 (substitute-lvar lvar victim
)
388 ;; Invoking local call analysis converts this call to a LET.
389 (locall-analyze-component *current-component
*))))
392 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
393 (defun delete-filter (node lvar value
)
394 (aver (eq (lvar-dest value
) node
))
395 (aver (eq (node-lvar node
) lvar
))
396 (cond (lvar (collect ((merges))
397 (when (return-p (lvar-dest lvar
))
399 (when (and (basic-combination-p use
)
400 (eq (basic-combination-kind use
) :local
))
402 (substitute-lvar-uses lvar value
403 (and lvar
(eq (lvar-uses lvar
) node
)))
404 (%delete-lvar-use node
)
407 (dolist (merge (merges))
408 (merge-tail-sets merge
)))))
409 (t (flush-dest value
)
410 (unlink-node node
))))
412 ;;; Make a CAST and insert it into IR1 before node NEXT.
413 (defun insert-cast-before (next lvar type policy
&optional context
)
414 (declare (type node next
) (type lvar lvar
) (type ctype type
))
415 (with-ir1-environment-from-node next
416 (let* ((ctran (node-prev next
))
417 (cast (make-cast lvar type policy context
))
418 (internal-ctran (make-ctran)))
419 (setf (ctran-next ctran
) cast
420 (node-prev cast
) ctran
)
421 (use-ctran cast internal-ctran
)
422 (link-node-to-previous-ctran next internal-ctran
)
423 (setf (lvar-dest lvar
) cast
)
424 (reoptimize-lvar lvar
)
425 (when (return-p next
)
426 (node-ends-block cast
))
427 (setf (block-attributep (block-flags (node-block cast
))
428 type-check type-asserted
)
432 ;;;; miscellaneous shorthand functions
434 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
435 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
436 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
437 ;;; deleted, and then return its home.
438 (defun node-home-lambda (node)
439 (declare (type node node
))
440 (do ((fun (lexenv-lambda (node-lexenv node
))
441 (lexenv-lambda (lambda-call-lexenv fun
))))
442 ((not (memq (functional-kind fun
) '(:deleted
:zombie
)))
444 (when (eq (lambda-home fun
) fun
)
447 (declaim (ftype (sfunction (node) component
) node-component
))
448 (defun node-component (node)
449 (block-component (node-block node
)))
450 (declaim (ftype (sfunction (node) physenv
) node-physenv
))
451 (defun node-physenv (node)
452 (lambda-physenv (node-home-lambda node
)))
454 #!-sb-fluid
(declaim (inline node-stack-allocate-p
))
455 (defun node-stack-allocate-p (node)
456 (awhen (node-lvar node
)
457 (lvar-dynamic-extent it
)))
459 (defun flushable-combination-p (call)
460 (declare (type combination call
))
461 (let ((kind (combination-kind call
))
462 (info (combination-fun-info call
)))
463 (when (and (eq kind
:known
) (fun-info-p info
))
464 (let ((attr (fun-info-attributes info
)))
465 (when (and (not (ir1-attributep attr call
))
466 ;; FIXME: For now, don't consider potentially flushable
467 ;; calls flushable when they have the CALL attribute.
468 ;; Someday we should look at the functional args to
469 ;; determine if they have any side effects.
470 (if (policy call
(= safety
3))
471 (ir1-attributep attr flushable
)
472 (ir1-attributep attr unsafely-flushable
)))
475 ;;;; DYNAMIC-EXTENT related
477 (defun lambda-var-original-name (leaf)
478 (let ((home (lambda-var-home leaf
)))
479 (if (eq :external
(functional-kind home
))
480 (let* ((entry (functional-entry-fun home
))
481 (p (1- (position leaf
(lambda-vars home
)))))
483 (if (optional-dispatch-p entry
)
484 (elt (optional-dispatch-arglist entry
) p
)
485 (elt (lambda-vars entry
) p
))))
486 (leaf-debug-name leaf
))))
488 (defun note-no-stack-allocation (lvar &key flush
)
489 (do-uses (use (principal-lvar lvar
))
491 ;; Don't complain about not being able to stack allocate constants.
492 (and (ref-p use
) (constant-p (ref-leaf use
)))
493 ;; If we're flushing, don't complain if we can flush the combination.
494 (and flush
(combination-p use
) (flushable-combination-p use
))
495 ;; Don't report those with homes in :OPTIONAL -- we'd get doubled
497 (and (ref-p use
) (lambda-var-p (ref-leaf use
))
498 (eq :optional
(lambda-kind (lambda-var-home (ref-leaf use
))))))
499 ;; FIXME: For the first leg (lambda-bind (lambda-var-home ...))
500 ;; would be a far better description, but since we use
501 ;; *COMPILER-ERROR-CONTEXT* for muffling we can't -- as that node
502 ;; can have different handled conditions.
503 (let ((*compiler-error-context
* use
))
504 (if (and (ref-p use
) (lambda-var-p (ref-leaf use
)))
505 (compiler-notify "~@<could~2:I not stack allocate ~S in: ~S~:@>"
506 (lambda-var-original-name (ref-leaf use
))
507 (find-original-source (node-source-path use
)))
508 (compiler-notify "~@<could~2:I not stack allocate: ~S~:@>"
509 (find-original-source (node-source-path use
))))))))
511 (defun use-good-for-dx-p (use dx
&optional component
)
512 ;; FIXME: Can casts point to LVARs in other components?
513 ;; RECHECK-DYNAMIC-EXTENT-LVARS assumes that they can't -- that is, that the
514 ;; PRINCIPAL-LVAR is always in the same component as the original one. It
515 ;; would be either good to have an explanation of why casts don't point
516 ;; across components, or an explanation of when they do it. ...in the
517 ;; meanwhile AVER that our assumption holds true.
518 (aver (or (not component
) (eq component
(node-component use
))))
519 (or (dx-combination-p use dx
)
521 (not (cast-type-check use
))
522 (lvar-good-for-dx-p (cast-value use
) dx component
))
523 (and (trivial-lambda-var-ref-p use
)
524 (let ((uses (lvar-uses (trivial-lambda-var-ref-lvar use
))))
526 (lvar-good-for-dx-p (trivial-lambda-var-ref-lvar use
) dx component
))))))
528 (defun lvar-good-for-dx-p (lvar dx
&optional component
)
529 (let ((uses (lvar-uses lvar
))) ; TODO use ENSURE-LIST? or is it too slow?
533 (use-good-for-dx-p use dx component
))
535 (use-good-for-dx-p uses dx component
))))
537 (defun known-dx-combination-p (use dx
)
538 (and (eq (combination-kind use
) :known
)
539 (let ((info (combination-fun-info use
)))
540 (or (awhen (fun-info-stack-allocate-result info
)
542 (awhen (fun-info-result-arg info
)
543 (let ((args (combination-args use
)))
544 (lvar-good-for-dx-p (if (zerop it
)
549 ;;; Bound to NIL in RECHECK-DYNAMIC-EXTENT-LVARS, so that the
550 ;;; combinations that didn't get converted are treated as dx-safe.
551 (defvar *dx-combination-p-check-local
* t
)
553 (defun dx-combination-p (use dx
)
554 (and (combination-p use
)
556 ;; Known, and can do DX.
557 (known-dx-combination-p use dx
)
558 ;; Possibly a not-yet-eliminated lambda which ends up returning the
559 ;; results of an actual known DX combination.
560 (and *dx-combination-p-check-local
*
561 (let* ((fun (combination-fun use
))
562 (ref (principal-lvar-use fun
))
563 (clambda (when (ref-p ref
)
565 (creturn (when (lambda-p clambda
)
566 (lambda-return clambda
)))
567 (result-use (when (return-p creturn
)
568 (principal-lvar-use (return-result creturn
)))))
569 ;; FIXME: We should be able to deal with multiple uses here as well.
570 (and (dx-combination-p result-use dx
)
571 (combination-args-flow-cleanly-p use result-use dx
)))))))
573 (defun combination-args-flow-cleanly-p (combination1 combination2 dx
)
574 (labels ((recurse (combination)
575 (or (eq combination combination2
)
576 (if (known-dx-combination-p combination dx
)
577 (let ((dest (lvar-dest (combination-lvar combination
))))
578 (and (combination-p dest
)
580 (let* ((fun1 (combination-fun combination
))
581 (ref1 (principal-lvar-use fun1
))
582 (clambda1 (when (ref-p ref1
) (ref-leaf ref1
))))
583 (when (lambda-p clambda1
)
584 (dolist (var (lambda-vars clambda1
) t
)
585 (dolist (var-ref (lambda-var-refs var
))
586 (let* ((lvar (ref-lvar var-ref
))
587 (dest (and lvar
(principal-lvar-dest lvar
))))
588 (unless (or (not dest
)
589 (and (combination-p dest
) (recurse dest
)))
590 (return-from combination-args-flow-cleanly-p nil
)))))))))))
591 (recurse combination1
)))
593 (defun ref-good-for-dx-p (ref)
594 (let* ((lvar (ref-lvar ref
))
595 (dest (when lvar
(lvar-dest lvar
))))
596 (and (combination-p dest
)
597 (eq :known
(combination-kind dest
))
598 (awhen (combination-fun-info dest
)
599 (or (ir1-attributep (fun-info-attributes it
) dx-safe
)
600 (and (not (combination-lvar dest
))
601 (awhen (fun-info-result-arg it
)
602 (eql lvar
(nth it
(combination-args dest
))))))))))
604 (defun trivial-lambda-var-ref-p (use)
606 (let ((var (ref-leaf use
)))
607 ;; lambda-var, no SETS, not explicitly indefinite-extent.
608 (when (and (lambda-var-p var
) (not (lambda-var-sets var
))
609 (neq :indefinite
(lambda-var-extent var
)))
610 (let ((home (lambda-var-home var
))
611 (refs (lambda-var-refs var
)))
612 ;; bound by a non-XEP system lambda, no other REFS that aren't
613 ;; DX-SAFE, or are result-args when the result is discarded.
614 (when (and (lambda-system-lambda-p home
)
615 (neq :external
(lambda-kind home
))
617 (unless (or (eq use ref
) (ref-good-for-dx-p ref
))
619 ;; the LAMBDA this var is bound by has only a single REF, going
621 (let* ((lambda-refs (lambda-refs home
))
622 (primary (car lambda-refs
)))
624 (not (cdr lambda-refs
))
625 (combination-p (lvar-dest (ref-lvar primary
)))))))))))
627 (defun trivial-lambda-var-ref-lvar (use)
628 (let* ((this (ref-leaf use
))
629 (fun (lambda-var-home this
))
630 (vars (lambda-vars fun
))
631 (combination (lvar-dest (ref-lvar (car (lambda-refs fun
)))))
632 (args (combination-args combination
)))
633 (aver (= (length vars
) (length args
)))
634 (loop for var in vars
639 ;;; This needs to play nice with LVAR-GOOD-FOR-DX-P and friends.
640 (defun handle-nested-dynamic-extent-lvars (dx lvar
&optional recheck-component
)
641 (let ((uses (lvar-uses lvar
)))
642 ;; DX value generators must end their blocks: see UPDATE-UVL-LIVE-SETS.
643 ;; Uses of mupltiple-use LVARs already end their blocks, so we just need
644 ;; to process uses of single-use LVARs.
646 (node-ends-block uses
))
647 ;; If this LVAR's USE is good for DX, it is either a CAST, or it
648 ;; must be a regular combination whose arguments are potentially DX as well.
649 (flet ((recurse (use)
652 (handle-nested-dynamic-extent-lvars
653 dx
(cast-value use
) recheck-component
))
655 (loop for arg in
(combination-args use
)
656 ;; deleted args show up as NIL here
658 (lvar-good-for-dx-p arg dx recheck-component
))
659 append
(handle-nested-dynamic-extent-lvars
660 dx arg recheck-component
)))
662 (let* ((other (trivial-lambda-var-ref-lvar use
)))
663 (unless (eq other lvar
)
664 (handle-nested-dynamic-extent-lvars
665 dx other recheck-component
)))))))
667 (if (listp uses
) ; TODO use ENSURE-LIST? or is it too slow?
668 (loop for use in uses
669 when
(use-good-for-dx-p use dx recheck-component
)
671 (when (use-good-for-dx-p uses dx recheck-component
)
676 (declaim (inline block-to-be-deleted-p
))
677 (defun block-to-be-deleted-p (block)
678 (or (block-delete-p block
)
679 (eq (functional-kind (block-home-lambda block
)) :deleted
)))
681 ;;; Checks whether NODE is in a block to be deleted
682 (declaim (inline node-to-be-deleted-p
))
683 (defun node-to-be-deleted-p (node)
684 (block-to-be-deleted-p (node-block node
)))
686 (declaim (ftype (sfunction (clambda) cblock
) lambda-block
))
687 (defun lambda-block (clambda)
688 (node-block (lambda-bind clambda
)))
689 (declaim (ftype (sfunction (clambda) component
) lambda-component
))
690 (defun lambda-component (clambda)
691 (block-component (lambda-block clambda
)))
693 (declaim (ftype (sfunction (cblock) node
) block-start-node
))
694 (defun block-start-node (block)
695 (ctran-next (block-start block
)))
697 ;;; Return the enclosing cleanup for environment of the first or last
699 (defun block-start-cleanup (block)
700 (node-enclosing-cleanup (block-start-node block
)))
701 (defun block-end-cleanup (block)
702 (node-enclosing-cleanup (block-last block
)))
704 ;;; Return the lexenv of the last node in BLOCK.
705 (defun block-end-lexenv (block)
706 (node-lexenv (block-last block
)))
708 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
709 ;;; if there is none.
711 ;;; There can legitimately be no home lambda in dead code early in the
712 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
713 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
714 ;;; where the block is just a placeholder during parsing and doesn't
715 ;;; actually correspond to code which will be written anywhere.
716 (declaim (ftype (sfunction (cblock) (or clambda null
)) block-home-lambda-or-null
))
717 (defun block-home-lambda-or-null (block)
718 (if (node-p (block-last block
))
719 ;; This is the old CMU CL way of doing it.
720 (node-home-lambda (block-last block
))
721 ;; Now that SBCL uses this operation more aggressively than CMU
722 ;; CL did, the old CMU CL way of doing it can fail in two ways.
723 ;; 1. It can fail in a few cases even when a meaningful home
724 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
726 ;; 2. It can fail when converting a form which is born orphaned
727 ;; so that it never had a meaningful home lambda, e.g. a form
728 ;; which follows a RETURN-FROM or GO form.
729 (let ((pred-list (block-pred block
)))
730 ;; To deal with case 1, we reason that
731 ;; previous-in-target-execution-order blocks should be in the
732 ;; same lambda, and that they seem in practice to be
733 ;; previous-in-compilation-order blocks too, so we look back
734 ;; to find one which is sufficiently initialized to tell us
735 ;; what the home lambda is.
737 ;; We could get fancy about this, flooding through the
738 ;; graph of all the previous blocks, but in practice it
739 ;; seems to work just to grab the first previous block and
741 (node-home-lambda (block-last (first pred-list
)))
742 ;; In case 2, we end up with an empty PRED-LIST and
743 ;; have to punt: There's no home lambda.
746 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
747 (declaim (ftype (sfunction (cblock) clambda
) block-home-lambda
))
748 (defun block-home-lambda (block)
749 (block-home-lambda-or-null block
))
751 ;;; Return the IR1 physical environment for BLOCK.
752 (declaim (ftype (sfunction (cblock) physenv
) block-physenv
))
753 (defun block-physenv (block)
754 (lambda-physenv (block-home-lambda block
)))
756 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
757 ;;; of its original source's top level form in its compilation unit.
758 (defun source-path-tlf-number (path)
759 (declare (list path
))
762 ;;; Return the (reversed) list for the PATH in the original source
763 ;;; (with the Top Level Form number last).
764 (defun source-path-original-source (path)
765 (declare (list path
) (inline member
))
766 (cddr (member 'original-source-start path
:test
#'eq
)))
768 ;;; Return the Form Number of PATH's original source inside the Top
769 ;;; Level Form that contains it. This is determined by the order that
770 ;;; we walk the subforms of the top level source form.
771 (defun source-path-form-number (path)
772 (declare (list path
) (inline member
))
773 (cadr (member 'original-source-start path
:test
#'eq
)))
775 ;;; Return a list of all the enclosing forms not in the original
776 ;;; source that converted to get to this form, with the immediate
777 ;;; source for node at the start of the list.
778 (defun source-path-forms (path)
779 (subseq path
0 (position 'original-source-start path
)))
781 (defun tree-some (predicate tree
)
782 (let ((seen (make-hash-table)))
783 (labels ((walk (tree)
784 (cond ((funcall predicate tree
))
786 (not (gethash tree seen
)))
787 (setf (gethash tree seen
) t
)
788 (or (walk (car tree
))
789 (walk (cdr tree
)))))))
792 ;;; Return the innermost source form for NODE.
793 (defun node-source-form (node)
794 (declare (type node node
))
795 (let* ((path (node-source-path node
))
796 (forms (remove-if (lambda (x)
797 (tree-some #'leaf-p x
))
798 (source-path-forms path
))))
799 ;; another option: if first form includes a leaf, return
800 ;; find-original-source instead.
803 (values (find-original-source path
)))))
805 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
807 (defun lvar-source (lvar)
808 (let ((use (lvar-uses lvar
)))
811 (values (node-source-form use
) t
))))
813 (defun common-suffix (x y
)
814 (let ((mismatch (mismatch x y
:from-end t
)))
819 ;;; If the LVAR has a single use, return NODE-SOURCE-FORM as a
820 ;;; singleton. Otherwise, return a list of the lowest common
821 ;;; ancestor source form of all the uses (if it can be found),
822 ;;; followed by all the uses' source forms.
823 (defun lvar-all-sources (lvar)
824 (let ((use (lvar-uses lvar
)))
827 (path (node-source-path (first use
))))
828 (dolist (use use
(cons (if (find 'original-source-start path
)
829 (find-original-source path
)
832 (pushnew (node-source-form use
) forms
)
833 (setf path
(common-suffix path
834 (node-source-path use
)))))
835 (list (node-source-form use
)))))
837 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
838 (declaim (ftype (sfunction (ctran) (or clambda null
))
839 ctran-home-lambda-or-null
))
840 (defun ctran-home-lambda-or-null (ctran)
841 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
842 ;; implementation might not be quite right, or might be uglier than
843 ;; necessary. It appears that the original Python never found a need
844 ;; to do this operation. The obvious things based on
845 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
846 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
847 ;; generalize it enough to grovel harder when the simple CMU CL
848 ;; approach fails, and furthermore realize that in some exceptional
849 ;; cases it might return NIL. -- WHN 2001-12-04
850 (cond ((ctran-use ctran
)
851 (node-home-lambda (ctran-use ctran
)))
853 (block-home-lambda-or-null (ctran-block ctran
)))
855 (bug "confused about home lambda for ~S" ctran
))))
857 ;;; Return the LAMBDA that is CTRAN's home.
858 (declaim (ftype (sfunction (ctran) clambda
) ctran-home-lambda
))
859 (defun ctran-home-lambda (ctran)
860 (ctran-home-lambda-or-null ctran
))
862 (declaim (inline cast-single-value-p
))
863 (defun cast-single-value-p (cast)
864 (not (values-type-p (cast-asserted-type cast
))))
866 #!-sb-fluid
(declaim (inline lvar-single-value-p
))
867 (defun lvar-single-value-p (lvar)
868 (or (not lvar
) (%lvar-single-value-p lvar
)))
869 (defun %lvar-single-value-p
(lvar)
870 (let ((dest (lvar-dest lvar
)))
875 (eq (basic-combination-fun dest
) lvar
))
877 (and (cast-single-value-p dest
)
878 (acond ((node-lvar dest
) (%lvar-single-value-p it
))
882 (defun principal-lvar-end (lvar)
883 (loop for prev
= lvar then
(node-lvar dest
)
884 for dest
= (and prev
(lvar-dest prev
))
886 finally
(return (values dest prev
))))
888 (defun principal-lvar-single-valuify (lvar)
889 (loop for prev
= lvar then
(node-lvar dest
)
890 for dest
= (and prev
(lvar-dest prev
))
892 do
(setf (node-derived-type dest
)
893 (make-short-values-type (list (single-value-type
894 (node-derived-type dest
)))))
895 (reoptimize-lvar prev
)))
897 ;;; Return a new LEXENV just like DEFAULT except for the specified
898 ;;; slot values. Values for the alist slots are APPENDed to the
899 ;;; beginning of the current value, rather than replacing it entirely.
900 (defun make-lexenv (&key
(default *lexenv
*)
901 funs vars blocks tags
903 (lambda (lexenv-lambda default
))
904 (cleanup (lexenv-cleanup default
))
905 (handled-conditions (lexenv-handled-conditions default
))
906 (disabled-package-locks
907 (lexenv-disabled-package-locks default
))
908 (policy (lexenv-policy default
))
909 (user-data (lexenv-user-data default
)))
910 (macrolet ((frob (var slot
)
911 `(let ((old (,slot default
)))
915 (internal-make-lexenv
916 (frob funs lexenv-funs
)
917 (frob vars lexenv-vars
)
918 (frob blocks lexenv-blocks
)
919 (frob tags lexenv-tags
)
920 (frob type-restrictions lexenv-type-restrictions
)
922 cleanup handled-conditions disabled-package-locks
927 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
929 (defun make-restricted-lexenv (lexenv)
930 (flet ((fun-good-p (fun)
931 (destructuring-bind (name . thing
) fun
932 (declare (ignore name
))
936 (cons (aver (eq (car thing
) 'macro
))
939 (destructuring-bind (name . thing
) var
940 (declare (ignore name
))
942 ;; The evaluator will mark lexicals with :BOGUS when it
943 ;; translates an interpreter lexenv to a compiler
945 ((or leaf
#!+sb-eval
(member :bogus
)) nil
)
946 (cons (aver (eq (car thing
) 'macro
))
948 (heap-alien-info nil
)))))
949 (internal-make-lexenv
950 (remove-if-not #'fun-good-p
(lexenv-funs lexenv
))
951 (remove-if-not #'var-good-p
(lexenv-vars lexenv
))
954 (lexenv-type-restrictions lexenv
) ; XXX
957 (lexenv-handled-conditions lexenv
)
958 (lexenv-disabled-package-locks lexenv
)
959 (lexenv-policy lexenv
)
960 (lexenv-user-data lexenv
)
963 ;;;; flow/DFO/component hackery
965 ;;; Join BLOCK1 and BLOCK2.
966 (defun link-blocks (block1 block2
)
967 (declare (type cblock block1 block2
))
968 (setf (block-succ block1
)
969 (if (block-succ block1
)
970 (%link-blocks block1 block2
)
972 (push block1
(block-pred block2
))
974 (defun %link-blocks
(block1 block2
)
975 (declare (type cblock block1 block2
))
976 (let ((succ1 (block-succ block1
)))
977 (aver (not (memq block2 succ1
)))
978 (cons block2 succ1
)))
980 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
981 ;;; this leaves a successor with a single predecessor that ends in an
982 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
983 ;;; now be able to be propagated to the successor.
984 (defun unlink-blocks (block1 block2
)
985 (declare (type cblock block1 block2
))
986 (let ((succ1 (block-succ block1
)))
987 (if (eq block2
(car succ1
))
988 (setf (block-succ block1
) (cdr succ1
))
989 (do ((succ (cdr succ1
) (cdr succ
))
991 ((eq (car succ
) block2
)
992 (setf (cdr prev
) (cdr succ
)))
995 (let ((new-pred (delq block1
(block-pred block2
))))
996 (setf (block-pred block2
) new-pred
)
997 (when (singleton-p new-pred
)
998 (let ((pred-block (first new-pred
)))
999 (when (if-p (block-last pred-block
))
1000 (setf (block-test-modified pred-block
) t
)))))
1003 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
1004 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
1005 ;;; consequent/alternative blocks to point to NEW. We also set
1006 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
1007 ;;; the new successor.
1008 (defun change-block-successor (block old new
)
1009 (declare (type cblock new old block
))
1010 (unlink-blocks block old
)
1011 (let ((last (block-last block
))
1012 (comp (block-component block
)))
1013 (setf (component-reanalyze comp
) t
)
1016 (setf (block-test-modified block
) t
)
1017 (let* ((succ-left (block-succ block
))
1018 (new (if (and (eq new
(component-tail comp
))
1022 (unless (memq new succ-left
)
1023 (link-blocks block new
))
1024 (macrolet ((frob (slot)
1025 `(when (eq (,slot last
) old
)
1026 (setf (,slot last
) new
))))
1027 (frob if-consequent
)
1028 (frob if-alternative
)
1029 (when (eq (if-consequent last
)
1030 (if-alternative last
))
1031 (reoptimize-component (block-component block
) :maybe
)))))
1033 (unless (memq new
(block-succ block
))
1034 (link-blocks block new
)))))
1038 ;;; Unlink a block from the next/prev chain. We also null out the
1040 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo
))
1041 (defun remove-from-dfo (block)
1042 (let ((next (block-next block
))
1043 (prev (block-prev block
)))
1044 (setf (block-component block
) nil
)
1045 (setf (block-next prev
) next
)
1046 (setf (block-prev next
) prev
))
1049 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
1050 ;;; COMPONENT to be the same as for AFTER.
1051 (defun add-to-dfo (block after
)
1052 (declare (type cblock block after
))
1053 (let ((next (block-next after
))
1054 (comp (block-component after
)))
1055 (aver (not (eq (component-kind comp
) :deleted
)))
1056 (setf (block-component block
) comp
)
1057 (setf (block-next after
) block
)
1058 (setf (block-prev block
) after
)
1059 (setf (block-next block
) next
)
1060 (setf (block-prev next
) block
))
1063 ;;; List all NLX-INFOs which BLOCK can exit to.
1065 ;;; We hope that no cleanup actions are performed in the middle of
1066 ;;; BLOCK, so it is enough to look only at cleanups in the block
1067 ;;; end. The tricky thing is a special cleanup block; all its nodes
1068 ;;; have the same cleanup info, corresponding to the start, so the
1069 ;;; same approach returns safe result.
1070 (defun map-block-nlxes (fun block
&optional dx-cleanup-fun
)
1071 (do-nested-cleanups (cleanup (block-end-lexenv block
))
1072 (let ((mess-up (cleanup-mess-up cleanup
)))
1073 (case (cleanup-kind cleanup
)
1075 (aver (entry-p mess-up
))
1076 (loop for exit in
(entry-exits mess-up
)
1077 for nlx-info
= (exit-nlx-info exit
)
1078 do
(funcall fun nlx-info
)))
1079 ((:catch
:unwind-protect
)
1080 (aver (combination-p mess-up
))
1081 (let* ((arg-lvar (first (basic-combination-args mess-up
)))
1082 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar
)))))
1083 (funcall fun nlx-info
)))
1085 (when dx-cleanup-fun
1086 (funcall dx-cleanup-fun cleanup
)))))))
1088 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
1089 ;;; the head and tail which are set to T.
1090 (declaim (ftype (sfunction (component) (values)) clear-flags
))
1091 (defun clear-flags (component)
1092 (let ((head (component-head component
))
1093 (tail (component-tail component
)))
1094 (setf (block-flag head
) t
)
1095 (setf (block-flag tail
) t
)
1096 (do-blocks (block component
)
1097 (setf (block-flag block
) nil
)))
1100 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
1101 ;;; true in the head and tail blocks.
1102 (declaim (ftype (sfunction () component
) make-empty-component
))
1103 (defun make-empty-component ()
1104 (let* ((head (make-block-key :start nil
:component nil
))
1105 (tail (make-block-key :start nil
:component nil
))
1106 (res (make-component head tail
)))
1107 (setf (block-flag head
) t
)
1108 (setf (block-flag tail
) t
)
1109 (setf (block-component head
) res
)
1110 (setf (block-component tail
) res
)
1111 (setf (block-next head
) tail
)
1112 (setf (block-prev tail
) head
)
1115 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
1116 ;;; The new block is added to the DFO immediately following NODE's block.
1117 (defun node-ends-block (node)
1118 (declare (type node node
))
1119 (let* ((block (node-block node
))
1120 (start (node-next node
))
1121 (last (block-last block
)))
1122 (check-type last node
)
1123 (unless (eq last node
)
1124 (aver (and (eq (ctran-kind start
) :inside-block
)
1125 (not (block-delete-p block
))))
1126 (let* ((succ (block-succ block
))
1128 (make-block-key :start start
1129 :component
(block-component block
)
1130 :succ succ
:last last
)))
1131 (setf (ctran-kind start
) :block-start
)
1132 (setf (ctran-use start
) nil
)
1133 (setf (block-last block
) node
)
1134 (setf (node-next node
) nil
)
1136 (setf (block-pred b
)
1137 (cons new-block
(remove block
(block-pred b
)))))
1138 (setf (block-succ block
) ())
1139 (link-blocks block new-block
)
1140 (add-to-dfo new-block block
)
1141 (setf (component-reanalyze (block-component block
)) t
)
1143 (do ((ctran start
(node-next (ctran-next ctran
))))
1145 (setf (ctran-block ctran
) new-block
))
1147 (setf (block-type-asserted block
) t
)
1148 (setf (block-test-modified block
) t
))))
1153 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
1154 (defun delete-lambda-var (leaf)
1155 (declare (type lambda-var leaf
))
1157 (setf (lambda-var-deleted leaf
) t
)
1158 ;; Iterate over all local calls flushing the corresponding argument,
1159 ;; allowing the computation of the argument to be deleted. We also
1160 ;; mark the LET for reoptimization, since it may be that we have
1161 ;; deleted its last variable.
1162 (let* ((fun (lambda-var-home leaf
))
1163 (n (position leaf
(lambda-vars fun
))))
1164 (dolist (ref (leaf-refs fun
))
1165 (let* ((lvar (node-lvar ref
))
1166 (dest (and lvar
(lvar-dest lvar
))))
1167 (when (and (combination-p dest
)
1168 (eq (basic-combination-fun dest
) lvar
)
1169 (eq (basic-combination-kind dest
) :local
))
1170 (let* ((args (basic-combination-args dest
))
1172 (reoptimize-lvar arg
)
1174 (setf (elt args n
) nil
))))))
1176 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
1177 ;; too much difficulty, since we can efficiently implement
1178 ;; write-only variables. We iterate over the SETs, marking their
1179 ;; blocks for dead code flushing, since we can delete SETs whose
1181 (dolist (set (lambda-var-sets leaf
))
1182 (setf (block-flush-p (node-block set
)) t
))
1186 ;;; Note that something interesting has happened to VAR.
1187 (defun reoptimize-lambda-var (var)
1188 (declare (type lambda-var var
))
1189 (let ((fun (lambda-var-home var
)))
1190 ;; We only deal with LET variables, marking the corresponding
1191 ;; initial value arg as needing to be reoptimized.
1192 (when (and (eq (functional-kind fun
) :let
)
1194 (do ((args (basic-combination-args
1195 (lvar-dest (node-lvar (first (leaf-refs fun
)))))
1197 (vars (lambda-vars fun
) (cdr vars
)))
1198 ((eq (car vars
) var
)
1199 (reoptimize-lvar (car args
))))))
1202 ;;; Delete a function that has no references. This need only be called
1203 ;;; on functions that never had any references, since otherwise
1204 ;;; DELETE-REF will handle the deletion.
1205 (defun delete-functional (fun)
1206 (aver (and (null (leaf-refs fun
))
1207 (not (functional-entry-fun fun
))))
1209 (optional-dispatch (delete-optional-dispatch fun
))
1210 (clambda (delete-lambda fun
)))
1213 ;;; Deal with deleting the last reference to a CLAMBDA, which means
1214 ;;; that the lambda is unreachable, so that its body may be
1215 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
1216 ;;; IR1-OPTIMIZE to delete its blocks.
1217 (defun delete-lambda (clambda)
1218 (declare (type clambda clambda
))
1219 (let ((original-kind (functional-kind clambda
))
1220 (bind (lambda-bind clambda
)))
1221 (aver (not (member original-kind
'(:deleted
:toplevel
))))
1222 (aver (not (functional-has-external-references-p clambda
)))
1223 (aver (or (eq original-kind
:zombie
) bind
))
1224 (setf (functional-kind clambda
) :deleted
)
1225 (setf (lambda-bind clambda
) nil
)
1227 (labels ((delete-children (lambda)
1228 (dolist (child (lambda-children lambda
))
1229 (cond ((eq (functional-kind child
) :deleted
)
1230 (delete-children child
))
1232 (delete-lambda child
))))
1233 (setf (lambda-children lambda
) nil
)
1234 (setf (lambda-parent lambda
) nil
)))
1235 (delete-children clambda
))
1237 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
1238 ;; that we're using the old value of the KIND slot, not the
1239 ;; current slot value, which has now been set to :DELETED.)
1242 ((:let
:mv-let
:assignment
)
1243 (let ((bind-block (node-block bind
)))
1244 (mark-for-deletion bind-block
))
1245 (let ((home (lambda-home clambda
)))
1246 (setf (lambda-lets home
) (delete clambda
(lambda-lets home
))))
1247 ;; KLUDGE: In presence of NLEs we cannot always understand that
1248 ;; LET's BIND dominates its body [for a LET "its" body is not
1249 ;; quite its]; let's delete too dangerous for IR2 stuff. --
1251 (dolist (var (lambda-vars clambda
))
1252 (flet ((delete-node (node)
1253 (mark-for-deletion (node-block node
))))
1254 (mapc #'delete-node
(leaf-refs var
))
1255 (mapc #'delete-node
(lambda-var-sets var
)))))
1257 ;; Function has no reachable references.
1258 (dolist (ref (lambda-refs clambda
))
1259 (mark-for-deletion (node-block ref
)))
1260 ;; If the function isn't a LET, we unlink the function head
1261 ;; and tail from the component head and tail to indicate that
1262 ;; the code is unreachable. We also delete the function from
1263 ;; COMPONENT-LAMBDAS (it won't be there before local call
1264 ;; analysis, but no matter.) If the lambda was never
1265 ;; referenced, we give a note.
1266 (let* ((bind-block (node-block bind
))
1267 (component (block-component bind-block
))
1268 (return (lambda-return clambda
))
1269 (return-block (and return
(node-block return
))))
1270 (unless (leaf-ever-used clambda
)
1271 (let ((*compiler-error-context
* bind
))
1272 (compiler-notify 'code-deletion-note
1273 :format-control
"deleting unused function~:[.~;~:*~% ~S~]"
1274 :format-arguments
(list (leaf-debug-name clambda
)))))
1275 (unless (block-delete-p bind-block
)
1276 (unlink-blocks (component-head component
) bind-block
))
1277 (when (and return-block
(not (block-delete-p return-block
)))
1278 (mark-for-deletion return-block
)
1279 (unlink-blocks return-block
(component-tail component
)))
1280 (setf (component-reanalyze component
) t
)
1281 (let ((tails (lambda-tail-set clambda
)))
1282 (setf (tail-set-funs tails
)
1283 (delete clambda
(tail-set-funs tails
)))
1284 (setf (lambda-tail-set clambda
) nil
))
1285 (setf (component-lambdas component
)
1286 (delq clambda
(component-lambdas component
))))))
1288 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
1289 ;; ENTRY-FUN so that people will know that it is not an entry
1291 (when (eq original-kind
:external
)
1292 (let ((fun (functional-entry-fun clambda
)))
1293 (setf (functional-entry-fun fun
) nil
)
1294 (when (optional-dispatch-p fun
)
1295 (delete-optional-dispatch fun
)))))
1299 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
1300 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
1301 ;;; is used both before and after local call analysis. Afterward, all
1302 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
1303 ;;; to the XEP, leaving it with no references at all. So we look at
1304 ;;; the XEP to see whether an optional-dispatch is still really being
1305 ;;; used. But before local call analysis, there are no XEPs, and all
1306 ;;; references are direct.
1308 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
1309 ;;; entry-points, making them be normal lambdas, and then deleting the
1310 ;;; ones with no references. This deletes any e-p lambdas that were
1311 ;;; either never referenced, or couldn't be deleted when the last
1312 ;;; reference was deleted (due to their :OPTIONAL kind.)
1314 ;;; Note that the last optional entry point may alias the main entry,
1315 ;;; so when we process the main entry, its KIND may have been changed
1316 ;;; to NIL or even converted to a LETlike value.
1317 (defun delete-optional-dispatch (leaf)
1318 (declare (type optional-dispatch leaf
))
1319 (let ((entry (functional-entry-fun leaf
)))
1321 (or (leaf-refs entry
)
1322 (eq (functional-kind entry
) :external
)))
1323 (aver (or (not entry
) (eq (functional-kind entry
) :deleted
)))
1324 (setf (functional-kind leaf
) :deleted
)
1327 (unless (eq (functional-kind fun
) :deleted
)
1328 (aver (eq (functional-kind fun
) :optional
))
1329 (setf (functional-kind fun
) nil
)
1330 (let ((refs (leaf-refs fun
)))
1332 (delete-lambda fun
))
1334 (or (maybe-let-convert fun
)
1335 (maybe-convert-to-assignment fun
)))
1337 (maybe-convert-to-assignment fun
)))))))
1339 (dolist (ep (optional-dispatch-entry-points leaf
))
1340 (when (promise-ready-p ep
)
1342 (when (optional-dispatch-more-entry leaf
)
1343 (frob (optional-dispatch-more-entry leaf
)))
1344 (let ((main (optional-dispatch-main-entry leaf
)))
1346 (setf (functional-entry-fun entry
) main
)
1347 (setf (functional-entry-fun main
) entry
))
1348 (when (eq (functional-kind main
) :optional
)
1353 (defun note-local-functional (fun)
1354 (declare (type functional fun
))
1355 (when (and (leaf-has-source-name-p fun
)
1356 (eq (leaf-source-name fun
) (functional-debug-name fun
)))
1357 (let ((name (leaf-source-name fun
)))
1358 (let ((defined-fun (gethash name
*free-funs
*)))
1359 (when (and defined-fun
1360 (defined-fun-p defined-fun
)
1361 (eq (defined-fun-functional defined-fun
) fun
))
1362 (remhash name
*free-funs
*))))))
1364 ;;; Return functional for DEFINED-FUN which has been converted in policy
1365 ;;; corresponding to the current one, or NIL if no such functional exists.
1367 ;;; Also check that the parent of the functional is visible in the current
1369 (defun defined-fun-functional (defined-fun)
1370 (let ((functionals (defined-fun-functionals defined-fun
)))
1372 (let* ((sample (car functionals
))
1373 (there (lambda-parent (if (lambda-p sample
)
1375 (optional-dispatch-main-entry sample
)))))
1377 (labels ((lookup (here)
1378 (unless (eq here there
)
1380 (lookup (lambda-parent here
))
1381 ;; We looked up all the way up, and didn't find the parent
1382 ;; of the functional -- therefore it is nested in a lambda
1383 ;; we don't see, so return nil.
1384 (return-from defined-fun-functional nil
)))))
1385 (lookup (lexenv-lambda *lexenv
*)))))
1386 ;; Now find a functional whose policy matches the current one, if we already
1388 (let ((policy (lexenv-%policy
*lexenv
*)))
1389 (dolist (functional functionals
)
1390 (when (policy= policy
(lexenv-%policy
(functional-lexenv functional
)))
1391 (return functional
)))))))
1393 ;;; Do stuff to delete the semantic attachments of a REF node. When
1394 ;;; this leaves zero or one reference, we do a type dispatch off of
1395 ;;; the leaf to determine if a special action is appropriate.
1396 (defun delete-ref (ref)
1397 (declare (type ref ref
))
1398 (let* ((leaf (ref-leaf ref
))
1399 (refs (delq ref
(leaf-refs leaf
))))
1400 (setf (leaf-refs leaf
) refs
)
1405 (delete-lambda-var leaf
))
1407 (ecase (functional-kind leaf
)
1408 ((nil :let
:mv-let
:assignment
:escape
:cleanup
)
1409 (aver (null (functional-entry-fun leaf
)))
1410 (delete-lambda leaf
))
1412 (unless (functional-has-external-references-p leaf
)
1413 (delete-lambda leaf
)))
1414 ((:deleted
:zombie
:optional
))))
1416 (unless (eq (functional-kind leaf
) :deleted
)
1417 (delete-optional-dispatch leaf
)))))
1420 (clambda (or (maybe-let-convert leaf
)
1421 (maybe-convert-to-assignment leaf
)))
1422 (lambda-var (reoptimize-lambda-var leaf
))))
1425 (clambda (maybe-convert-to-assignment leaf
))))))
1429 ;;; This function is called to unlink a node from its LVAR;
1430 ;;; we assume that the LVAR's USE list has already been updated,
1431 ;;; and that we only have to mark the node as up for dead code
1432 ;;; elimination, and to clear it LVAR slot.
1433 (defun flush-node (node)
1434 (declare (type node node
))
1435 (let* ((prev (node-prev node
))
1436 (block (ctran-block prev
)))
1437 (reoptimize-component (block-component block
) t
)
1438 (setf (block-attributep (block-flags block
)
1439 flush-p type-asserted type-check
)
1441 (setf (node-lvar node
) nil
))
1443 ;;; This function is called by people who delete nodes; it provides a
1444 ;;; way to indicate that the value of a lvar is no longer used. We
1445 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1446 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1447 (defun flush-dest (lvar)
1448 (declare (type (or lvar null
) lvar
))
1450 (when (lvar-dynamic-extent lvar
)
1451 (note-no-stack-allocation lvar
:flush t
))
1452 (setf (lvar-dest lvar
) nil
)
1453 (flush-lvar-externally-checkable-type lvar
)
1456 (setf (lvar-uses lvar
) nil
))
1459 (defun delete-dest (lvar)
1461 (let* ((dest (lvar-dest lvar
))
1462 (prev (node-prev dest
)))
1463 (let ((block (ctran-block prev
)))
1464 (unless (block-delete-p block
)
1465 (mark-for-deletion block
))))))
1467 ;;; Queue the block for deletion
1468 (defun delete-block-lazily (block)
1469 (declare (type cblock block
))
1470 (unless (block-delete-p block
)
1471 (setf (block-delete-p block
) t
)
1472 (push block
(component-delete-blocks (block-component block
)))))
1474 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1475 ;;; blocks with the DELETE-P flag.
1476 (defun mark-for-deletion (block)
1477 (declare (type cblock block
))
1478 (let* ((component (block-component block
))
1479 (head (component-head component
)))
1480 (labels ((helper (block)
1481 (delete-block-lazily block
)
1482 (dolist (pred (block-pred block
))
1483 (unless (or (block-delete-p pred
)
1486 (unless (block-delete-p block
)
1488 (setf (component-reanalyze component
) t
))))
1491 ;;; This function does what is necessary to eliminate the code in it
1492 ;;; from the IR1 representation. This involves unlinking it from its
1493 ;;; predecessors and successors and deleting various node-specific
1494 ;;; semantic information. BLOCK must be already removed from
1495 ;;; COMPONENT-DELETE-BLOCKS.
1496 (defun delete-block (block &optional silent
)
1497 (declare (type cblock block
))
1498 (aver (block-component block
)) ; else block is already deleted!
1499 #!+high-security
(aver (not (memq block
(component-delete-blocks (block-component block
)))))
1501 (note-block-deletion block
))
1502 (setf (block-delete-p block
) t
)
1504 (dolist (b (block-pred block
))
1505 (unlink-blocks b block
)
1506 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1507 ;; broken when successors were deleted without setting the
1508 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1509 ;; doesn't happen again.
1510 (aver (not (and (null (block-succ b
))
1511 (not (block-delete-p b
))
1512 (not (eq b
(component-head (block-component b
))))))))
1513 (dolist (b (block-succ block
))
1514 (unlink-blocks block b
))
1516 (do-nodes-carefully (node block
)
1517 (when (valued-node-p node
)
1518 (delete-lvar-use node
))
1520 (ref (delete-ref node
))
1521 (cif (flush-dest (if-test node
)))
1522 ;; The next two cases serve to maintain the invariant that a LET
1523 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1524 ;; the lambda whenever we delete any of these, but we must be
1525 ;; careful that this LET has not already been partially deleted.
1527 (when (and (eq (basic-combination-kind node
) :local
)
1528 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1529 (lvar-uses (basic-combination-fun node
)))
1530 (let ((fun (combination-lambda node
)))
1531 ;; If our REF was the second-to-last ref, and has been
1532 ;; deleted, then FUN may be a LET for some other
1534 (when (and (functional-letlike-p fun
)
1535 (eq (let-combination fun
) node
))
1536 (delete-lambda fun
))))
1537 (flush-dest (basic-combination-fun node
))
1538 (dolist (arg (basic-combination-args node
))
1539 (when arg
(flush-dest arg
))))
1541 (let ((lambda (bind-lambda node
)))
1542 (unless (eq (functional-kind lambda
) :deleted
)
1543 (delete-lambda lambda
))))
1545 (let ((value (exit-value node
))
1546 (entry (exit-entry node
)))
1550 (setf (entry-exits entry
)
1551 (delq node
(entry-exits entry
))))))
1553 (dolist (exit (entry-exits node
))
1554 (mark-for-deletion (node-block exit
)))
1555 (let ((home (node-home-lambda node
)))
1556 (setf (lambda-entries home
) (delq node
(lambda-entries home
)))))
1558 (flush-dest (return-result node
))
1559 (delete-return node
))
1561 (flush-dest (set-value node
))
1562 (let ((var (set-var node
)))
1563 (setf (basic-var-sets var
)
1564 (delete node
(basic-var-sets var
)))))
1566 (flush-dest (cast-value node
)))))
1568 (remove-from-dfo block
)
1571 ;;; Do stuff to indicate that the return node NODE is being deleted.
1572 (defun delete-return (node)
1573 (declare (type creturn node
))
1574 (let* ((fun (return-lambda node
))
1575 (tail-set (lambda-tail-set fun
)))
1576 (aver (lambda-return fun
))
1577 (setf (lambda-return fun
) nil
)
1578 (when (and tail-set
(not (find-if #'lambda-return
1579 (tail-set-funs tail-set
))))
1580 (setf (tail-set-type tail-set
) *empty-type
*)))
1583 ;;; If any of the VARS in FUN was never referenced and was not
1584 ;;; declared IGNORE, then complain.
1585 (defun note-unreferenced-vars (vars policy
)
1587 (unless (or (leaf-ever-used var
)
1588 (lambda-var-ignorep var
))
1589 (unless (policy policy
(= inhibit-warnings
3))
1590 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1591 ;; requires this to be no more than a STYLE-WARNING.
1593 (compiler-style-warn "The variable ~S is defined but never used."
1594 (leaf-debug-name var
))
1595 ;; There's no reason to accept this kind of equivocation
1596 ;; when compiling our own code, though.
1598 (warn "The variable ~S is defined but never used."
1599 (leaf-debug-name var
)))
1600 (setf (leaf-ever-used var
) t
)))) ; to avoid repeated warnings? -- WHN
1602 (defun note-unreferenced-fun-vars (fun)
1603 (declare (type clambda fun
))
1604 (let ((*compiler-error-context
* (lambda-bind fun
)))
1605 (note-unreferenced-vars (lambda-vars fun
)
1606 *compiler-error-context
*))
1609 (defvar *deletion-ignored-objects
* '(t nil
))
1611 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1612 ;;; our recursion so that we don't get lost in circular structures. We
1613 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1614 ;;; function referencess with variables), and we also ignore anything
1616 (defun present-in-form (obj form depth
)
1617 (declare (type (integer 0 20) depth
))
1618 (cond ((= depth
20) nil
)
1622 (let ((first (car form
))
1624 (if (member first
'(quote function
))
1626 (or (and (not (symbolp first
))
1627 (present-in-form obj first depth
))
1628 (do ((l (cdr form
) (cdr l
))
1630 ((or (atom l
) (> n
100))
1632 (declare (fixnum n
))
1633 (when (present-in-form obj
(car l
) depth
)
1636 ;;; This function is called on a block immediately before we delete
1637 ;;; it. We check to see whether any of the code about to die appeared
1638 ;;; in the original source, and emit a note if so.
1640 ;;; If the block was in a lambda is now deleted, then we ignore the
1641 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1642 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1643 ;;; reasonable for a function to not return, and there is a different
1644 ;;; note for that case anyway.
1646 ;;; If the actual source is an atom, then we use a bunch of heuristics
1647 ;;; to guess whether this reference really appeared in the original
1649 ;;; -- If a symbol, it must be interned and not a keyword.
1650 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1651 ;;; or a character.)
1652 ;;; -- The atom must be "present" in the original source form, and
1653 ;;; present in all intervening actual source forms.
1654 (defun note-block-deletion (block)
1655 (let ((home (block-home-lambda block
)))
1656 (unless (eq (functional-kind home
) :deleted
)
1657 (do-nodes (node nil block
)
1658 (let* ((path (node-source-path node
))
1659 (first (first path
)))
1660 (when (or (eq first
'original-source-start
)
1662 (or (not (symbolp first
))
1663 (let ((pkg (symbol-package first
)))
1665 (not (eq pkg
(symbol-package :end
))))))
1666 (not (member first
*deletion-ignored-objects
*))
1667 (not (typep first
'(or fixnum character
)))
1669 (present-in-form first x
0))
1670 (source-path-forms path
))
1671 (present-in-form first
(find-original-source path
)
1673 (unless (return-p node
)
1674 (let ((*compiler-error-context
* node
))
1675 (compiler-notify 'code-deletion-note
1676 :format-control
"deleting unreachable code"
1677 :format-arguments nil
)))
1681 ;;; Delete a node from a block, deleting the block if there are no
1682 ;;; nodes left. We remove the node from the uses of its LVAR.
1684 ;;; If the node is the last node, there must be exactly one successor.
1685 ;;; We link all of our precedessors to the successor and unlink the
1686 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1687 ;;; left, and the block is a successor of itself, then we replace the
1688 ;;; only node with a degenerate exit node. This provides a way to
1689 ;;; represent the bodyless infinite loop, given the prohibition on
1690 ;;; empty blocks in IR1.
1691 (defun unlink-node (node)
1692 (declare (type node node
))
1693 (when (valued-node-p node
)
1694 (delete-lvar-use node
))
1696 (let* ((ctran (node-next node
))
1697 (next (and ctran
(ctran-next ctran
)))
1698 (prev (node-prev node
))
1699 (block (ctran-block prev
))
1700 (prev-kind (ctran-kind prev
))
1701 (last (block-last block
)))
1703 (setf (block-type-asserted block
) t
)
1704 (setf (block-test-modified block
) t
)
1706 (cond ((or (eq prev-kind
:inside-block
)
1707 (and (eq prev-kind
:block-start
)
1708 (not (eq node last
))))
1709 (cond ((eq node last
)
1710 (setf (block-last block
) (ctran-use prev
))
1711 (setf (node-next (ctran-use prev
)) nil
))
1713 (setf (ctran-next prev
) next
)
1714 (setf (node-prev next
) prev
)
1715 (when (if-p next
) ; AOP wanted
1716 (reoptimize-lvar (if-test next
)))))
1717 (setf (node-prev node
) nil
)
1720 (aver (eq prev-kind
:block-start
))
1721 (aver (eq node last
))
1722 (let* ((succ (block-succ block
))
1723 (next (first succ
)))
1724 (aver (singleton-p succ
))
1726 ((eq block
(first succ
))
1727 (with-ir1-environment-from-node node
1728 (let ((exit (make-exit)))
1729 (setf (ctran-next prev
) nil
)
1730 (link-node-to-previous-ctran exit prev
)
1731 (setf (block-last block
) exit
)))
1732 (setf (node-prev node
) nil
)
1735 (aver (eq (block-start-cleanup block
)
1736 (block-end-cleanup block
)))
1737 (unlink-blocks block next
)
1738 (dolist (pred (block-pred block
))
1739 (change-block-successor pred block next
))
1740 (when (block-delete-p block
)
1741 (let ((component (block-component block
)))
1742 (setf (component-delete-blocks component
)
1743 (delq block
(component-delete-blocks component
)))))
1744 (remove-from-dfo block
)
1745 (setf (block-delete-p block
) t
)
1746 (setf (node-prev node
) nil
)
1749 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1751 (defun ctran-deleted-p (ctran)
1752 (declare (type ctran ctran
))
1753 (let ((block (ctran-block ctran
)))
1754 (or (not (block-component block
))
1755 (block-delete-p block
))))
1757 ;;; Return true if NODE has been deleted, false if it is still a valid
1759 (defun node-deleted (node)
1760 (declare (type node node
))
1761 (let ((prev (node-prev node
)))
1763 (ctran-deleted-p prev
))))
1765 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1766 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1767 ;;; triggered by deletion.
1768 (defun delete-component (component)
1769 (declare (type component component
))
1770 (aver (null (component-new-functionals component
)))
1771 (setf (component-kind component
) :deleted
)
1772 (do-blocks (block component
)
1773 (delete-block-lazily block
))
1774 (dolist (fun (component-lambdas component
))
1775 (unless (eq (functional-kind fun
) :deleted
)
1776 (setf (functional-kind fun
) nil
)
1777 (setf (functional-entry-fun fun
) nil
)
1778 (setf (leaf-refs fun
) nil
)
1779 (delete-functional fun
)))
1780 (clean-component component
)
1783 ;;; Remove all pending blocks to be deleted. Return the nearest live
1784 ;;; block after or equal to BLOCK.
1785 (defun clean-component (component &optional block
)
1786 (loop while
(component-delete-blocks component
)
1787 ;; actual deletion of a block may queue new blocks
1788 do
(let ((current (pop (component-delete-blocks component
))))
1789 (when (eq block current
)
1790 (setq block
(block-next block
)))
1791 (delete-block current
)))
1794 ;;; Convert code of the form
1795 ;;; (FOO ... (FUN ...) ...)
1797 ;;; (FOO ... ... ...).
1798 ;;; In other words, replace the function combination FUN by its
1799 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1800 ;;; to blow out of whatever transform called this. Note, as the number
1801 ;;; of arguments changes, the transform must be prepared to return a
1802 ;;; lambda with a new lambda-list with the correct number of
1804 (defun splice-fun-args (lvar fun num-args
)
1805 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments to feed
1806 directly to the LVAR-DEST of LVAR, which must be a combination. If FUN
1807 is :ANY, the function name is not checked."
1808 (declare (type lvar lvar
)
1810 (type index num-args
))
1811 (let ((outside (lvar-dest lvar
))
1812 (inside (lvar-uses lvar
)))
1813 (aver (combination-p outside
))
1814 (unless (combination-p inside
)
1815 (give-up-ir1-transform))
1816 (let ((inside-fun (combination-fun inside
)))
1817 (unless (or (eq fun
:any
)
1818 (eq (lvar-fun-name inside-fun
) fun
))
1819 (give-up-ir1-transform))
1820 (let ((inside-args (combination-args inside
)))
1821 (unless (= (length inside-args
) num-args
)
1822 (give-up-ir1-transform))
1823 (let* ((outside-args (combination-args outside
))
1824 (arg-position (position lvar outside-args
))
1825 (before-args (subseq outside-args
0 arg-position
))
1826 (after-args (subseq outside-args
(1+ arg-position
))))
1827 (dolist (arg inside-args
)
1828 (setf (lvar-dest arg
) outside
)
1829 (flush-lvar-externally-checkable-type arg
))
1830 (setf (combination-args inside
) nil
)
1831 (setf (combination-args outside
)
1832 (append before-args inside-args after-args
))
1833 (change-ref-leaf (lvar-uses inside-fun
)
1834 (find-free-fun 'list
"???"))
1835 (setf (combination-fun-info inside
) (info :function
:info
'list
)
1836 (combination-kind inside
) :known
)
1837 (setf (node-derived-type inside
) *wild-type
*)
1841 ;;; Eliminate keyword arguments from the call (leaving the
1842 ;;; parameters in place.
1844 ;;; (FOO ... :BAR X :QUUX Y)
1848 ;;; SPECS is a list of (:KEYWORD PARAMETER) specifications.
1849 ;;; Returns the list of specified parameters names in the
1850 ;;; order they appeared in the call. N-POSITIONAL is the
1851 ;;; number of positional arguments in th call.
1852 (defun eliminate-keyword-args (call n-positional specs
)
1853 (let* ((specs (copy-tree specs
))
1854 (all (combination-args call
))
1855 (new-args (reverse (subseq all
0 n-positional
)))
1856 (key-args (subseq all n-positional
))
1859 (loop while key-args
1860 do
(let* ((key (pop key-args
))
1861 (val (pop key-args
))
1862 (keyword (if (constant-lvar-p key
)
1864 (give-up-ir1-transform)))
1865 (spec (or (assoc keyword specs
:test
#'eq
)
1866 (give-up-ir1-transform))))
1868 (push key flushed-keys
)
1869 (push (second spec
) parameters
)
1870 ;; In case of duplicate keys.
1871 (setf (second spec
) (gensym))))
1872 (dolist (key flushed-keys
)
1874 (setf (combination-args call
) (reverse new-args
))
1875 (reverse parameters
)))
1877 (defun extract-fun-args (lvar fun num-args
)
1878 (declare (type lvar lvar
)
1879 (type (or symbol list
) fun
)
1880 (type index num-args
))
1881 (let ((inside (lvar-uses lvar
)))
1882 (unless (combination-p inside
)
1883 (give-up-ir1-transform))
1884 (let ((inside-fun (combination-fun inside
)))
1885 (unless (member (lvar-fun-name inside-fun
) (ensure-list fun
))
1886 (give-up-ir1-transform))
1887 (let ((inside-args (combination-args inside
)))
1888 (unless (= (length inside-args
) num-args
)
1889 (give-up-ir1-transform))
1890 (values (lvar-fun-name inside-fun
) inside-args
)))))
1892 (defun flush-combination (combination)
1893 (declare (type combination combination
))
1894 (flush-dest (combination-fun combination
))
1895 (dolist (arg (combination-args combination
))
1897 (unlink-node combination
)
1903 ;;; Change the LEAF that a REF refers to.
1904 (defun change-ref-leaf (ref leaf
&key recklessly
)
1905 (declare (type ref ref
) (type leaf leaf
))
1906 (unless (eq (ref-leaf ref
) leaf
)
1907 (push ref
(leaf-refs leaf
))
1909 (setf (ref-leaf ref
) leaf
)
1910 (setf (leaf-ever-used leaf
) t
)
1911 (let* ((ltype (leaf-type leaf
))
1912 (vltype (make-single-value-type ltype
)))
1913 (if (let* ((lvar (node-lvar ref
))
1914 (dest (and lvar
(lvar-dest lvar
))))
1915 (and (basic-combination-p dest
)
1916 (eq lvar
(basic-combination-fun dest
))
1917 (csubtypep ltype
(specifier-type 'function
))))
1918 (setf (node-derived-type ref
) vltype
)
1919 (derive-node-type ref vltype
:from-scratch recklessly
)))
1920 (reoptimize-lvar (node-lvar ref
)))
1923 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1924 (defun substitute-leaf (new-leaf old-leaf
)
1925 (declare (type leaf new-leaf old-leaf
))
1926 (dolist (ref (leaf-refs old-leaf
))
1927 (change-ref-leaf ref new-leaf
))
1930 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1931 ;;; whether to substitute
1932 (defun substitute-leaf-if (test new-leaf old-leaf
)
1933 (declare (type leaf new-leaf old-leaf
) (type function test
))
1934 (dolist (ref (leaf-refs old-leaf
))
1935 (when (funcall test ref
)
1936 (change-ref-leaf ref new-leaf
)))
1939 ;;; Return a LEAF which represents the specified constant object. If
1940 ;;; the object is not in *CONSTANTS*, then we create a new constant
1941 ;;; LEAF and enter it. If we are producing a fasl file, make sure that
1942 ;;; MAKE-LOAD-FORM gets used on any parts of the constant that it
1945 ;;; We are allowed to coalesce things like EQUAL strings and bit-vectors
1946 ;;; when file-compiling, but not when using COMPILE.
1947 (defun find-constant (object &optional
(name nil namep
))
1948 (let ((faslp (producing-fasl-file)))
1949 (labels ((make-it ()
1952 (maybe-emit-make-load-forms object name
)
1953 (maybe-emit-make-load-forms object
)))
1954 (make-constant object
))
1955 (core-coalesce-p (x)
1956 ;; True for things which retain their identity under EQUAL,
1957 ;; so we can safely share the same CONSTANT leaf between
1958 ;; multiple references.
1959 (or (typep x
'(or symbol number character
))
1960 ;; Amusingly enough, we see CLAMBDAs --among other things--
1961 ;; here, from compiling things like %ALLOCATE-CLOSUREs forms.
1962 ;; No point in stuffing them in the hash-table.
1963 (and (typep x
'instance
)
1964 (not (or (leaf-p x
) (node-p x
))))))
1965 (file-coalesce-p (x)
1966 ;; CLHS 3.2.4.2.2: We are also allowed to coalesce various
1967 ;; other things when file-compiling.
1968 (or (core-coalesce-p x
)
1970 (if (eq +code-coverage-unmarked
+ (cdr x
))
1971 ;; These are already coalesced, and the CAR should
1972 ;; always be OK, so no need to check.
1974 (unless (maybe-cyclic-p x
) ; safe for EQUAL?
1976 ((atom y
) (file-coalesce-p y
))
1977 (unless (file-coalesce-p (car y
))
1979 ;; We *could* coalesce base-strings as well,
1980 ;; but we'd need a separate hash-table for
1981 ;; that, since we are not allowed to coalesce
1982 ;; base-strings with non-base-strings.
1985 ;; in the cross-compiler, we coalesce
1986 ;; all strings with the same contents,
1987 ;; because we will end up dumping them
1988 ;; as base-strings anyway. In the
1989 ;; real compiler, we're not allowed to
1990 ;; coalesce regardless of string
1991 ;; specialized element type, so we
1992 ;; KLUDGE by coalescing only character
1993 ;; strings (the common case) and
1994 ;; punting on the other types.
1998 (vector character
))))))
2000 (if faslp
(file-coalesce-p x
) (core-coalesce-p x
))))
2001 (if (and (boundp '*constants
*) (coalescep object
))
2002 (or (gethash object
*constants
*)
2003 (setf (gethash object
*constants
*)
2007 ;;; Return true if VAR would have to be closed over if environment
2008 ;;; analysis ran now (i.e. if there are any uses that have a different
2009 ;;; home lambda than VAR's home.)
2010 (defun closure-var-p (var)
2011 (declare (type lambda-var var
))
2012 (let ((home (lambda-var-home var
)))
2013 (cond ((eq (functional-kind home
) :deleted
)
2015 (t (let ((home (lambda-home home
)))
2018 :key
#'node-home-lambda
2020 (or (frob (leaf-refs var
))
2021 (frob (basic-var-sets var
)))))))))
2023 ;;; If there is a non-local exit noted in ENTRY's environment that
2024 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
2025 (defun find-nlx-info (exit)
2026 (declare (type exit exit
))
2027 (let* ((entry (exit-entry exit
))
2028 (cleanup (entry-cleanup entry
))
2029 (block (first (block-succ (node-block exit
)))))
2030 (dolist (nlx (physenv-nlx-info (node-physenv entry
)) nil
)
2031 (when (and (eq (nlx-info-block nlx
) block
)
2032 (eq (nlx-info-cleanup nlx
) cleanup
))
2035 (defun nlx-info-lvar (nlx)
2036 (declare (type nlx-info nlx
))
2037 (node-lvar (block-last (nlx-info-target nlx
))))
2039 ;;;; functional hackery
2041 (declaim (ftype (sfunction (functional) clambda
) main-entry
))
2042 (defun main-entry (functional)
2043 (etypecase functional
2044 (clambda functional
)
2046 (optional-dispatch-main-entry functional
))))
2048 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
2049 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
2050 ;;; optional with null default and no SUPPLIED-P. There must be a
2051 ;;; &REST arg with no references.
2052 (declaim (ftype (sfunction (functional) boolean
) looks-like-an-mv-bind
))
2053 (defun looks-like-an-mv-bind (functional)
2054 (and (optional-dispatch-p functional
)
2055 (do ((arg (optional-dispatch-arglist functional
) (cdr arg
)))
2057 (let ((info (lambda-var-arg-info (car arg
))))
2058 (unless info
(return nil
))
2059 (case (arg-info-kind info
)
2061 (when (or (arg-info-supplied-p info
) (arg-info-default info
))
2064 (return (and (null (cdr arg
)) (null (leaf-refs (car arg
))))))
2068 (defun call-all-args-fixed-p (call)
2069 (loop for arg in
(basic-combination-args call
)
2070 always
(numberp (nth-value 1 (values-types
2071 (lvar-derived-type arg
))))))
2073 ;;; Return true if function is an external entry point. This is true
2074 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
2075 ;;; (:TOPLEVEL kind.)
2077 (declare (type functional fun
))
2078 (not (null (member (functional-kind fun
) '(:external
:toplevel
)))))
2080 ;;; If LVAR's only use is a non-notinline global function reference,
2081 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
2082 ;;; is true, then we don't care if the leaf is NOTINLINE.
2083 (defun lvar-fun-name (lvar &optional notinline-ok
)
2084 (declare (type lvar lvar
))
2085 (let ((use (lvar-uses lvar
)))
2087 (let ((leaf (ref-leaf use
)))
2088 (if (and (global-var-p leaf
)
2089 (eq (global-var-kind leaf
) :global-function
)
2090 (or (not (defined-fun-p leaf
))
2091 (not (eq (defined-fun-inlinep leaf
) :notinline
))
2093 (leaf-source-name leaf
)
2097 ;;; As above, but allow a quoted symbol also,
2098 ;;; in which case we don't check for notinline-ness,
2099 ;;; so be careful how you use this.
2100 ;;; Also note that Case 2 in LVAR-FUN-IS for dealing with #.#'NAME
2101 ;;; has no equivalent here.
2102 (defun lvar-fun-name* (lvar)
2103 (if (constant-lvar-p lvar
) (lvar-value lvar
) (lvar-fun-name lvar
)))
2105 (defun lvar-fun-debug-name (lvar)
2106 (declare (type lvar lvar
))
2107 (let ((uses (lvar-uses lvar
)))
2109 (leaf-debug-name (ref-leaf use
))))
2112 (mapcar #'name1 uses
)))))
2114 ;;; Return the source name of a combination -- or signals an error
2115 ;;; if the function leaf is anonymous.
2116 (defun combination-fun-source-name (combination &optional
(errorp t
))
2117 (let ((uses (principal-lvar-use (combination-fun combination
)))
2119 (cond ((and (ref-p uses
)
2120 (leaf-has-source-name-p (setf leaf
(ref-leaf uses
))))
2121 (values (leaf-source-name leaf
) t
))
2123 (aver (not "COMBINATION-FUN is not a ref to a nameful leaf")))
2125 (values nil nil
)))))
2127 (defun combination-fun-debug-name (combination)
2128 (leaf-debug-name (ref-leaf (lvar-uses (combination-fun combination
)))))
2130 ;;; Return the COMBINATION node that is the call to the LET FUN.
2131 (defun let-combination (fun)
2132 (declare (type clambda fun
))
2133 (aver (functional-letlike-p fun
))
2134 (lvar-dest (node-lvar (first (leaf-refs fun
)))))
2136 ;;; Return the initial value lvar for a LET variable, or NIL if there
2138 (defun let-var-initial-value (var)
2139 (declare (type lambda-var var
))
2140 (let ((fun (lambda-var-home var
)))
2141 (elt (combination-args (let-combination fun
))
2142 (position-or-lose var
(lambda-vars fun
)))))
2144 ;;; Return the LAMBDA that is called by the local CALL.
2145 (defun combination-lambda (call)
2146 (declare (type basic-combination call
))
2147 (aver (eq (basic-combination-kind call
) :local
))
2148 (ref-leaf (lvar-uses (basic-combination-fun call
))))
2150 (defvar *inline-expansion-limit
* 200
2151 "an upper limit on the number of inline function calls that will be expanded
2152 in any given code object (single function or block compilation)")
2154 ;;; Check whether NODE's component has exceeded its inline expansion
2155 ;;; limit, and warn if so, returning NIL.
2156 (defun inline-expansion-ok (node)
2157 (let ((expanded (incf (component-inline-expansions
2159 (node-block node
))))))
2160 (cond ((> expanded
*inline-expansion-limit
*) nil
)
2161 ((= expanded
*inline-expansion-limit
*)
2162 ;; FIXME: If the objective is to stop the recursive
2163 ;; expansion of inline functions, wouldn't it be more
2164 ;; correct to look back through surrounding expansions
2165 ;; (which are, I think, stored in the *CURRENT-PATH*, and
2166 ;; possibly stored elsewhere too) and suppress expansion
2167 ;; and print this warning when the function being proposed
2168 ;; for inline expansion is found there? (I don't like the
2169 ;; arbitrary numerical limit in principle, and I think
2170 ;; it'll be a nuisance in practice if we ever want the
2171 ;; compiler to be able to use WITH-COMPILATION-UNIT on
2172 ;; arbitrarily huge blocks of code. -- WHN)
2173 (let ((*compiler-error-context
* node
))
2174 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
2175 probably trying to~% ~
2176 inline a recursive function."
2177 *inline-expansion-limit
*))
2181 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
2182 (defun assure-functional-live-p (functional)
2183 (declare (type functional functional
))
2185 ;; looks LET-converted
2186 (functional-somewhat-letlike-p functional
)
2187 ;; It's possible for a LET-converted function to end up
2188 ;; deleted later. In that case, for the purposes of this
2189 ;; analysis, it is LET-converted: LET-converted functionals
2190 ;; are too badly trashed to expand them inline, and deleted
2191 ;; LET-converted functionals are even worse.
2192 (memq (functional-kind functional
) '(:deleted
:zombie
))))
2193 (throw 'locall-already-let-converted functional
)))
2195 (defun assure-leaf-live-p (leaf)
2198 (when (lambda-var-deleted leaf
)
2199 (throw 'locall-already-let-converted leaf
)))
2201 (assure-functional-live-p leaf
))))
2204 (defun call-full-like-p (call)
2205 (declare (type basic-combination call
))
2206 (let ((kind (basic-combination-kind call
)))
2208 (and (eq kind
:known
)
2209 (let ((info (basic-combination-fun-info call
)))
2211 (not (fun-info-ir2-convert info
))
2212 (dolist (template (fun-info-templates info
) t
)
2213 (when (eq (template-ltn-policy template
) :fast-safe
)
2214 (multiple-value-bind (val win
)
2215 (valid-fun-use call
(template-type template
))
2216 (when (or val
(not win
)) (return nil
)))))))))))
2220 ;;; Apply a function to some arguments, returning a list of the values
2221 ;;; resulting of the evaluation. If an error is signalled during the
2222 ;;; application, then we produce a warning message using WARN-FUN and
2223 ;;; return NIL as our second value to indicate this. NODE is used as
2224 ;;; the error context for any error message, and CONTEXT is a string
2225 ;;; that is spliced into the warning.
2226 (declaim (ftype (sfunction ((or symbol function
) list node function string
)
2227 (values list boolean
))
2229 (defun careful-call (function args node warn-fun context
)
2231 (multiple-value-list
2232 (handler-case (apply function args
)
2234 (let ((*compiler-error-context
* node
))
2235 (funcall warn-fun
"Lisp error during ~A:~%~A" context condition
)
2236 (return-from careful-call
(values nil nil
))))))
2239 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
2242 ((deffrob (basic careful compiler transform
)
2244 (defun ,careful
(specifier)
2245 (handler-case (,basic specifier
)
2246 ((or sb
!kernel
::arg-count-error
2247 type-error
) (condition)
2248 (values nil
(list (princ-to-string condition
))))
2249 (simple-error (condition)
2250 (values nil
(list* (simple-condition-format-control condition
)
2251 (simple-condition-format-arguments condition
))))))
2252 (defun ,compiler
(specifier)
2253 (multiple-value-bind (type error-args
) (,careful specifier
)
2255 (apply #'compiler-error error-args
))))
2256 (defun ,transform
(specifier)
2257 (multiple-value-bind (type error-args
) (,careful specifier
)
2259 (apply #'give-up-ir1-transform
2261 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type
)
2262 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type
))
2265 ;;;; utilities used at run-time for parsing &KEY args in IR1
2267 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
2268 ;;; the lvar for the value of the &KEY argument KEY in the list of
2269 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
2270 ;;; otherwise. The legality and constantness of the keywords should
2271 ;;; already have been checked.
2272 (declaim (ftype (sfunction (list keyword
) (or lvar null
))
2274 (defun find-keyword-lvar (args key
)
2275 (do ((arg args
(cddr arg
)))
2277 (when (eq (lvar-value (first arg
)) key
)
2278 (return (second arg
)))))
2280 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2281 ;;; verify that alternating lvars in ARGS are constant and that there
2282 ;;; is an even number of args.
2283 (declaim (ftype (sfunction (list) boolean
) check-key-args-constant
))
2284 (defun check-key-args-constant (args)
2285 (do ((arg args
(cddr arg
)))
2287 (unless (and (rest arg
)
2288 (constant-lvar-p (first arg
)))
2291 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2292 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
2293 ;;; and that only keywords present in the list KEYS are supplied.
2294 (declaim (ftype (sfunction (list list
) boolean
) check-transform-keys
))
2295 (defun check-transform-keys (args keys
)
2296 (and (check-key-args-constant args
)
2297 (do ((arg args
(cddr arg
)))
2299 (unless (member (lvar-value (first arg
)) keys
)
2304 ;;; Called by the expansion of the EVENT macro.
2305 (declaim (ftype (sfunction (event-info (or node null
)) *) %event
))
2306 (defun %event
(info node
)
2307 (incf (event-info-count info
))
2308 (when (and (>= (event-info-level info
) *event-note-threshold
*)
2309 (policy (or node
*lexenv
*)
2310 (= inhibit-warnings
0)))
2311 (let ((*compiler-error-context
* node
))
2312 (compiler-notify (event-info-description info
))))
2314 (let ((action (event-info-action info
)))
2315 (when action
(funcall action node
))))
2318 (defun make-cast (value type policy
&optional context
)
2319 (declare (type lvar value
)
2321 (type policy policy
))
2322 (%make-cast
:asserted-type type
2323 :type-to-check
(maybe-weaken-check type policy
)
2325 :derived-type
(coerce-to-values type
)
2328 (defun cast-type-check (cast)
2329 (declare (type cast cast
))
2330 (when (cast-reoptimize cast
)
2331 (ir1-optimize-cast cast t
))
2332 (cast-%type-check cast
))
2334 (defun note-single-valuified-lvar (lvar)
2335 (declare (type (or lvar null
) lvar
))
2337 (let ((use (lvar-uses lvar
)))
2339 (let ((leaf (ref-leaf use
)))
2340 (when (and (lambda-var-p leaf
)
2341 (null (rest (leaf-refs leaf
))))
2342 (reoptimize-lambda-var leaf
))))
2343 ((or (listp use
) (combination-p use
))
2344 (do-uses (node lvar
)
2345 (setf (node-reoptimize node
) t
)
2346 (setf (block-reoptimize (node-block node
)) t
)
2347 (reoptimize-component (node-component node
) :maybe
)))))))
2349 ;;; Return true if LVAR's only use is a reference to a global function
2350 ;;; designator with one of the specified NAMES, that hasn't been
2351 ;;; declared NOTINLINE.
2352 (defun lvar-fun-is (lvar names
)
2353 (declare (type lvar lvar
) (list names
))
2354 (let ((use (lvar-uses lvar
)))
2356 (let* ((*lexenv
* (node-lexenv use
))
2357 (leaf (ref-leaf use
))
2359 (cond ((global-var-p leaf
)
2361 (and (eq (global-var-kind leaf
) :global-function
)
2362 (car (member (leaf-source-name leaf
) names
2365 (let ((value (constant-value leaf
)))
2366 (car (if (functionp value
)
2371 (fdefinition name
)))
2375 :test
#'equal
))))))))
2377 (not (fun-lexically-notinline-p name
)))))))
2379 ;;; Return true if LVAR's only use is a call to one of the named functions
2380 ;;; (or any function if none are specified) with the specified number of
2381 ;;; of arguments (or any number if number is not specified)
2382 (defun lvar-matches (lvar &key fun-names arg-count
)
2383 (let ((use (lvar-uses lvar
)))
2384 (and (combination-p use
)
2386 (multiple-value-bind (name ok
)
2387 (combination-fun-source-name use nil
)
2388 (and ok
(member name fun-names
:test
#'eq
))))
2390 (= arg-count
(length (combination-args use
)))))))
2392 ;;; True if the optional has a rest-argument.
2393 (defun optional-rest-p (opt)
2394 (dolist (var (optional-dispatch-arglist opt
) nil
)
2395 (let* ((info (when (lambda-var-p var
)
2396 (lambda-var-arg-info var
)))
2398 (arg-info-kind info
))))
2399 (when (eq :rest kind
)
2402 ;;; Don't substitute single-ref variables on high-debug / low speed, to
2403 ;;; improve the debugging experience. ...but don't bother keeping those
2404 ;;; from system lambdas.
2405 (defun preserve-single-use-debug-var-p (call var
)
2406 (and (policy call
(eql preserve-single-use-debug-variables
3))
2407 (or (not (lambda-var-p var
))
2408 (not (lambda-system-lambda-p (lambda-var-home var
))))))