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 (declaim (ftype (sfunction (lvar) lvar) principal-lvar))
66 (defun principal-lvar (lvar)
68 (let ((use (lvar-uses lvar)))
74 (defun principal-lvar-use (lvar)
76 (declare (type lvar lvar))
77 (let ((use (lvar-uses lvar)))
79 (plu (cast-value use))
83 ;;; Update lvar use information so that NODE is no longer a use of its
86 ;;; Note: if you call this function, you may have to do a
87 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
89 (declaim (ftype (sfunction (node) (values))
92 ;;; Just delete NODE from its LVAR uses; LVAR is preserved so it may
93 ;;; be given a new use.
94 (defun %delete-lvar-use (node)
95 (let ((lvar (node-lvar node)))
97 (if (listp (lvar-uses lvar))
98 (let ((new-uses (delq node (lvar-uses lvar))))
99 (setf (lvar-uses lvar)
100 (if (singleton-p new-uses)
103 (setf (lvar-uses lvar) nil))
104 (setf (node-lvar node) nil)))
106 ;;; Delete NODE from its LVAR uses; if LVAR has no other uses, delete
107 ;;; its DEST's block, which must be unreachable.
108 (defun delete-lvar-use (node)
109 (let ((lvar (node-lvar node)))
111 (%delete-lvar-use node)
112 (if (null (lvar-uses lvar))
113 (binding* ((dest (lvar-dest lvar) :exit-if-null)
114 (() (not (node-deleted dest)) :exit-if-null)
115 (block (node-block dest)))
116 (mark-for-deletion block))
117 (reoptimize-lvar lvar))))
120 ;;; Update lvar use information so that NODE uses LVAR.
122 ;;; Note: if you call this function, you may have to do a
123 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
125 (declaim (ftype (sfunction (node (or lvar null)) (values)) add-lvar-use))
126 (defun add-lvar-use (node lvar)
127 (aver (not (node-lvar node)))
129 (let ((uses (lvar-uses lvar)))
130 (setf (lvar-uses lvar)
137 (setf (node-lvar node) lvar)))
141 ;;; Return true if LVAR destination is executed immediately after
142 ;;; NODE. Cleanups are ignored.
143 (defun immediately-used-p (lvar node)
144 (declare (type lvar lvar) (type node node))
145 (aver (eq (node-lvar node) lvar))
146 (let ((dest (lvar-dest lvar)))
147 (acond ((node-next node)
148 (eq (ctran-next it) dest))
149 (t (eq (block-start (first (block-succ (node-block node))))
150 (node-prev dest))))))
152 ;;; Returns the defined (usually untrusted) type of the combination,
153 ;;; or NIL if we couldn't figure it out.
154 (defun combination-defined-type (combination)
155 (let ((use (principal-lvar-use (basic-combination-fun combination))))
156 (or (when (ref-p use)
157 (let ((type (leaf-defined-type (ref-leaf use))))
158 (when (fun-type-p type)
159 (fun-type-returns type))))
162 ;;; Return true if LVAR destination is executed after node with only
163 ;;; uninteresting nodes intervening.
165 ;;; Uninteresting nodes are nodes in the same block which are either
166 ;;; REFs, external CASTs to the same destination, or known combinations
167 ;;; that never unwind.
168 (defun almost-immediately-used-p (lvar node)
169 (declare (type lvar lvar)
171 (aver (eq (node-lvar node) lvar))
172 (let ((dest (lvar-dest lvar)))
175 (let ((ctran (node-next node)))
177 (setf node (ctran-next ctran))
179 (return-from almost-immediately-used-p t)
184 (when (and (eq :external (cast-type-check node))
185 (eq dest (node-dest node)))
188 ;; KLUDGE: Unfortunately we don't have an attribute for
189 ;; "never unwinds", so we just special case
190 ;; %ALLOCATE-CLOSURES: it is easy to run into with eg.
191 ;; FORMAT and a non-constant first argument.
192 (when (eq '%allocate-closures (combination-fun-source-name node nil))
195 (when (eq (block-start (first (block-succ (node-block node))))
197 (return-from almost-immediately-used-p t))))))))
199 ;;;; lvar substitution
201 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
202 ;;; NIL. We do not flush OLD's DEST.
203 (defun substitute-lvar (new old)
204 (declare (type lvar old new))
205 (aver (not (lvar-dest new)))
206 (let ((dest (lvar-dest old)))
209 (cif (setf (if-test dest) new))
210 (cset (setf (set-value dest) new))
211 (creturn (setf (return-result dest) new))
212 (exit (setf (exit-value dest) new))
214 (if (eq old (basic-combination-fun dest))
215 (setf (basic-combination-fun dest) new)
216 (setf (basic-combination-args dest)
217 (nsubst new old (basic-combination-args dest)))))
218 (cast (setf (cast-value dest) new)))
220 (setf (lvar-dest old) nil)
221 (setf (lvar-dest new) dest)
222 (flush-lvar-externally-checkable-type new))
225 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
226 ;;; arbitary number of uses. NEW is supposed to be "later" than OLD.
227 (defun substitute-lvar-uses (new old propagate-dx)
228 (declare (type lvar old)
229 (type (or lvar null) new)
230 (type boolean propagate-dx))
234 (%delete-lvar-use node)
235 (add-lvar-use node new))
236 (reoptimize-lvar new)
237 (awhen (and propagate-dx (lvar-dynamic-extent old))
238 (setf (lvar-dynamic-extent old) nil)
239 (unless (lvar-dynamic-extent new)
240 (setf (lvar-dynamic-extent new) it)
241 (setf (cleanup-info it) (subst new old (cleanup-info it)))))
242 (when (lvar-dynamic-extent new)
244 (node-ends-block node))))
245 (t (flush-dest old)))
249 ;;;; block starting/creation
251 ;;; Return the block that CTRAN is the start of, making a block if
252 ;;; necessary. This function is called by IR1 translators which may
253 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
254 ;;; used more than once must start a block by the time that anyone
255 ;;; does a USE-CTRAN on it.
257 ;;; We also throw the block into the next/prev list for the
258 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
260 (defun ctran-starts-block (ctran)
261 (declare (type ctran ctran))
262 (ecase (ctran-kind ctran)
264 (aver (not (ctran-block ctran)))
265 (let* ((next (component-last-block *current-component*))
266 (prev (block-prev next))
267 (new-block (make-block ctran)))
268 (setf (block-next new-block) next
269 (block-prev new-block) prev
270 (block-prev next) new-block
271 (block-next prev) new-block
272 (ctran-block ctran) new-block
273 (ctran-kind ctran) :block-start)
274 (aver (not (ctran-use ctran)))
277 (ctran-block ctran))))
279 ;;; Ensure that CTRAN is the start of a block so that the use set can
280 ;;; be freely manipulated.
281 (defun ensure-block-start (ctran)
282 (declare (type ctran ctran))
283 (let ((kind (ctran-kind ctran)))
287 (setf (ctran-block ctran)
288 (make-block-key :start ctran))
289 (setf (ctran-kind ctran) :block-start))
291 (node-ends-block (ctran-use ctran)))))
294 ;;; CTRAN must be the last ctran in an incomplete block; finish the
295 ;;; block and start a new one if necessary.
296 (defun start-block (ctran)
297 (declare (type ctran ctran))
298 (aver (not (ctran-next ctran)))
299 (ecase (ctran-kind ctran)
301 (let ((block (ctran-block ctran))
302 (node (ctran-use ctran)))
303 (aver (not (block-last block)))
305 (setf (block-last block) node)
306 (setf (node-next node) nil)
307 (setf (ctran-use ctran) nil)
308 (setf (ctran-kind ctran) :unused)
309 (setf (ctran-block ctran) nil)
310 (link-blocks block (ctran-starts-block ctran))))
315 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
316 ;;; call. First argument must be 'DUMMY, which will be replaced with
317 ;;; LVAR. In case of an ordinary call the function should not have
318 ;;; return type NIL. We create a new "filtered" lvar.
320 ;;; TODO: remove preconditions.
321 (defun filter-lvar (lvar form)
322 (declare (type lvar lvar) (type list form))
323 (let* ((dest (lvar-dest lvar))
324 (ctran (node-prev dest)))
325 (with-ir1-environment-from-node dest
327 (ensure-block-start ctran)
328 (let* ((old-block (ctran-block ctran))
329 (new-start (make-ctran))
330 (filtered-lvar (make-lvar))
331 (new-block (ctran-starts-block new-start)))
333 ;; Splice in the new block before DEST, giving the new block
334 ;; all of DEST's predecessors.
335 (dolist (block (block-pred old-block))
336 (change-block-successor block old-block new-block))
338 (ir1-convert new-start ctran filtered-lvar form)
340 ;; KLUDGE: Comments at the head of this function in CMU CL
341 ;; said that somewhere in here we
342 ;; Set the new block's start and end cleanups to the *start*
343 ;; cleanup of PREV's block. This overrides the incorrect
344 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
345 ;; Unfortunately I can't find any code which corresponds to this.
346 ;; Perhaps it was a stale comment? Or perhaps I just don't
347 ;; understand.. -- WHN 19990521
349 ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
350 ;; no LET conversion has been done yet.) The [mv-]combination
351 ;; code from the call in the form will be the use of the new
352 ;; check lvar. We substitute for the first argument of
354 (let* ((node (lvar-use filtered-lvar))
355 (args (basic-combination-args node))
356 (victim (first args)))
357 (aver (eq (constant-value (ref-leaf (lvar-use victim)))
360 (substitute-lvar filtered-lvar lvar)
361 (substitute-lvar lvar victim)
364 ;; Invoking local call analysis converts this call to a LET.
365 (locall-analyze-component *current-component*))))
368 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
369 (defun delete-filter (node lvar value)
370 (aver (eq (lvar-dest value) node))
371 (aver (eq (node-lvar node) lvar))
372 (cond (lvar (collect ((merges))
373 (when (return-p (lvar-dest lvar))
375 (when (and (basic-combination-p use)
376 (eq (basic-combination-kind use) :local))
378 (substitute-lvar-uses lvar value
379 (and lvar (eq (lvar-uses lvar) node)))
380 (%delete-lvar-use node)
383 (dolist (merge (merges))
384 (merge-tail-sets merge)))))
385 (t (flush-dest value)
386 (unlink-node node))))
388 ;;; Make a CAST and insert it into IR1 before node NEXT.
389 (defun insert-cast-before (next lvar type policy)
390 (declare (type node next) (type lvar lvar) (type ctype type))
391 (with-ir1-environment-from-node next
392 (let* ((ctran (node-prev next))
393 (cast (make-cast lvar type policy))
394 (internal-ctran (make-ctran)))
395 (setf (ctran-next ctran) cast
396 (node-prev cast) ctran)
397 (use-ctran cast internal-ctran)
398 (link-node-to-previous-ctran next internal-ctran)
399 (setf (lvar-dest lvar) cast)
400 (reoptimize-lvar lvar)
401 (when (return-p next)
402 (node-ends-block cast))
403 (setf (block-attributep (block-flags (node-block cast))
404 type-check type-asserted)
408 ;;;; miscellaneous shorthand functions
410 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
411 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
412 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
413 ;;; deleted, and then return its home.
414 (defun node-home-lambda (node)
415 (declare (type node node))
416 (do ((fun (lexenv-lambda (node-lexenv node))
417 (lexenv-lambda (lambda-call-lexenv fun))))
418 ((not (memq (functional-kind fun) '(:deleted :zombie)))
420 (when (eq (lambda-home fun) fun)
423 #!-sb-fluid (declaim (inline node-block))
424 (defun node-block (node)
425 (ctran-block (node-prev node)))
426 (declaim (ftype (sfunction (node) component) node-component))
427 (defun node-component (node)
428 (block-component (node-block node)))
429 (declaim (ftype (sfunction (node) physenv) node-physenv))
430 (defun node-physenv (node)
431 (lambda-physenv (node-home-lambda node)))
432 #!-sb-fluid (declaim (inline node-dest))
433 (defun node-dest (node)
434 (awhen (node-lvar node) (lvar-dest it)))
436 #!-sb-fluid (declaim (inline node-stack-allocate-p))
437 (defun node-stack-allocate-p (node)
438 (awhen (node-lvar node)
439 (lvar-dynamic-extent it)))
441 (defun flushable-combination-p (call)
442 (declare (type combination call))
443 (let ((kind (combination-kind call))
444 (info (combination-fun-info call)))
445 (when (and (eq kind :known) (fun-info-p info))
446 (let ((attr (fun-info-attributes info)))
447 (when (and (not (ir1-attributep attr call))
448 ;; FIXME: For now, don't consider potentially flushable
449 ;; calls flushable when they have the CALL attribute.
450 ;; Someday we should look at the functional args to
451 ;; determine if they have any side effects.
452 (if (policy call (= safety 3))
453 (ir1-attributep attr flushable)
454 (ir1-attributep attr unsafely-flushable)))
457 ;;;; DYNAMIC-EXTENT related
459 (defun lambda-var-original-name (leaf)
460 (let ((home (lambda-var-home leaf)))
461 (if (eq :external (functional-kind home))
462 (let* ((entry (functional-entry-fun home))
463 (p (1- (position leaf (lambda-vars home)))))
465 (if (optional-dispatch-p entry)
466 (elt (optional-dispatch-arglist entry) p)
467 (elt (lambda-vars entry) p))))
468 (leaf-debug-name leaf))))
470 (defun note-no-stack-allocation (lvar &key flush)
471 (do-uses (use (principal-lvar lvar))
473 ;; Don't complain about not being able to stack allocate constants.
474 (and (ref-p use) (constant-p (ref-leaf use)))
475 ;; If we're flushing, don't complain if we can flush the combination.
476 (and flush (combination-p use) (flushable-combination-p use))
477 ;; Don't report those with homes in :OPTIONAL -- we'd get doubled
479 (and (ref-p use) (lambda-var-p (ref-leaf use))
480 (eq :optional (lambda-kind (lambda-var-home (ref-leaf use))))))
481 ;; FIXME: For the first leg (lambda-bind (lambda-var-home ...))
482 ;; would be a far better description, but since we use
483 ;; *COMPILER-ERROR-CONTEXT* for muffling we can't -- as that node
484 ;; can have different handled conditions.
485 (let ((*compiler-error-context* use))
486 (if (and (ref-p use) (lambda-var-p (ref-leaf use)))
487 (compiler-notify "~@<could~2:I not stack allocate ~S in: ~S~:@>"
488 (lambda-var-original-name (ref-leaf use))
489 (find-original-source (node-source-path use)))
490 (compiler-notify "~@<could~2:I not stack allocate: ~S~:@>"
491 (find-original-source (node-source-path use))))))))
493 (defun use-good-for-dx-p (use dx &optional component)
494 ;; FIXME: Can casts point to LVARs in other components?
495 ;; RECHECK-DYNAMIC-EXTENT-LVARS assumes that they can't -- that is, that the
496 ;; PRINCIPAL-LVAR is always in the same component as the original one. It
497 ;; would be either good to have an explanation of why casts don't point
498 ;; across components, or an explanation of when they do it. ...in the
499 ;; meanwhile AVER that our assumption holds true.
500 (aver (or (not component) (eq component (node-component use))))
501 (or (dx-combination-p use dx)
503 (not (cast-type-check use))
504 (lvar-good-for-dx-p (cast-value use) dx component))
505 (and (trivial-lambda-var-ref-p use)
506 (let ((uses (lvar-uses (trivial-lambda-var-ref-lvar use))))
508 (lvar-good-for-dx-p (trivial-lambda-var-ref-lvar use) dx component))))))
510 (defun lvar-good-for-dx-p (lvar dx &optional component)
511 (let ((uses (lvar-uses lvar)))
515 (use-good-for-dx-p use dx component))
517 (use-good-for-dx-p uses dx component))))
519 (defun known-dx-combination-p (use dx)
520 (and (eq (combination-kind use) :known)
521 (let ((info (combination-fun-info use)))
522 (or (awhen (fun-info-stack-allocate-result info)
524 (awhen (fun-info-result-arg info)
525 (let ((args (combination-args use)))
526 (lvar-good-for-dx-p (if (zerop it)
531 (defun dx-combination-p (use dx)
532 (and (combination-p use)
534 ;; Known, and can do DX.
535 (known-dx-combination-p use dx)
536 ;; Possibly a not-yet-eliminated lambda which ends up returning the
537 ;; results of an actual known DX combination.
538 (let* ((fun (combination-fun use))
539 (ref (principal-lvar-use fun))
540 (clambda (when (ref-p ref)
542 (creturn (when (lambda-p clambda)
543 (lambda-return clambda)))
544 (result-use (when (return-p creturn)
545 (principal-lvar-use (return-result creturn)))))
546 ;; FIXME: We should be able to deal with multiple uses here as well.
547 (and (dx-combination-p result-use dx)
548 (combination-args-flow-cleanly-p use result-use dx))))))
550 (defun combination-args-flow-cleanly-p (combination1 combination2 dx)
551 (labels ((recurse (combination)
552 (or (eq combination combination2)
553 (if (known-dx-combination-p combination dx)
554 (let ((dest (lvar-dest (combination-lvar combination))))
555 (and (combination-p dest)
557 (let* ((fun1 (combination-fun combination))
558 (ref1 (principal-lvar-use fun1))
559 (clambda1 (when (ref-p ref1) (ref-leaf ref1))))
560 (when (lambda-p clambda1)
561 (dolist (var (lambda-vars clambda1) t)
562 (dolist (var-ref (lambda-var-refs var))
563 (let ((dest (lvar-dest (ref-lvar var-ref))))
564 (unless (and (combination-p dest) (recurse dest))
565 (return-from combination-args-flow-cleanly-p nil)))))))))))
566 (recurse combination1)))
568 (defun ref-good-for-dx-p (ref)
569 (let* ((lvar (ref-lvar ref))
570 (dest (when lvar (lvar-dest lvar))))
571 (and (combination-p dest)
572 (eq :known (combination-kind dest))
573 (awhen (combination-fun-info dest)
574 (or (ir1-attributep (fun-info-attributes it) dx-safe)
575 (and (not (combination-lvar dest))
576 (awhen (fun-info-result-arg it)
577 (eql lvar (nth it (combination-args dest))))))))))
579 (defun trivial-lambda-var-ref-p (use)
581 (let ((var (ref-leaf use)))
582 ;; lambda-var, no SETS, not explicitly indefinite-extent.
583 (when (and (lambda-var-p var) (not (lambda-var-sets var))
584 (neq :indefinite (lambda-var-extent var)))
585 (let ((home (lambda-var-home var))
586 (refs (lambda-var-refs var)))
587 ;; bound by a non-XEP system lambda, no other REFS that aren't
588 ;; DX-SAFE, or are result-args when the result is discarded.
589 (when (and (lambda-system-lambda-p home)
590 (neq :external (lambda-kind home))
592 (unless (or (eq use ref) (ref-good-for-dx-p ref))
594 ;; the LAMBDA this var is bound by has only a single REF, going
596 (let* ((lambda-refs (lambda-refs home))
597 (primary (car lambda-refs)))
599 (not (cdr lambda-refs))
600 (combination-p (lvar-dest (ref-lvar primary)))))))))))
602 (defun trivial-lambda-var-ref-lvar (use)
603 (let* ((this (ref-leaf use))
604 (fun (lambda-var-home this))
605 (vars (lambda-vars fun))
606 (combination (lvar-dest (ref-lvar (car (lambda-refs fun)))))
607 (args (combination-args combination)))
608 (aver (= (length vars) (length args)))
609 (loop for var in vars
614 ;;; This needs to play nice with LVAR-GOOD-FOR-DX-P and friends.
615 (defun handle-nested-dynamic-extent-lvars (dx lvar &optional recheck-component)
616 (let ((uses (lvar-uses lvar)))
617 ;; DX value generators must end their blocks: see UPDATE-UVL-LIVE-SETS.
618 ;; Uses of mupltiple-use LVARs already end their blocks, so we just need
619 ;; to process uses of single-use LVARs.
621 (node-ends-block uses))
622 ;; If this LVAR's USE is good for DX, it is either a CAST, or it
623 ;; must be a regular combination whose arguments are potentially DX as well.
624 (flet ((recurse (use)
627 (handle-nested-dynamic-extent-lvars
628 dx (cast-value use) recheck-component))
630 (loop for arg in (combination-args use)
631 ;; deleted args show up as NIL here
633 (lvar-good-for-dx-p arg dx recheck-component))
634 append (handle-nested-dynamic-extent-lvars
635 dx arg recheck-component)))
637 (let* ((other (trivial-lambda-var-ref-lvar use)))
638 (unless (eq other lvar)
639 (handle-nested-dynamic-extent-lvars
640 dx other recheck-component)))))))
643 (loop for use in uses
644 when (use-good-for-dx-p use dx recheck-component)
646 (when (use-good-for-dx-p uses dx recheck-component)
651 (declaim (inline block-to-be-deleted-p))
652 (defun block-to-be-deleted-p (block)
653 (or (block-delete-p block)
654 (eq (functional-kind (block-home-lambda block)) :deleted)))
656 ;;; Checks whether NODE is in a block to be deleted
657 (declaim (inline node-to-be-deleted-p))
658 (defun node-to-be-deleted-p (node)
659 (block-to-be-deleted-p (node-block node)))
661 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
662 (defun lambda-block (clambda)
663 (node-block (lambda-bind clambda)))
664 (declaim (ftype (sfunction (clambda) component) lambda-component))
665 (defun lambda-component (clambda)
666 (block-component (lambda-block clambda)))
668 (declaim (ftype (sfunction (cblock) node) block-start-node))
669 (defun block-start-node (block)
670 (ctran-next (block-start block)))
672 ;;; Return the enclosing cleanup for environment of the first or last
674 (defun block-start-cleanup (block)
675 (node-enclosing-cleanup (block-start-node block)))
676 (defun block-end-cleanup (block)
677 (node-enclosing-cleanup (block-last block)))
679 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
680 ;;; if there is none.
682 ;;; There can legitimately be no home lambda in dead code early in the
683 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
684 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
685 ;;; where the block is just a placeholder during parsing and doesn't
686 ;;; actually correspond to code which will be written anywhere.
687 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
688 (defun block-home-lambda-or-null (block)
689 (if (node-p (block-last block))
690 ;; This is the old CMU CL way of doing it.
691 (node-home-lambda (block-last block))
692 ;; Now that SBCL uses this operation more aggressively than CMU
693 ;; CL did, the old CMU CL way of doing it can fail in two ways.
694 ;; 1. It can fail in a few cases even when a meaningful home
695 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
697 ;; 2. It can fail when converting a form which is born orphaned
698 ;; so that it never had a meaningful home lambda, e.g. a form
699 ;; which follows a RETURN-FROM or GO form.
700 (let ((pred-list (block-pred block)))
701 ;; To deal with case 1, we reason that
702 ;; previous-in-target-execution-order blocks should be in the
703 ;; same lambda, and that they seem in practice to be
704 ;; previous-in-compilation-order blocks too, so we look back
705 ;; to find one which is sufficiently initialized to tell us
706 ;; what the home lambda is.
708 ;; We could get fancy about this, flooding through the
709 ;; graph of all the previous blocks, but in practice it
710 ;; seems to work just to grab the first previous block and
712 (node-home-lambda (block-last (first pred-list)))
713 ;; In case 2, we end up with an empty PRED-LIST and
714 ;; have to punt: There's no home lambda.
717 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
718 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
719 (defun block-home-lambda (block)
720 (block-home-lambda-or-null block))
722 ;;; Return the IR1 physical environment for BLOCK.
723 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
724 (defun block-physenv (block)
725 (lambda-physenv (block-home-lambda block)))
727 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
728 ;;; of its original source's top level form in its compilation unit.
729 (defun source-path-tlf-number (path)
730 (declare (list path))
733 ;;; Return the (reversed) list for the PATH in the original source
734 ;;; (with the Top Level Form number last).
735 (defun source-path-original-source (path)
736 (declare (list path) (inline member))
737 (cddr (member 'original-source-start path :test #'eq)))
739 ;;; Return the Form Number of PATH's original source inside the Top
740 ;;; Level Form that contains it. This is determined by the order that
741 ;;; we walk the subforms of the top level source form.
742 (defun source-path-form-number (path)
743 (declare (list path) (inline member))
744 (cadr (member 'original-source-start path :test #'eq)))
746 ;;; Return a list of all the enclosing forms not in the original
747 ;;; source that converted to get to this form, with the immediate
748 ;;; source for node at the start of the list.
749 (defun source-path-forms (path)
750 (subseq path 0 (position 'original-source-start path)))
752 ;;; Return the innermost source form for NODE.
753 (defun node-source-form (node)
754 (declare (type node node))
755 (let* ((path (node-source-path node))
756 (forms (source-path-forms path)))
759 (values (find-original-source path)))))
761 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
763 (defun lvar-source (lvar)
764 (let ((use (lvar-uses lvar)))
767 (values (node-source-form use) t))))
769 ;;; Return the unique node, delivering a value to LVAR.
770 #!-sb-fluid (declaim (inline lvar-use))
771 (defun lvar-use (lvar)
772 (the (not list) (lvar-uses lvar)))
774 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
775 (defun lvar-has-single-use-p (lvar)
776 (typep (lvar-uses lvar) '(not list)))
778 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
779 (declaim (ftype (sfunction (ctran) (or clambda null))
780 ctran-home-lambda-or-null))
781 (defun ctran-home-lambda-or-null (ctran)
782 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
783 ;; implementation might not be quite right, or might be uglier than
784 ;; necessary. It appears that the original Python never found a need
785 ;; to do this operation. The obvious things based on
786 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
787 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
788 ;; generalize it enough to grovel harder when the simple CMU CL
789 ;; approach fails, and furthermore realize that in some exceptional
790 ;; cases it might return NIL. -- WHN 2001-12-04
791 (cond ((ctran-use ctran)
792 (node-home-lambda (ctran-use ctran)))
794 (block-home-lambda-or-null (ctran-block ctran)))
796 (bug "confused about home lambda for ~S" ctran))))
798 ;;; Return the LAMBDA that is CTRAN's home.
799 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
800 (defun ctran-home-lambda (ctran)
801 (ctran-home-lambda-or-null ctran))
803 (declaim (inline cast-single-value-p))
804 (defun cast-single-value-p (cast)
805 (not (values-type-p (cast-asserted-type cast))))
807 #!-sb-fluid (declaim (inline lvar-single-value-p))
808 (defun lvar-single-value-p (lvar)
810 (let ((dest (lvar-dest lvar)))
815 (eq (basic-combination-fun dest) lvar))
818 (declare (notinline lvar-single-value-p))
819 (and (cast-single-value-p dest)
820 (lvar-single-value-p (node-lvar dest)))))
824 (defun principal-lvar-end (lvar)
825 (loop for prev = lvar then (node-lvar dest)
826 for dest = (and prev (lvar-dest prev))
828 finally (return (values dest prev))))
830 (defun principal-lvar-single-valuify (lvar)
831 (loop for prev = lvar then (node-lvar dest)
832 for dest = (and prev (lvar-dest prev))
834 do (setf (node-derived-type dest)
835 (make-short-values-type (list (single-value-type
836 (node-derived-type dest)))))
837 (reoptimize-lvar prev)))
839 ;;; Return a new LEXENV just like DEFAULT except for the specified
840 ;;; slot values. Values for the alist slots are NCONCed to the
841 ;;; beginning of the current value, rather than replacing it entirely.
842 (defun make-lexenv (&key (default *lexenv*)
843 funs vars blocks tags
845 (lambda (lexenv-lambda default))
846 (cleanup (lexenv-cleanup default))
847 (handled-conditions (lexenv-handled-conditions default))
848 (disabled-package-locks
849 (lexenv-disabled-package-locks default))
850 (policy (lexenv-policy default))
851 (user-data (lexenv-user-data default)))
852 (macrolet ((frob (var slot)
853 `(let ((old (,slot default)))
857 (internal-make-lexenv
858 (frob funs lexenv-funs)
859 (frob vars lexenv-vars)
860 (frob blocks lexenv-blocks)
861 (frob tags lexenv-tags)
862 (frob type-restrictions lexenv-type-restrictions)
864 cleanup handled-conditions disabled-package-locks
868 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
870 (defun make-restricted-lexenv (lexenv)
871 (flet ((fun-good-p (fun)
872 (destructuring-bind (name . thing) fun
873 (declare (ignore name))
877 (cons (aver (eq (car thing) 'macro))
880 (destructuring-bind (name . thing) var
881 (declare (ignore name))
883 ;; The evaluator will mark lexicals with :BOGUS when it
884 ;; translates an interpreter lexenv to a compiler
886 ((or leaf #!+sb-eval (member :bogus)) nil)
887 (cons (aver (eq (car thing) 'macro))
889 (heap-alien-info nil)))))
890 (internal-make-lexenv
891 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
892 (remove-if-not #'var-good-p (lexenv-vars lexenv))
895 (lexenv-type-restrictions lexenv) ; XXX
898 (lexenv-handled-conditions lexenv)
899 (lexenv-disabled-package-locks lexenv)
900 (lexenv-policy lexenv)
901 (lexenv-user-data lexenv))))
903 ;;;; flow/DFO/component hackery
905 ;;; Join BLOCK1 and BLOCK2.
906 (defun link-blocks (block1 block2)
907 (declare (type cblock block1 block2))
908 (setf (block-succ block1)
909 (if (block-succ block1)
910 (%link-blocks block1 block2)
912 (push block1 (block-pred block2))
914 (defun %link-blocks (block1 block2)
915 (declare (type cblock block1 block2))
916 (let ((succ1 (block-succ block1)))
917 (aver (not (memq block2 succ1)))
918 (cons block2 succ1)))
920 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
921 ;;; this leaves a successor with a single predecessor that ends in an
922 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
923 ;;; now be able to be propagated to the successor.
924 (defun unlink-blocks (block1 block2)
925 (declare (type cblock block1 block2))
926 (let ((succ1 (block-succ block1)))
927 (if (eq block2 (car succ1))
928 (setf (block-succ block1) (cdr succ1))
929 (do ((succ (cdr succ1) (cdr succ))
931 ((eq (car succ) block2)
932 (setf (cdr prev) (cdr succ)))
935 (let ((new-pred (delq block1 (block-pred block2))))
936 (setf (block-pred block2) new-pred)
937 (when (singleton-p new-pred)
938 (let ((pred-block (first new-pred)))
939 (when (if-p (block-last pred-block))
940 (setf (block-test-modified pred-block) t)))))
943 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
944 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
945 ;;; consequent/alternative blocks to point to NEW. We also set
946 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
947 ;;; the new successor.
948 (defun change-block-successor (block old new)
949 (declare (type cblock new old block))
950 (unlink-blocks block old)
951 (let ((last (block-last block))
952 (comp (block-component block)))
953 (setf (component-reanalyze comp) t)
956 (setf (block-test-modified block) t)
957 (let* ((succ-left (block-succ block))
958 (new (if (and (eq new (component-tail comp))
962 (unless (memq new succ-left)
963 (link-blocks block new))
964 (macrolet ((frob (slot)
965 `(when (eq (,slot last) old)
966 (setf (,slot last) new))))
968 (frob if-alternative)
969 (when (eq (if-consequent last)
970 (if-alternative last))
971 (reoptimize-component (block-component block) :maybe)))))
973 (unless (memq new (block-succ block))
974 (link-blocks block new)))))
978 ;;; Unlink a block from the next/prev chain. We also null out the
980 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
981 (defun remove-from-dfo (block)
982 (let ((next (block-next block))
983 (prev (block-prev block)))
984 (setf (block-component block) nil)
985 (setf (block-next prev) next)
986 (setf (block-prev next) prev))
989 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
990 ;;; COMPONENT to be the same as for AFTER.
991 (defun add-to-dfo (block after)
992 (declare (type cblock block after))
993 (let ((next (block-next after))
994 (comp (block-component after)))
995 (aver (not (eq (component-kind comp) :deleted)))
996 (setf (block-component block) comp)
997 (setf (block-next after) block)
998 (setf (block-prev block) after)
999 (setf (block-next block) next)
1000 (setf (block-prev next) block))
1003 ;;; List all NLX-INFOs which BLOCK can exit to.
1005 ;;; We hope that no cleanup actions are performed in the middle of
1006 ;;; BLOCK, so it is enough to look only at cleanups in the block
1007 ;;; end. The tricky thing is a special cleanup block; all its nodes
1008 ;;; have the same cleanup info, corresponding to the start, so the
1009 ;;; same approach returns safe result.
1010 (defun map-block-nlxes (fun block &optional dx-cleanup-fun)
1011 (loop for cleanup = (block-end-cleanup block)
1012 then (node-enclosing-cleanup (cleanup-mess-up cleanup))
1014 do (let ((mess-up (cleanup-mess-up cleanup)))
1015 (case (cleanup-kind cleanup)
1017 (aver (entry-p mess-up))
1018 (loop for exit in (entry-exits mess-up)
1019 for nlx-info = (exit-nlx-info exit)
1020 do (funcall fun nlx-info)))
1021 ((:catch :unwind-protect)
1022 (aver (combination-p mess-up))
1023 (let* ((arg-lvar (first (basic-combination-args mess-up)))
1024 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
1025 (funcall fun nlx-info)))
1027 (when dx-cleanup-fun
1028 (funcall dx-cleanup-fun cleanup)))))))
1030 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
1031 ;;; the head and tail which are set to T.
1032 (declaim (ftype (sfunction (component) (values)) clear-flags))
1033 (defun clear-flags (component)
1034 (let ((head (component-head component))
1035 (tail (component-tail component)))
1036 (setf (block-flag head) t)
1037 (setf (block-flag tail) t)
1038 (do-blocks (block component)
1039 (setf (block-flag block) nil)))
1042 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
1043 ;;; true in the head and tail blocks.
1044 (declaim (ftype (sfunction () component) make-empty-component))
1045 (defun make-empty-component ()
1046 (let* ((head (make-block-key :start nil :component nil))
1047 (tail (make-block-key :start nil :component nil))
1048 (res (make-component head tail)))
1049 (setf (block-flag head) t)
1050 (setf (block-flag tail) t)
1051 (setf (block-component head) res)
1052 (setf (block-component tail) res)
1053 (setf (block-next head) tail)
1054 (setf (block-prev tail) head)
1057 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
1058 ;;; The new block is added to the DFO immediately following NODE's block.
1059 (defun node-ends-block (node)
1060 (declare (type node node))
1061 (let* ((block (node-block node))
1062 (start (node-next node))
1063 (last (block-last block)))
1064 (check-type last node)
1065 (unless (eq last node)
1066 (aver (and (eq (ctran-kind start) :inside-block)
1067 (not (block-delete-p block))))
1068 (let* ((succ (block-succ block))
1070 (make-block-key :start start
1071 :component (block-component block)
1072 :succ succ :last last)))
1073 (setf (ctran-kind start) :block-start)
1074 (setf (ctran-use start) nil)
1075 (setf (block-last block) node)
1076 (setf (node-next node) nil)
1078 (setf (block-pred b)
1079 (cons new-block (remove block (block-pred b)))))
1080 (setf (block-succ block) ())
1081 (link-blocks block new-block)
1082 (add-to-dfo new-block block)
1083 (setf (component-reanalyze (block-component block)) t)
1085 (do ((ctran start (node-next (ctran-next ctran))))
1087 (setf (ctran-block ctran) new-block))
1089 (setf (block-type-asserted block) t)
1090 (setf (block-test-modified block) t))))
1095 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
1096 (defun delete-lambda-var (leaf)
1097 (declare (type lambda-var leaf))
1099 (setf (lambda-var-deleted leaf) t)
1100 ;; Iterate over all local calls flushing the corresponding argument,
1101 ;; allowing the computation of the argument to be deleted. We also
1102 ;; mark the LET for reoptimization, since it may be that we have
1103 ;; deleted its last variable.
1104 (let* ((fun (lambda-var-home leaf))
1105 (n (position leaf (lambda-vars fun))))
1106 (dolist (ref (leaf-refs fun))
1107 (let* ((lvar (node-lvar ref))
1108 (dest (and lvar (lvar-dest lvar))))
1109 (when (and (combination-p dest)
1110 (eq (basic-combination-fun dest) lvar)
1111 (eq (basic-combination-kind dest) :local))
1112 (let* ((args (basic-combination-args dest))
1114 (reoptimize-lvar arg)
1116 (setf (elt args n) nil))))))
1118 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
1119 ;; too much difficulty, since we can efficiently implement
1120 ;; write-only variables. We iterate over the SETs, marking their
1121 ;; blocks for dead code flushing, since we can delete SETs whose
1123 (dolist (set (lambda-var-sets leaf))
1124 (setf (block-flush-p (node-block set)) t))
1128 ;;; Note that something interesting has happened to VAR.
1129 (defun reoptimize-lambda-var (var)
1130 (declare (type lambda-var var))
1131 (let ((fun (lambda-var-home var)))
1132 ;; We only deal with LET variables, marking the corresponding
1133 ;; initial value arg as needing to be reoptimized.
1134 (when (and (eq (functional-kind fun) :let)
1136 (do ((args (basic-combination-args
1137 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1139 (vars (lambda-vars fun) (cdr vars)))
1140 ((eq (car vars) var)
1141 (reoptimize-lvar (car args))))))
1144 ;;; Delete a function that has no references. This need only be called
1145 ;;; on functions that never had any references, since otherwise
1146 ;;; DELETE-REF will handle the deletion.
1147 (defun delete-functional (fun)
1148 (aver (and (null (leaf-refs fun))
1149 (not (functional-entry-fun fun))))
1151 (optional-dispatch (delete-optional-dispatch fun))
1152 (clambda (delete-lambda fun)))
1155 ;;; Deal with deleting the last reference to a CLAMBDA, which means
1156 ;;; that the lambda is unreachable, so that its body may be
1157 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
1158 ;;; IR1-OPTIMIZE to delete its blocks.
1159 (defun delete-lambda (clambda)
1160 (declare (type clambda clambda))
1161 (let ((original-kind (functional-kind clambda))
1162 (bind (lambda-bind clambda)))
1163 (aver (not (member original-kind '(:deleted :toplevel))))
1164 (aver (not (functional-has-external-references-p clambda)))
1165 (aver (or (eq original-kind :zombie) bind))
1166 (setf (functional-kind clambda) :deleted)
1167 (setf (lambda-bind clambda) nil)
1169 (labels ((delete-children (lambda)
1170 (dolist (child (lambda-children lambda))
1171 (cond ((eq (functional-kind child) :deleted)
1172 (delete-children child))
1174 (delete-lambda child))))
1175 (setf (lambda-children lambda) nil)
1176 (setf (lambda-parent lambda) nil)))
1177 (delete-children clambda))
1179 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
1180 ;; that we're using the old value of the KIND slot, not the
1181 ;; current slot value, which has now been set to :DELETED.)
1184 ((:let :mv-let :assignment)
1185 (let ((bind-block (node-block bind)))
1186 (mark-for-deletion bind-block))
1187 (let ((home (lambda-home clambda)))
1188 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1189 ;; KLUDGE: In presence of NLEs we cannot always understand that
1190 ;; LET's BIND dominates its body [for a LET "its" body is not
1191 ;; quite its]; let's delete too dangerous for IR2 stuff. --
1193 (dolist (var (lambda-vars clambda))
1194 (flet ((delete-node (node)
1195 (mark-for-deletion (node-block node))))
1196 (mapc #'delete-node (leaf-refs var))
1197 (mapc #'delete-node (lambda-var-sets var)))))
1199 ;; Function has no reachable references.
1200 (dolist (ref (lambda-refs clambda))
1201 (mark-for-deletion (node-block ref)))
1202 ;; If the function isn't a LET, we unlink the function head
1203 ;; and tail from the component head and tail to indicate that
1204 ;; the code is unreachable. We also delete the function from
1205 ;; COMPONENT-LAMBDAS (it won't be there before local call
1206 ;; analysis, but no matter.) If the lambda was never
1207 ;; referenced, we give a note.
1208 (let* ((bind-block (node-block bind))
1209 (component (block-component bind-block))
1210 (return (lambda-return clambda))
1211 (return-block (and return (node-block return))))
1212 (unless (leaf-ever-used clambda)
1213 (let ((*compiler-error-context* bind))
1214 (compiler-notify 'code-deletion-note
1215 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
1216 :format-arguments (list (leaf-debug-name clambda)))))
1217 (unless (block-delete-p bind-block)
1218 (unlink-blocks (component-head component) bind-block))
1219 (when (and return-block (not (block-delete-p return-block)))
1220 (mark-for-deletion return-block)
1221 (unlink-blocks return-block (component-tail component)))
1222 (setf (component-reanalyze component) t)
1223 (let ((tails (lambda-tail-set clambda)))
1224 (setf (tail-set-funs tails)
1225 (delete clambda (tail-set-funs tails)))
1226 (setf (lambda-tail-set clambda) nil))
1227 (setf (component-lambdas component)
1228 (delq clambda (component-lambdas component))))))
1230 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
1231 ;; ENTRY-FUN so that people will know that it is not an entry
1233 (when (eq original-kind :external)
1234 (let ((fun (functional-entry-fun clambda)))
1235 (setf (functional-entry-fun fun) nil)
1236 (when (optional-dispatch-p fun)
1237 (delete-optional-dispatch fun)))))
1241 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
1242 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
1243 ;;; is used both before and after local call analysis. Afterward, all
1244 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
1245 ;;; to the XEP, leaving it with no references at all. So we look at
1246 ;;; the XEP to see whether an optional-dispatch is still really being
1247 ;;; used. But before local call analysis, there are no XEPs, and all
1248 ;;; references are direct.
1250 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
1251 ;;; entry-points, making them be normal lambdas, and then deleting the
1252 ;;; ones with no references. This deletes any e-p lambdas that were
1253 ;;; either never referenced, or couldn't be deleted when the last
1254 ;;; reference was deleted (due to their :OPTIONAL kind.)
1256 ;;; Note that the last optional entry point may alias the main entry,
1257 ;;; so when we process the main entry, its KIND may have been changed
1258 ;;; to NIL or even converted to a LETlike value.
1259 (defun delete-optional-dispatch (leaf)
1260 (declare (type optional-dispatch leaf))
1261 (let ((entry (functional-entry-fun leaf)))
1262 (unless (and entry (leaf-refs entry))
1263 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
1264 (setf (functional-kind leaf) :deleted)
1267 (unless (eq (functional-kind fun) :deleted)
1268 (aver (eq (functional-kind fun) :optional))
1269 (setf (functional-kind fun) nil)
1270 (let ((refs (leaf-refs fun)))
1272 (delete-lambda fun))
1274 (or (maybe-let-convert fun)
1275 (maybe-convert-to-assignment fun)))
1277 (maybe-convert-to-assignment fun)))))))
1279 (dolist (ep (optional-dispatch-entry-points leaf))
1280 (when (promise-ready-p ep)
1282 (when (optional-dispatch-more-entry leaf)
1283 (frob (optional-dispatch-more-entry leaf)))
1284 (let ((main (optional-dispatch-main-entry leaf)))
1286 (setf (functional-entry-fun entry) main)
1287 (setf (functional-entry-fun main) entry))
1288 (when (eq (functional-kind main) :optional)
1293 (defun note-local-functional (fun)
1294 (declare (type functional fun))
1295 (when (and (leaf-has-source-name-p fun)
1296 (eq (leaf-source-name fun) (functional-debug-name fun)))
1297 (let ((name (leaf-source-name fun)))
1298 (let ((defined-fun (gethash name *free-funs*)))
1299 (when (and defined-fun
1300 (defined-fun-p defined-fun)
1301 (eq (defined-fun-functional defined-fun) fun))
1302 (remhash name *free-funs*))))))
1304 ;;; Return functional for DEFINED-FUN which has been converted in policy
1305 ;;; corresponding to the current one, or NIL if no such functional exists.
1307 ;;; Also check that the parent of the functional is visible in the current
1309 (defun defined-fun-functional (defined-fun)
1310 (let ((functionals (defined-fun-functionals defined-fun)))
1312 (let* ((sample (car functionals))
1313 (there (lambda-parent (if (lambda-p sample)
1315 (optional-dispatch-main-entry sample)))))
1317 (labels ((lookup (here)
1318 (unless (eq here there)
1320 (lookup (lambda-parent here))
1321 ;; We looked up all the way up, and didn't find the parent
1322 ;; of the functional -- therefore it is nested in a lambda
1323 ;; we don't see, so return nil.
1324 (return-from defined-fun-functional nil)))))
1325 (lookup (lexenv-lambda *lexenv*)))))
1326 ;; Now find a functional whose policy matches the current one, if we already
1328 (let ((policy (lexenv-%policy *lexenv*)))
1329 (dolist (functional functionals)
1330 (when (equal policy (lexenv-%policy (functional-lexenv functional)))
1331 (return functional)))))))
1333 ;;; Do stuff to delete the semantic attachments of a REF node. When
1334 ;;; this leaves zero or one reference, we do a type dispatch off of
1335 ;;; the leaf to determine if a special action is appropriate.
1336 (defun delete-ref (ref)
1337 (declare (type ref ref))
1338 (let* ((leaf (ref-leaf ref))
1339 (refs (delq ref (leaf-refs leaf))))
1340 (setf (leaf-refs leaf) refs)
1345 (delete-lambda-var leaf))
1347 (ecase (functional-kind leaf)
1348 ((nil :let :mv-let :assignment :escape :cleanup)
1349 (aver (null (functional-entry-fun leaf)))
1350 (delete-lambda leaf))
1352 (unless (functional-has-external-references-p leaf)
1353 (delete-lambda leaf)))
1354 ((:deleted :zombie :optional))))
1356 (unless (eq (functional-kind leaf) :deleted)
1357 (delete-optional-dispatch leaf)))))
1360 (clambda (or (maybe-let-convert leaf)
1361 (maybe-convert-to-assignment leaf)))
1362 (lambda-var (reoptimize-lambda-var leaf))))
1365 (clambda (maybe-convert-to-assignment leaf))))))
1369 ;;; This function is called by people who delete nodes; it provides a
1370 ;;; way to indicate that the value of a lvar is no longer used. We
1371 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1372 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1373 (defun flush-dest (lvar)
1374 (declare (type (or lvar null) lvar))
1376 (when (lvar-dynamic-extent lvar)
1377 (note-no-stack-allocation lvar :flush t))
1378 (setf (lvar-dest lvar) nil)
1379 (flush-lvar-externally-checkable-type lvar)
1381 (let ((prev (node-prev use)))
1382 (let ((block (ctran-block prev)))
1383 (reoptimize-component (block-component block) t)
1384 (setf (block-attributep (block-flags block)
1385 flush-p type-asserted type-check)
1387 (setf (node-lvar use) nil))
1388 (setf (lvar-uses lvar) nil))
1391 (defun delete-dest (lvar)
1393 (let* ((dest (lvar-dest lvar))
1394 (prev (node-prev dest)))
1395 (let ((block (ctran-block prev)))
1396 (unless (block-delete-p block)
1397 (mark-for-deletion block))))))
1399 ;;; Queue the block for deletion
1400 (defun delete-block-lazily (block)
1401 (declare (type cblock block))
1402 (unless (block-delete-p block)
1403 (setf (block-delete-p block) t)
1404 (push block (component-delete-blocks (block-component block)))))
1406 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1407 ;;; blocks with the DELETE-P flag.
1408 (defun mark-for-deletion (block)
1409 (declare (type cblock block))
1410 (let* ((component (block-component block))
1411 (head (component-head component)))
1412 (labels ((helper (block)
1413 (delete-block-lazily block)
1414 (dolist (pred (block-pred block))
1415 (unless (or (block-delete-p pred)
1418 (unless (block-delete-p block)
1420 (setf (component-reanalyze component) t))))
1423 ;;; This function does what is necessary to eliminate the code in it
1424 ;;; from the IR1 representation. This involves unlinking it from its
1425 ;;; predecessors and successors and deleting various node-specific
1426 ;;; semantic information. BLOCK must be already removed from
1427 ;;; COMPONENT-DELETE-BLOCKS.
1428 (defun delete-block (block &optional silent)
1429 (declare (type cblock block))
1430 (aver (block-component block)) ; else block is already deleted!
1431 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1433 (note-block-deletion block))
1434 (setf (block-delete-p block) t)
1436 (dolist (b (block-pred block))
1437 (unlink-blocks b block)
1438 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1439 ;; broken when successors were deleted without setting the
1440 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1441 ;; doesn't happen again.
1442 (aver (not (and (null (block-succ b))
1443 (not (block-delete-p b))
1444 (not (eq b (component-head (block-component b))))))))
1445 (dolist (b (block-succ block))
1446 (unlink-blocks block b))
1448 (do-nodes-carefully (node block)
1449 (when (valued-node-p node)
1450 (delete-lvar-use node))
1452 (ref (delete-ref node))
1453 (cif (flush-dest (if-test node)))
1454 ;; The next two cases serve to maintain the invariant that a LET
1455 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1456 ;; the lambda whenever we delete any of these, but we must be
1457 ;; careful that this LET has not already been partially deleted.
1459 (when (and (eq (basic-combination-kind node) :local)
1460 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1461 (lvar-uses (basic-combination-fun node)))
1462 (let ((fun (combination-lambda node)))
1463 ;; If our REF was the second-to-last ref, and has been
1464 ;; deleted, then FUN may be a LET for some other
1466 (when (and (functional-letlike-p fun)
1467 (eq (let-combination fun) node))
1468 (delete-lambda fun))))
1469 (flush-dest (basic-combination-fun node))
1470 (dolist (arg (basic-combination-args node))
1471 (when arg (flush-dest arg))))
1473 (let ((lambda (bind-lambda node)))
1474 (unless (eq (functional-kind lambda) :deleted)
1475 (delete-lambda lambda))))
1477 (let ((value (exit-value node))
1478 (entry (exit-entry node)))
1482 (setf (entry-exits entry)
1483 (delq node (entry-exits entry))))))
1485 (dolist (exit (entry-exits node))
1486 (mark-for-deletion (node-block exit)))
1487 (let ((home (node-home-lambda node)))
1488 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1490 (flush-dest (return-result node))
1491 (delete-return node))
1493 (flush-dest (set-value node))
1494 (let ((var (set-var node)))
1495 (setf (basic-var-sets var)
1496 (delete node (basic-var-sets var)))))
1498 (flush-dest (cast-value node)))))
1500 (remove-from-dfo block)
1503 ;;; Do stuff to indicate that the return node NODE is being deleted.
1504 (defun delete-return (node)
1505 (declare (type creturn node))
1506 (let* ((fun (return-lambda node))
1507 (tail-set (lambda-tail-set fun)))
1508 (aver (lambda-return fun))
1509 (setf (lambda-return fun) nil)
1510 (when (and tail-set (not (find-if #'lambda-return
1511 (tail-set-funs tail-set))))
1512 (setf (tail-set-type tail-set) *empty-type*)))
1515 ;;; If any of the VARS in FUN was never referenced and was not
1516 ;;; declared IGNORE, then complain.
1517 (defun note-unreferenced-vars (fun)
1518 (declare (type clambda fun))
1519 (dolist (var (lambda-vars fun))
1520 (unless (or (leaf-ever-used var)
1521 (lambda-var-ignorep var))
1522 (let ((*compiler-error-context* (lambda-bind fun)))
1523 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1524 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1525 ;; requires this to be no more than a STYLE-WARNING.
1527 (compiler-style-warn "The variable ~S is defined but never used."
1528 (leaf-debug-name var))
1529 ;; There's no reason to accept this kind of equivocation
1530 ;; when compiling our own code, though.
1532 (warn "The variable ~S is defined but never used."
1533 (leaf-debug-name var)))
1534 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1537 (defvar *deletion-ignored-objects* '(t nil))
1539 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1540 ;;; our recursion so that we don't get lost in circular structures. We
1541 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1542 ;;; function referencess with variables), and we also ignore anything
1544 (defun present-in-form (obj form depth)
1545 (declare (type (integer 0 20) depth))
1546 (cond ((= depth 20) nil)
1550 (let ((first (car form))
1552 (if (member first '(quote function))
1554 (or (and (not (symbolp first))
1555 (present-in-form obj first depth))
1556 (do ((l (cdr form) (cdr l))
1558 ((or (atom l) (> n 100))
1560 (declare (fixnum n))
1561 (when (present-in-form obj (car l) depth)
1564 ;;; This function is called on a block immediately before we delete
1565 ;;; it. We check to see whether any of the code about to die appeared
1566 ;;; in the original source, and emit a note if so.
1568 ;;; If the block was in a lambda is now deleted, then we ignore the
1569 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1570 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1571 ;;; reasonable for a function to not return, and there is a different
1572 ;;; note for that case anyway.
1574 ;;; If the actual source is an atom, then we use a bunch of heuristics
1575 ;;; to guess whether this reference really appeared in the original
1577 ;;; -- If a symbol, it must be interned and not a keyword.
1578 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1579 ;;; or a character.)
1580 ;;; -- The atom must be "present" in the original source form, and
1581 ;;; present in all intervening actual source forms.
1582 (defun note-block-deletion (block)
1583 (let ((home (block-home-lambda block)))
1584 (unless (eq (functional-kind home) :deleted)
1585 (do-nodes (node nil block)
1586 (let* ((path (node-source-path node))
1587 (first (first path)))
1588 (when (or (eq first 'original-source-start)
1590 (or (not (symbolp first))
1591 (let ((pkg (symbol-package first)))
1593 (not (eq pkg (symbol-package :end))))))
1594 (not (member first *deletion-ignored-objects*))
1595 (not (typep first '(or fixnum character)))
1597 (present-in-form first x 0))
1598 (source-path-forms path))
1599 (present-in-form first (find-original-source path)
1601 (unless (return-p node)
1602 (let ((*compiler-error-context* node))
1603 (compiler-notify 'code-deletion-note
1604 :format-control "deleting unreachable code"
1605 :format-arguments nil)))
1609 ;;; Delete a node from a block, deleting the block if there are no
1610 ;;; nodes left. We remove the node from the uses of its LVAR.
1612 ;;; If the node is the last node, there must be exactly one successor.
1613 ;;; We link all of our precedessors to the successor and unlink the
1614 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1615 ;;; left, and the block is a successor of itself, then we replace the
1616 ;;; only node with a degenerate exit node. This provides a way to
1617 ;;; represent the bodyless infinite loop, given the prohibition on
1618 ;;; empty blocks in IR1.
1619 (defun unlink-node (node)
1620 (declare (type node node))
1621 (when (valued-node-p node)
1622 (delete-lvar-use node))
1624 (let* ((ctran (node-next node))
1625 (next (and ctran (ctran-next ctran)))
1626 (prev (node-prev node))
1627 (block (ctran-block prev))
1628 (prev-kind (ctran-kind prev))
1629 (last (block-last block)))
1631 (setf (block-type-asserted block) t)
1632 (setf (block-test-modified block) t)
1634 (cond ((or (eq prev-kind :inside-block)
1635 (and (eq prev-kind :block-start)
1636 (not (eq node last))))
1637 (cond ((eq node last)
1638 (setf (block-last block) (ctran-use prev))
1639 (setf (node-next (ctran-use prev)) nil))
1641 (setf (ctran-next prev) next)
1642 (setf (node-prev next) prev)
1643 (when (if-p next) ; AOP wanted
1644 (reoptimize-lvar (if-test next)))))
1645 (setf (node-prev node) nil)
1648 (aver (eq prev-kind :block-start))
1649 (aver (eq node last))
1650 (let* ((succ (block-succ block))
1651 (next (first succ)))
1652 (aver (singleton-p succ))
1654 ((eq block (first succ))
1655 (with-ir1-environment-from-node node
1656 (let ((exit (make-exit)))
1657 (setf (ctran-next prev) nil)
1658 (link-node-to-previous-ctran exit prev)
1659 (setf (block-last block) exit)))
1660 (setf (node-prev node) nil)
1663 (aver (eq (block-start-cleanup block)
1664 (block-end-cleanup block)))
1665 (unlink-blocks block next)
1666 (dolist (pred (block-pred block))
1667 (change-block-successor pred block next))
1668 (when (block-delete-p block)
1669 (let ((component (block-component block)))
1670 (setf (component-delete-blocks component)
1671 (delq block (component-delete-blocks component)))))
1672 (remove-from-dfo block)
1673 (setf (block-delete-p block) t)
1674 (setf (node-prev node) nil)
1677 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1679 (defun ctran-deleted-p (ctran)
1680 (declare (type ctran ctran))
1681 (let ((block (ctran-block ctran)))
1682 (or (not (block-component block))
1683 (block-delete-p block))))
1685 ;;; Return true if NODE has been deleted, false if it is still a valid
1687 (defun node-deleted (node)
1688 (declare (type node node))
1689 (let ((prev (node-prev node)))
1691 (ctran-deleted-p prev))))
1693 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1694 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1695 ;;; triggered by deletion.
1696 (defun delete-component (component)
1697 (declare (type component component))
1698 (aver (null (component-new-functionals component)))
1699 (setf (component-kind component) :deleted)
1700 (do-blocks (block component)
1701 (delete-block-lazily block))
1702 (dolist (fun (component-lambdas component))
1703 (unless (eq (functional-kind fun) :deleted)
1704 (setf (functional-kind fun) nil)
1705 (setf (functional-entry-fun fun) nil)
1706 (setf (leaf-refs fun) nil)
1707 (delete-functional fun)))
1708 (clean-component component)
1711 ;;; Remove all pending blocks to be deleted. Return the nearest live
1712 ;;; block after or equal to BLOCK.
1713 (defun clean-component (component &optional block)
1714 (loop while (component-delete-blocks component)
1715 ;; actual deletion of a block may queue new blocks
1716 do (let ((current (pop (component-delete-blocks component))))
1717 (when (eq block current)
1718 (setq block (block-next block)))
1719 (delete-block current)))
1722 ;;; Convert code of the form
1723 ;;; (FOO ... (FUN ...) ...)
1725 ;;; (FOO ... ... ...).
1726 ;;; In other words, replace the function combination FUN by its
1727 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1728 ;;; to blow out of whatever transform called this. Note, as the number
1729 ;;; of arguments changes, the transform must be prepared to return a
1730 ;;; lambda with a new lambda-list with the correct number of
1732 (defun splice-fun-args (lvar fun num-args)
1734 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments to feed
1735 directly to the LVAR-DEST of LVAR, which must be a combination. If FUN
1736 is :ANY, the function name is not checked."
1737 (declare (type lvar lvar)
1739 (type index num-args))
1740 (let ((outside (lvar-dest lvar))
1741 (inside (lvar-uses lvar)))
1742 (aver (combination-p outside))
1743 (unless (combination-p inside)
1744 (give-up-ir1-transform))
1745 (let ((inside-fun (combination-fun inside)))
1746 (unless (or (eq fun :any)
1747 (eq (lvar-fun-name inside-fun) fun))
1748 (give-up-ir1-transform))
1749 (let ((inside-args (combination-args inside)))
1750 (unless (= (length inside-args) num-args)
1751 (give-up-ir1-transform))
1752 (let* ((outside-args (combination-args outside))
1753 (arg-position (position lvar outside-args))
1754 (before-args (subseq outside-args 0 arg-position))
1755 (after-args (subseq outside-args (1+ arg-position))))
1756 (dolist (arg inside-args)
1757 (setf (lvar-dest arg) outside)
1758 (flush-lvar-externally-checkable-type arg))
1759 (setf (combination-args inside) nil)
1760 (setf (combination-args outside)
1761 (append before-args inside-args after-args))
1762 (change-ref-leaf (lvar-uses inside-fun)
1763 (find-free-fun 'list "???"))
1764 (setf (combination-fun-info inside) (info :function :info 'list)
1765 (combination-kind inside) :known)
1766 (setf (node-derived-type inside) *wild-type*)
1770 ;;; Eliminate keyword arguments from the call (leaving the
1771 ;;; parameters in place.
1773 ;;; (FOO ... :BAR X :QUUX Y)
1777 ;;; SPECS is a list of (:KEYWORD PARAMETER) specifications.
1778 ;;; Returns the list of specified parameters names in the
1779 ;;; order they appeared in the call. N-POSITIONAL is the
1780 ;;; number of positional arguments in th call.
1781 (defun eliminate-keyword-args (call n-positional specs)
1782 (let* ((specs (copy-tree specs))
1783 (all (combination-args call))
1784 (new-args (reverse (subseq all 0 n-positional)))
1785 (key-args (subseq all n-positional))
1788 (loop while key-args
1789 do (let* ((key (pop key-args))
1790 (val (pop key-args))
1791 (keyword (if (constant-lvar-p key)
1793 (give-up-ir1-transform)))
1794 (spec (or (assoc keyword specs :test #'eq)
1795 (give-up-ir1-transform))))
1797 (push key flushed-keys)
1798 (push (second spec) parameters)
1799 ;; In case of duplicate keys.
1800 (setf (second spec) (gensym))))
1801 (dolist (key flushed-keys)
1803 (setf (combination-args call) (reverse new-args))
1804 (reverse parameters)))
1806 (defun extract-fun-args (lvar fun num-args)
1807 (declare (type lvar lvar)
1808 (type (or symbol list) fun)
1809 (type index num-args))
1810 (let ((fun (if (listp fun) fun (list fun))))
1811 (let ((inside (lvar-uses lvar)))
1812 (unless (combination-p inside)
1813 (give-up-ir1-transform))
1814 (let ((inside-fun (combination-fun inside)))
1815 (unless (member (lvar-fun-name inside-fun) fun)
1816 (give-up-ir1-transform))
1817 (let ((inside-args (combination-args inside)))
1818 (unless (= (length inside-args) num-args)
1819 (give-up-ir1-transform))
1820 (values (lvar-fun-name inside-fun) inside-args))))))
1822 (defun flush-combination (combination)
1823 (declare (type combination combination))
1824 (flush-dest (combination-fun combination))
1825 (dolist (arg (combination-args combination))
1827 (unlink-node combination)
1833 ;;; Change the LEAF that a REF refers to.
1834 (defun change-ref-leaf (ref leaf)
1835 (declare (type ref ref) (type leaf leaf))
1836 (unless (eq (ref-leaf ref) leaf)
1837 (push ref (leaf-refs leaf))
1839 (setf (ref-leaf ref) leaf)
1840 (setf (leaf-ever-used leaf) t)
1841 (let* ((ltype (leaf-type leaf))
1842 (vltype (make-single-value-type ltype)))
1843 (if (let* ((lvar (node-lvar ref))
1844 (dest (and lvar (lvar-dest lvar))))
1845 (and (basic-combination-p dest)
1846 (eq lvar (basic-combination-fun dest))
1847 (csubtypep ltype (specifier-type 'function))))
1848 (setf (node-derived-type ref) vltype)
1849 (derive-node-type ref vltype)))
1850 (reoptimize-lvar (node-lvar ref)))
1853 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1854 (defun substitute-leaf (new-leaf old-leaf)
1855 (declare (type leaf new-leaf old-leaf))
1856 (dolist (ref (leaf-refs old-leaf))
1857 (change-ref-leaf ref new-leaf))
1860 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1861 ;;; whether to substitute
1862 (defun substitute-leaf-if (test new-leaf old-leaf)
1863 (declare (type leaf new-leaf old-leaf) (type function test))
1864 (dolist (ref (leaf-refs old-leaf))
1865 (when (funcall test ref)
1866 (change-ref-leaf ref new-leaf)))
1869 ;;; Return a LEAF which represents the specified constant object. If
1870 ;;; the object is not in *CONSTANTS*, then we create a new constant
1871 ;;; LEAF and enter it. If we are producing a fasl file, make sure that
1872 ;;; MAKE-LOAD-FORM gets used on any parts of the constant that it
1875 ;;; We are allowed to coalesce things like EQUAL strings and bit-vectors
1876 ;;; when file-compiling, but not when using COMPILE.
1877 (defun find-constant (object &optional (name nil namep))
1878 (let ((faslp (producing-fasl-file)))
1879 (labels ((make-it ()
1882 (maybe-emit-make-load-forms object name)
1883 (maybe-emit-make-load-forms object)))
1884 (make-constant object))
1885 (core-coalesce-p (x)
1886 ;; True for things which retain their identity under EQUAL,
1887 ;; so we can safely share the same CONSTANT leaf between
1888 ;; multiple references.
1889 (or (typep x '(or symbol number character))
1890 ;; Amusingly enough, we see CLAMBDAs --among other things--
1891 ;; here, from compiling things like %ALLOCATE-CLOSUREs forms.
1892 ;; No point in stuffing them in the hash-table.
1893 (and (typep x 'instance)
1894 (not (or (leaf-p x) (node-p x))))))
1895 (file-coalesce-p (x)
1896 ;; CLHS 3.2.4.2.2: We are also allowed to coalesce various
1897 ;; other things when file-compiling.
1898 (or (core-coalesce-p x)
1900 (if (eq +code-coverage-unmarked+ (cdr x))
1901 ;; These are already coalesced, and the CAR should
1902 ;; always be OK, so no need to check.
1904 (unless (maybe-cyclic-p x) ; safe for EQUAL?
1906 ((atom y) (file-coalesce-p y))
1907 (unless (file-coalesce-p (car y))
1909 ;; We *could* coalesce base-strings as well,
1910 ;; but we'd need a separate hash-table for
1911 ;; that, since we are not allowed to coalesce
1912 ;; base-strings with non-base-strings.
1915 ;; in the cross-compiler, we coalesce
1916 ;; all strings with the same contents,
1917 ;; because we will end up dumping them
1918 ;; as base-strings anyway. In the
1919 ;; real compiler, we're not allowed to
1920 ;; coalesce regardless of string
1921 ;; specialized element type, so we
1922 ;; KLUDGE by coalescing only character
1923 ;; strings (the common case) and
1924 ;; punting on the other types.
1928 (vector character))))))
1930 (if faslp (file-coalesce-p x) (core-coalesce-p x))))
1931 (if (and (boundp '*constants*) (coalescep object))
1932 (or (gethash object *constants*)
1933 (setf (gethash object *constants*)
1937 ;;; Return true if VAR would have to be closed over if environment
1938 ;;; analysis ran now (i.e. if there are any uses that have a different
1939 ;;; home lambda than VAR's home.)
1940 (defun closure-var-p (var)
1941 (declare (type lambda-var var))
1942 (let ((home (lambda-var-home var)))
1943 (cond ((eq (functional-kind home) :deleted)
1945 (t (let ((home (lambda-home home)))
1948 :key #'node-home-lambda
1950 (or (frob (leaf-refs var))
1951 (frob (basic-var-sets var)))))))))
1953 ;;; If there is a non-local exit noted in ENTRY's environment that
1954 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1955 (defun find-nlx-info (exit)
1956 (declare (type exit exit))
1957 (let* ((entry (exit-entry exit))
1958 (cleanup (entry-cleanup entry))
1959 (block (first (block-succ (node-block exit)))))
1960 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1961 (when (and (eq (nlx-info-block nlx) block)
1962 (eq (nlx-info-cleanup nlx) cleanup))
1965 (defun nlx-info-lvar (nlx)
1966 (declare (type nlx-info nlx))
1967 (node-lvar (block-last (nlx-info-target nlx))))
1969 ;;;; functional hackery
1971 (declaim (ftype (sfunction (functional) clambda) main-entry))
1972 (defun main-entry (functional)
1973 (etypecase functional
1974 (clambda functional)
1976 (optional-dispatch-main-entry functional))))
1978 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1979 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1980 ;;; optional with null default and no SUPPLIED-P. There must be a
1981 ;;; &REST arg with no references.
1982 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
1983 (defun looks-like-an-mv-bind (functional)
1984 (and (optional-dispatch-p functional)
1985 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1987 (let ((info (lambda-var-arg-info (car arg))))
1988 (unless info (return nil))
1989 (case (arg-info-kind info)
1991 (when (or (arg-info-supplied-p info) (arg-info-default info))
1994 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1998 ;;; Return true if function is an external entry point. This is true
1999 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
2000 ;;; (:TOPLEVEL kind.)
2002 (declare (type functional fun))
2003 (not (null (member (functional-kind fun) '(:external :toplevel)))))
2005 ;;; If LVAR's only use is a non-notinline global function reference,
2006 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
2007 ;;; is true, then we don't care if the leaf is NOTINLINE.
2008 (defun lvar-fun-name (lvar &optional notinline-ok)
2009 (declare (type lvar lvar))
2010 (let ((use (lvar-uses lvar)))
2012 (let ((leaf (ref-leaf use)))
2013 (if (and (global-var-p leaf)
2014 (eq (global-var-kind leaf) :global-function)
2015 (or (not (defined-fun-p leaf))
2016 (not (eq (defined-fun-inlinep leaf) :notinline))
2018 (leaf-source-name leaf)
2022 (defun lvar-fun-debug-name (lvar)
2023 (declare (type lvar lvar))
2024 (let ((uses (lvar-uses lvar)))
2026 (leaf-debug-name (ref-leaf use))))
2029 (mapcar #'name1 uses)))))
2031 ;;; Return the source name of a combination -- or signals an error
2032 ;;; if the function leaf is anonymous.
2033 (defun combination-fun-source-name (combination &optional (errorp t))
2034 (let ((leaf (ref-leaf (lvar-uses (combination-fun combination)))))
2035 (if (or errorp (leaf-has-source-name-p leaf))
2036 (values (leaf-source-name leaf) t)
2039 ;;; Return the COMBINATION node that is the call to the LET FUN.
2040 (defun let-combination (fun)
2041 (declare (type clambda fun))
2042 (aver (functional-letlike-p fun))
2043 (lvar-dest (node-lvar (first (leaf-refs fun)))))
2045 ;;; Return the initial value lvar for a LET variable, or NIL if there
2047 (defun let-var-initial-value (var)
2048 (declare (type lambda-var var))
2049 (let ((fun (lambda-var-home var)))
2050 (elt (combination-args (let-combination fun))
2051 (position-or-lose var (lambda-vars fun)))))
2053 ;;; Return the LAMBDA that is called by the local CALL.
2054 (defun combination-lambda (call)
2055 (declare (type basic-combination call))
2056 (aver (eq (basic-combination-kind call) :local))
2057 (ref-leaf (lvar-uses (basic-combination-fun call))))
2059 (defvar *inline-expansion-limit* 200
2061 "an upper limit on the number of inline function calls that will be expanded
2062 in any given code object (single function or block compilation)")
2064 ;;; Check whether NODE's component has exceeded its inline expansion
2065 ;;; limit, and warn if so, returning NIL.
2066 (defun inline-expansion-ok (node)
2067 (let ((expanded (incf (component-inline-expansions
2069 (node-block node))))))
2070 (cond ((> expanded *inline-expansion-limit*) nil)
2071 ((= expanded *inline-expansion-limit*)
2072 ;; FIXME: If the objective is to stop the recursive
2073 ;; expansion of inline functions, wouldn't it be more
2074 ;; correct to look back through surrounding expansions
2075 ;; (which are, I think, stored in the *CURRENT-PATH*, and
2076 ;; possibly stored elsewhere too) and suppress expansion
2077 ;; and print this warning when the function being proposed
2078 ;; for inline expansion is found there? (I don't like the
2079 ;; arbitrary numerical limit in principle, and I think
2080 ;; it'll be a nuisance in practice if we ever want the
2081 ;; compiler to be able to use WITH-COMPILATION-UNIT on
2082 ;; arbitrarily huge blocks of code. -- WHN)
2083 (let ((*compiler-error-context* node))
2084 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
2085 probably trying to~% ~
2086 inline a recursive function."
2087 *inline-expansion-limit*))
2091 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
2092 (defun assure-functional-live-p (functional)
2093 (declare (type functional functional))
2095 ;; looks LET-converted
2096 (functional-somewhat-letlike-p functional)
2097 ;; It's possible for a LET-converted function to end up
2098 ;; deleted later. In that case, for the purposes of this
2099 ;; analysis, it is LET-converted: LET-converted functionals
2100 ;; are too badly trashed to expand them inline, and deleted
2101 ;; LET-converted functionals are even worse.
2102 (memq (functional-kind functional) '(:deleted :zombie))))
2103 (throw 'locall-already-let-converted functional)))
2105 (defun assure-leaf-live-p (leaf)
2108 (when (lambda-var-deleted leaf)
2109 (throw 'locall-already-let-converted leaf)))
2111 (assure-functional-live-p leaf))))
2114 (defun call-full-like-p (call)
2115 (declare (type combination call))
2116 (let ((kind (basic-combination-kind call)))
2118 (and (eq kind :known)
2119 (let ((info (basic-combination-fun-info call)))
2121 (not (fun-info-ir2-convert info))
2122 (dolist (template (fun-info-templates info) t)
2123 (when (eq (template-ltn-policy template) :fast-safe)
2124 (multiple-value-bind (val win)
2125 (valid-fun-use call (template-type template))
2126 (when (or val (not win)) (return nil)))))))))))
2130 ;;; Apply a function to some arguments, returning a list of the values
2131 ;;; resulting of the evaluation. If an error is signalled during the
2132 ;;; application, then we produce a warning message using WARN-FUN and
2133 ;;; return NIL as our second value to indicate this. NODE is used as
2134 ;;; the error context for any error message, and CONTEXT is a string
2135 ;;; that is spliced into the warning.
2136 (declaim (ftype (sfunction ((or symbol function) list node function string)
2137 (values list boolean))
2139 (defun careful-call (function args node warn-fun context)
2141 (multiple-value-list
2142 (handler-case (apply function args)
2144 (let ((*compiler-error-context* node))
2145 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
2146 (return-from careful-call (values nil nil))))))
2149 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
2152 ((deffrob (basic careful compiler transform)
2154 (defun ,careful (specifier)
2155 (handler-case (,basic specifier)
2156 (sb!kernel::arg-count-error (condition)
2157 (values nil (list (format nil "~A" condition))))
2158 (simple-error (condition)
2159 (values nil (list* (simple-condition-format-control condition)
2160 (simple-condition-format-arguments condition))))))
2161 (defun ,compiler (specifier)
2162 (multiple-value-bind (type error-args) (,careful specifier)
2164 (apply #'compiler-error error-args))))
2165 (defun ,transform (specifier)
2166 (multiple-value-bind (type error-args) (,careful specifier)
2168 (apply #'give-up-ir1-transform
2170 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
2171 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
2174 ;;;; utilities used at run-time for parsing &KEY args in IR1
2176 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
2177 ;;; the lvar for the value of the &KEY argument KEY in the list of
2178 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
2179 ;;; otherwise. The legality and constantness of the keywords should
2180 ;;; already have been checked.
2181 (declaim (ftype (sfunction (list keyword) (or lvar null))
2183 (defun find-keyword-lvar (args key)
2184 (do ((arg args (cddr arg)))
2186 (when (eq (lvar-value (first arg)) key)
2187 (return (second arg)))))
2189 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2190 ;;; verify that alternating lvars in ARGS are constant and that there
2191 ;;; is an even number of args.
2192 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
2193 (defun check-key-args-constant (args)
2194 (do ((arg args (cddr arg)))
2196 (unless (and (rest arg)
2197 (constant-lvar-p (first arg)))
2200 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2201 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
2202 ;;; and that only keywords present in the list KEYS are supplied.
2203 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
2204 (defun check-transform-keys (args keys)
2205 (and (check-key-args-constant args)
2206 (do ((arg args (cddr arg)))
2208 (unless (member (lvar-value (first arg)) keys)
2213 ;;; Called by the expansion of the EVENT macro.
2214 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
2215 (defun %event (info node)
2216 (incf (event-info-count info))
2217 (when (and (>= (event-info-level info) *event-note-threshold*)
2218 (policy (or node *lexenv*)
2219 (= inhibit-warnings 0)))
2220 (let ((*compiler-error-context* node))
2221 (compiler-notify (event-info-description info))))
2223 (let ((action (event-info-action info)))
2224 (when action (funcall action node))))
2227 (defun make-cast (value type policy)
2228 (declare (type lvar value)
2230 (type policy policy))
2231 (%make-cast :asserted-type type
2232 :type-to-check (maybe-weaken-check type policy)
2234 :derived-type (coerce-to-values type)))
2236 (defun cast-type-check (cast)
2237 (declare (type cast cast))
2238 (when (cast-reoptimize cast)
2239 (ir1-optimize-cast cast t))
2240 (cast-%type-check cast))
2242 (defun note-single-valuified-lvar (lvar)
2243 (declare (type (or lvar null) lvar))
2245 (let ((use (lvar-uses lvar)))
2247 (let ((leaf (ref-leaf use)))
2248 (when (and (lambda-var-p leaf)
2249 (null (rest (leaf-refs leaf))))
2250 (reoptimize-lambda-var leaf))))
2251 ((or (listp use) (combination-p use))
2252 (do-uses (node lvar)
2253 (setf (node-reoptimize node) t)
2254 (setf (block-reoptimize (node-block node)) t)
2255 (reoptimize-component (node-component node) :maybe)))))))
2257 ;;; Return true if LVAR's only use is a reference to a global function
2258 ;;; designator with one of the specified NAMES, that hasn't been
2259 ;;; declared NOTINLINE.
2260 (defun lvar-fun-is (lvar names)
2261 (declare (type lvar lvar) (list names))
2262 (let ((use (lvar-uses lvar)))
2264 (let* ((*lexenv* (node-lexenv use))
2265 (leaf (ref-leaf use))
2267 (cond ((global-var-p leaf)
2269 (and (eq (global-var-kind leaf) :global-function)
2270 (car (member (leaf-source-name leaf) names
2273 (let ((value (constant-value leaf)))
2274 (car (if (functionp value)
2279 (fdefinition name)))
2283 :test #'equal))))))))
2285 (not (fun-lexically-notinline-p name)))))))
2287 ;;; Return true if LVAR's only use is a call to one of the named functions
2288 ;;; (or any function if none are specified) with the specified number of
2289 ;;; of arguments (or any number if number is not specified)
2290 (defun lvar-matches (lvar &key fun-names arg-count)
2291 (let ((use (lvar-uses lvar)))
2292 (and (combination-p use)
2294 (multiple-value-bind (name ok)
2295 (combination-fun-source-name use nil)
2296 (and ok (member name fun-names :test #'eq))))
2298 (= arg-count (length (combination-args use)))))))