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 (defun principal-lvar-dest (lvar)
85 (declare (type lvar lvar))
86 (let ((dest (lvar-dest lvar)))
88 (pld (cast-lvar dest))
92 ;;; Update lvar use information so that NODE is no longer a use of its
95 ;;; Note: if you call this function, you may have to do a
96 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
98 (declaim (ftype (sfunction (node) (values))
101 ;;; Just delete NODE from its LVAR uses; LVAR is preserved so it may
102 ;;; be given a new use.
103 (defun %delete-lvar-use (node)
104 (let ((lvar (node-lvar node)))
106 (if (listp (lvar-uses lvar))
107 (let ((new-uses (delq node (lvar-uses lvar))))
108 (setf (lvar-uses lvar)
109 (if (singleton-p new-uses)
112 (setf (lvar-uses lvar) nil))
113 (setf (node-lvar node) nil)))
115 ;;; Delete NODE from its LVAR uses; if LVAR has no other uses, delete
116 ;;; its DEST's block, which must be unreachable.
117 (defun delete-lvar-use (node)
118 (let ((lvar (node-lvar node)))
120 (%delete-lvar-use node)
121 (if (null (lvar-uses lvar))
122 (binding* ((dest (lvar-dest lvar) :exit-if-null)
123 (() (not (node-deleted dest)) :exit-if-null)
124 (block (node-block dest)))
125 (mark-for-deletion block))
126 (reoptimize-lvar lvar))))
129 ;;; Update lvar use information so that NODE uses LVAR.
131 ;;; Note: if you call this function, you may have to do a
132 ;;; REOPTIMIZE-LVAR to inform IR1 optimization that something has
134 (declaim (ftype (sfunction (node (or lvar null)) (values)) add-lvar-use))
135 (defun add-lvar-use (node lvar)
136 (aver (not (node-lvar node)))
138 (let ((uses (lvar-uses lvar)))
139 (setf (lvar-uses lvar)
146 (setf (node-lvar node) lvar)))
150 ;;; Return true if LVAR destination is executed immediately after
151 ;;; NODE. Cleanups are ignored.
152 (defun immediately-used-p (lvar node)
153 (declare (type lvar lvar) (type node node))
154 (aver (eq (node-lvar node) lvar))
155 (let ((dest (lvar-dest lvar)))
156 (acond ((node-next node)
157 (eq (ctran-next it) dest))
158 (t (eq (block-start (first (block-succ (node-block node))))
159 (node-prev dest))))))
161 ;;; Returns the defined (usually untrusted) type of the combination,
162 ;;; or NIL if we couldn't figure it out.
163 (defun combination-defined-type (combination)
164 (let ((use (principal-lvar-use (basic-combination-fun combination))))
165 (or (when (ref-p use)
166 (let ((type (leaf-defined-type (ref-leaf use))))
167 (when (fun-type-p type)
168 (fun-type-returns type))))
171 ;;; Return true if LVAR destination is executed after node with only
172 ;;; uninteresting nodes intervening.
174 ;;; Uninteresting nodes are nodes in the same block which are either
175 ;;; REFs, external CASTs to the same destination, or known combinations
176 ;;; that never unwind.
177 (defun almost-immediately-used-p (lvar node)
178 (declare (type lvar lvar)
180 (aver (eq (node-lvar node) lvar))
181 (let ((dest (lvar-dest lvar)))
184 (let ((ctran (node-next node)))
186 (setf node (ctran-next ctran))
188 (return-from almost-immediately-used-p t)
193 (when (and (eq :external (cast-type-check node))
194 (eq dest (node-dest node)))
197 ;; KLUDGE: Unfortunately we don't have an attribute for
198 ;; "never unwinds", so we just special case
199 ;; %ALLOCATE-CLOSURES: it is easy to run into with eg.
200 ;; FORMAT and a non-constant first argument.
201 (when (eq '%allocate-closures (combination-fun-source-name node nil))
204 (when (eq (block-start (first (block-succ (node-block node))))
206 (return-from almost-immediately-used-p t))))))))
208 ;;;; lvar substitution
210 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
211 ;;; NIL. We do not flush OLD's DEST.
212 (defun substitute-lvar (new old)
213 (declare (type lvar old new))
214 (aver (not (lvar-dest new)))
215 (let ((dest (lvar-dest old)))
218 (cif (setf (if-test dest) new))
219 (cset (setf (set-value dest) new))
220 (creturn (setf (return-result dest) new))
221 (exit (setf (exit-value dest) new))
223 (if (eq old (basic-combination-fun dest))
224 (setf (basic-combination-fun dest) new)
225 (setf (basic-combination-args dest)
226 (nsubst new old (basic-combination-args dest)))))
227 (cast (setf (cast-value dest) new)))
229 (setf (lvar-dest old) nil)
230 (setf (lvar-dest new) dest)
231 (flush-lvar-externally-checkable-type new))
234 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
235 ;;; arbitary number of uses. NEW is supposed to be "later" than OLD.
236 (defun substitute-lvar-uses (new old propagate-dx)
237 (declare (type lvar old)
238 (type (or lvar null) new)
239 (type boolean propagate-dx))
243 (%delete-lvar-use node)
244 (add-lvar-use node new))
245 (reoptimize-lvar new)
246 (awhen (and propagate-dx (lvar-dynamic-extent old))
247 (setf (lvar-dynamic-extent old) nil)
248 (unless (lvar-dynamic-extent new)
249 (setf (lvar-dynamic-extent new) it)
250 (setf (cleanup-info it) (subst new old (cleanup-info it)))))
251 (when (lvar-dynamic-extent new)
253 (node-ends-block node))))
254 (t (flush-dest old)))
258 ;;;; block starting/creation
260 ;;; Return the block that CTRAN is the start of, making a block if
261 ;;; necessary. This function is called by IR1 translators which may
262 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
263 ;;; used more than once must start a block by the time that anyone
264 ;;; does a USE-CTRAN on it.
266 ;;; We also throw the block into the next/prev list for the
267 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
269 (defun ctran-starts-block (ctran)
270 (declare (type ctran ctran))
271 (ecase (ctran-kind ctran)
273 (aver (not (ctran-block ctran)))
274 (let* ((next (component-last-block *current-component*))
275 (prev (block-prev next))
276 (new-block (make-block ctran)))
277 (setf (block-next new-block) next
278 (block-prev new-block) prev
279 (block-prev next) new-block
280 (block-next prev) new-block
281 (ctran-block ctran) new-block
282 (ctran-kind ctran) :block-start)
283 (aver (not (ctran-use ctran)))
286 (ctran-block ctran))))
288 ;;; Ensure that CTRAN is the start of a block so that the use set can
289 ;;; be freely manipulated.
290 (defun ensure-block-start (ctran)
291 (declare (type ctran ctran))
292 (let ((kind (ctran-kind ctran)))
296 (setf (ctran-block ctran)
297 (make-block-key :start ctran))
298 (setf (ctran-kind ctran) :block-start))
300 (node-ends-block (ctran-use ctran)))))
303 ;;; CTRAN must be the last ctran in an incomplete block; finish the
304 ;;; block and start a new one if necessary.
305 (defun start-block (ctran)
306 (declare (type ctran ctran))
307 (aver (not (ctran-next ctran)))
308 (ecase (ctran-kind ctran)
310 (let ((block (ctran-block ctran))
311 (node (ctran-use ctran)))
312 (aver (not (block-last block)))
314 (setf (block-last block) node)
315 (setf (node-next node) nil)
316 (setf (ctran-use ctran) nil)
317 (setf (ctran-kind ctran) :unused)
318 (setf (ctran-block ctran) nil)
319 (link-blocks block (ctran-starts-block ctran))))
324 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
325 ;;; call. Exactly one argument must be 'DUMMY, which will be replaced
326 ;;; with LVAR. In case of an ordinary call the function should not
327 ;;; have return type NIL. We create a new "filtered" lvar.
329 ;;; TODO: remove preconditions.
330 (defun filter-lvar (lvar form)
331 (declare (type lvar lvar) (type list form))
332 (let* ((dest (lvar-dest lvar))
333 (ctran (node-prev dest)))
334 (with-ir1-environment-from-node dest
336 (ensure-block-start ctran)
337 (let* ((old-block (ctran-block ctran))
338 (new-start (make-ctran))
339 (filtered-lvar (make-lvar))
340 (new-block (ctran-starts-block new-start)))
342 ;; Splice in the new block before DEST, giving the new block
343 ;; all of DEST's predecessors.
344 (dolist (block (block-pred old-block))
345 (change-block-successor block old-block new-block))
347 (ir1-convert new-start ctran filtered-lvar form)
349 ;; KLUDGE: Comments at the head of this function in CMU CL
350 ;; said that somewhere in here we
351 ;; Set the new block's start and end cleanups to the *start*
352 ;; cleanup of PREV's block. This overrides the incorrect
353 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
354 ;; Unfortunately I can't find any code which corresponds to this.
355 ;; Perhaps it was a stale comment? Or perhaps I just don't
356 ;; understand.. -- WHN 19990521
358 ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
359 ;; no LET conversion has been done yet.) The [mv-]combination
360 ;; code from the call in the form will be the use of the new
361 ;; check lvar. We substitute exactly one argument.
362 (let* ((node (lvar-use filtered-lvar))
364 (dolist (arg (basic-combination-args node) (aver victim))
365 (let* ((arg (principal-lvar arg))
368 (when (and (ref-p use)
369 (constant-p (setf leaf (ref-leaf use)))
370 (eql (constant-value leaf) 'dummy))
373 (aver (eq (constant-value (ref-leaf (lvar-use victim)))
376 (substitute-lvar filtered-lvar lvar)
377 (substitute-lvar lvar victim)
380 ;; Invoking local call analysis converts this call to a LET.
381 (locall-analyze-component *current-component*))))
384 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
385 (defun delete-filter (node lvar value)
386 (aver (eq (lvar-dest value) node))
387 (aver (eq (node-lvar node) lvar))
388 (cond (lvar (collect ((merges))
389 (when (return-p (lvar-dest lvar))
391 (when (and (basic-combination-p use)
392 (eq (basic-combination-kind use) :local))
394 (substitute-lvar-uses lvar value
395 (and lvar (eq (lvar-uses lvar) node)))
396 (%delete-lvar-use node)
399 (dolist (merge (merges))
400 (merge-tail-sets merge)))))
401 (t (flush-dest value)
402 (unlink-node node))))
404 ;;; Make a CAST and insert it into IR1 before node NEXT.
405 (defun insert-cast-before (next lvar type policy)
406 (declare (type node next) (type lvar lvar) (type ctype type))
407 (with-ir1-environment-from-node next
408 (let* ((ctran (node-prev next))
409 (cast (make-cast lvar type policy))
410 (internal-ctran (make-ctran)))
411 (setf (ctran-next ctran) cast
412 (node-prev cast) ctran)
413 (use-ctran cast internal-ctran)
414 (link-node-to-previous-ctran next internal-ctran)
415 (setf (lvar-dest lvar) cast)
416 (reoptimize-lvar lvar)
417 (when (return-p next)
418 (node-ends-block cast))
419 (setf (block-attributep (block-flags (node-block cast))
420 type-check type-asserted)
424 ;;;; miscellaneous shorthand functions
426 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
427 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
428 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
429 ;;; deleted, and then return its home.
430 (defun node-home-lambda (node)
431 (declare (type node node))
432 (do ((fun (lexenv-lambda (node-lexenv node))
433 (lexenv-lambda (lambda-call-lexenv fun))))
434 ((not (memq (functional-kind fun) '(:deleted :zombie)))
436 (when (eq (lambda-home fun) fun)
439 #!-sb-fluid (declaim (inline node-block))
440 (defun node-block (node)
441 (ctran-block (node-prev node)))
442 (declaim (ftype (sfunction (node) component) node-component))
443 (defun node-component (node)
444 (block-component (node-block node)))
445 (declaim (ftype (sfunction (node) physenv) node-physenv))
446 (defun node-physenv (node)
447 (lambda-physenv (node-home-lambda node)))
448 #!-sb-fluid (declaim (inline node-dest))
449 (defun node-dest (node)
450 (awhen (node-lvar node) (lvar-dest it)))
452 #!-sb-fluid (declaim (inline node-stack-allocate-p))
453 (defun node-stack-allocate-p (node)
454 (awhen (node-lvar node)
455 (lvar-dynamic-extent it)))
457 (defun flushable-combination-p (call)
458 (declare (type combination call))
459 (let ((kind (combination-kind call))
460 (info (combination-fun-info call)))
461 (when (and (eq kind :known) (fun-info-p info))
462 (let ((attr (fun-info-attributes info)))
463 (when (and (not (ir1-attributep attr call))
464 ;; FIXME: For now, don't consider potentially flushable
465 ;; calls flushable when they have the CALL attribute.
466 ;; Someday we should look at the functional args to
467 ;; determine if they have any side effects.
468 (if (policy call (= safety 3))
469 (ir1-attributep attr flushable)
470 (ir1-attributep attr unsafely-flushable)))
473 ;;;; DYNAMIC-EXTENT related
475 (defun lambda-var-original-name (leaf)
476 (let ((home (lambda-var-home leaf)))
477 (if (eq :external (functional-kind home))
478 (let* ((entry (functional-entry-fun home))
479 (p (1- (position leaf (lambda-vars home)))))
481 (if (optional-dispatch-p entry)
482 (elt (optional-dispatch-arglist entry) p)
483 (elt (lambda-vars entry) p))))
484 (leaf-debug-name leaf))))
486 (defun note-no-stack-allocation (lvar &key flush)
487 (do-uses (use (principal-lvar lvar))
489 ;; Don't complain about not being able to stack allocate constants.
490 (and (ref-p use) (constant-p (ref-leaf use)))
491 ;; If we're flushing, don't complain if we can flush the combination.
492 (and flush (combination-p use) (flushable-combination-p use))
493 ;; Don't report those with homes in :OPTIONAL -- we'd get doubled
495 (and (ref-p use) (lambda-var-p (ref-leaf use))
496 (eq :optional (lambda-kind (lambda-var-home (ref-leaf use))))))
497 ;; FIXME: For the first leg (lambda-bind (lambda-var-home ...))
498 ;; would be a far better description, but since we use
499 ;; *COMPILER-ERROR-CONTEXT* for muffling we can't -- as that node
500 ;; can have different handled conditions.
501 (let ((*compiler-error-context* use))
502 (if (and (ref-p use) (lambda-var-p (ref-leaf use)))
503 (compiler-notify "~@<could~2:I not stack allocate ~S in: ~S~:@>"
504 (lambda-var-original-name (ref-leaf use))
505 (find-original-source (node-source-path use)))
506 (compiler-notify "~@<could~2:I not stack allocate: ~S~:@>"
507 (find-original-source (node-source-path use))))))))
509 (defun use-good-for-dx-p (use dx &optional component)
510 ;; FIXME: Can casts point to LVARs in other components?
511 ;; RECHECK-DYNAMIC-EXTENT-LVARS assumes that they can't -- that is, that the
512 ;; PRINCIPAL-LVAR is always in the same component as the original one. It
513 ;; would be either good to have an explanation of why casts don't point
514 ;; across components, or an explanation of when they do it. ...in the
515 ;; meanwhile AVER that our assumption holds true.
516 (aver (or (not component) (eq component (node-component use))))
517 (or (dx-combination-p use dx)
519 (not (cast-type-check use))
520 (lvar-good-for-dx-p (cast-value use) dx component))
521 (and (trivial-lambda-var-ref-p use)
522 (let ((uses (lvar-uses (trivial-lambda-var-ref-lvar use))))
524 (lvar-good-for-dx-p (trivial-lambda-var-ref-lvar use) dx component))))))
526 (defun lvar-good-for-dx-p (lvar dx &optional component)
527 (let ((uses (lvar-uses lvar)))
531 (use-good-for-dx-p use dx component))
533 (use-good-for-dx-p uses dx component))))
535 (defun known-dx-combination-p (use dx)
536 (and (eq (combination-kind use) :known)
537 (let ((info (combination-fun-info use)))
538 (or (awhen (fun-info-stack-allocate-result info)
540 (awhen (fun-info-result-arg info)
541 (let ((args (combination-args use)))
542 (lvar-good-for-dx-p (if (zerop it)
547 (defun dx-combination-p (use dx)
548 (and (combination-p use)
550 ;; Known, and can do DX.
551 (known-dx-combination-p use dx)
552 ;; Possibly a not-yet-eliminated lambda which ends up returning the
553 ;; results of an actual known DX combination.
554 (let* ((fun (combination-fun use))
555 (ref (principal-lvar-use fun))
556 (clambda (when (ref-p ref)
558 (creturn (when (lambda-p clambda)
559 (lambda-return clambda)))
560 (result-use (when (return-p creturn)
561 (principal-lvar-use (return-result creturn)))))
562 ;; FIXME: We should be able to deal with multiple uses here as well.
563 (and (dx-combination-p result-use dx)
564 (combination-args-flow-cleanly-p use result-use dx))))))
566 (defun combination-args-flow-cleanly-p (combination1 combination2 dx)
567 (labels ((recurse (combination)
568 (or (eq combination combination2)
569 (if (known-dx-combination-p combination dx)
570 (let ((dest (lvar-dest (combination-lvar combination))))
571 (and (combination-p dest)
573 (let* ((fun1 (combination-fun combination))
574 (ref1 (principal-lvar-use fun1))
575 (clambda1 (when (ref-p ref1) (ref-leaf ref1))))
576 (when (lambda-p clambda1)
577 (dolist (var (lambda-vars clambda1) t)
578 (dolist (var-ref (lambda-var-refs var))
579 (let ((dest (principal-lvar-dest (ref-lvar var-ref))))
580 (unless (and (combination-p dest) (recurse dest))
581 (return-from combination-args-flow-cleanly-p nil)))))))))))
582 (recurse combination1)))
584 (defun ref-good-for-dx-p (ref)
585 (let* ((lvar (ref-lvar ref))
586 (dest (when lvar (lvar-dest lvar))))
587 (and (combination-p dest)
588 (eq :known (combination-kind dest))
589 (awhen (combination-fun-info dest)
590 (or (ir1-attributep (fun-info-attributes it) dx-safe)
591 (and (not (combination-lvar dest))
592 (awhen (fun-info-result-arg it)
593 (eql lvar (nth it (combination-args dest))))))))))
595 (defun trivial-lambda-var-ref-p (use)
597 (let ((var (ref-leaf use)))
598 ;; lambda-var, no SETS, not explicitly indefinite-extent.
599 (when (and (lambda-var-p var) (not (lambda-var-sets var))
600 (neq :indefinite (lambda-var-extent var)))
601 (let ((home (lambda-var-home var))
602 (refs (lambda-var-refs var)))
603 ;; bound by a non-XEP system lambda, no other REFS that aren't
604 ;; DX-SAFE, or are result-args when the result is discarded.
605 (when (and (lambda-system-lambda-p home)
606 (neq :external (lambda-kind home))
608 (unless (or (eq use ref) (ref-good-for-dx-p ref))
610 ;; the LAMBDA this var is bound by has only a single REF, going
612 (let* ((lambda-refs (lambda-refs home))
613 (primary (car lambda-refs)))
615 (not (cdr lambda-refs))
616 (combination-p (lvar-dest (ref-lvar primary)))))))))))
618 (defun trivial-lambda-var-ref-lvar (use)
619 (let* ((this (ref-leaf use))
620 (fun (lambda-var-home this))
621 (vars (lambda-vars fun))
622 (combination (lvar-dest (ref-lvar (car (lambda-refs fun)))))
623 (args (combination-args combination)))
624 (aver (= (length vars) (length args)))
625 (loop for var in vars
630 ;;; This needs to play nice with LVAR-GOOD-FOR-DX-P and friends.
631 (defun handle-nested-dynamic-extent-lvars (dx lvar &optional recheck-component)
632 (let ((uses (lvar-uses lvar)))
633 ;; DX value generators must end their blocks: see UPDATE-UVL-LIVE-SETS.
634 ;; Uses of mupltiple-use LVARs already end their blocks, so we just need
635 ;; to process uses of single-use LVARs.
637 (node-ends-block uses))
638 ;; If this LVAR's USE is good for DX, it is either a CAST, or it
639 ;; must be a regular combination whose arguments are potentially DX as well.
640 (flet ((recurse (use)
643 (handle-nested-dynamic-extent-lvars
644 dx (cast-value use) recheck-component))
646 (loop for arg in (combination-args use)
647 ;; deleted args show up as NIL here
649 (lvar-good-for-dx-p arg dx recheck-component))
650 append (handle-nested-dynamic-extent-lvars
651 dx arg recheck-component)))
653 (let* ((other (trivial-lambda-var-ref-lvar use)))
654 (unless (eq other lvar)
655 (handle-nested-dynamic-extent-lvars
656 dx other recheck-component)))))))
659 (loop for use in uses
660 when (use-good-for-dx-p use dx recheck-component)
662 (when (use-good-for-dx-p uses dx recheck-component)
667 (declaim (inline block-to-be-deleted-p))
668 (defun block-to-be-deleted-p (block)
669 (or (block-delete-p block)
670 (eq (functional-kind (block-home-lambda block)) :deleted)))
672 ;;; Checks whether NODE is in a block to be deleted
673 (declaim (inline node-to-be-deleted-p))
674 (defun node-to-be-deleted-p (node)
675 (block-to-be-deleted-p (node-block node)))
677 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
678 (defun lambda-block (clambda)
679 (node-block (lambda-bind clambda)))
680 (declaim (ftype (sfunction (clambda) component) lambda-component))
681 (defun lambda-component (clambda)
682 (block-component (lambda-block clambda)))
684 (declaim (ftype (sfunction (cblock) node) block-start-node))
685 (defun block-start-node (block)
686 (ctran-next (block-start block)))
688 ;;; Return the enclosing cleanup for environment of the first or last
690 (defun block-start-cleanup (block)
691 (node-enclosing-cleanup (block-start-node block)))
692 (defun block-end-cleanup (block)
693 (node-enclosing-cleanup (block-last block)))
695 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
696 ;;; if there is none.
698 ;;; There can legitimately be no home lambda in dead code early in the
699 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
700 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
701 ;;; where the block is just a placeholder during parsing and doesn't
702 ;;; actually correspond to code which will be written anywhere.
703 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
704 (defun block-home-lambda-or-null (block)
705 (if (node-p (block-last block))
706 ;; This is the old CMU CL way of doing it.
707 (node-home-lambda (block-last block))
708 ;; Now that SBCL uses this operation more aggressively than CMU
709 ;; CL did, the old CMU CL way of doing it can fail in two ways.
710 ;; 1. It can fail in a few cases even when a meaningful home
711 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
713 ;; 2. It can fail when converting a form which is born orphaned
714 ;; so that it never had a meaningful home lambda, e.g. a form
715 ;; which follows a RETURN-FROM or GO form.
716 (let ((pred-list (block-pred block)))
717 ;; To deal with case 1, we reason that
718 ;; previous-in-target-execution-order blocks should be in the
719 ;; same lambda, and that they seem in practice to be
720 ;; previous-in-compilation-order blocks too, so we look back
721 ;; to find one which is sufficiently initialized to tell us
722 ;; what the home lambda is.
724 ;; We could get fancy about this, flooding through the
725 ;; graph of all the previous blocks, but in practice it
726 ;; seems to work just to grab the first previous block and
728 (node-home-lambda (block-last (first pred-list)))
729 ;; In case 2, we end up with an empty PRED-LIST and
730 ;; have to punt: There's no home lambda.
733 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
734 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
735 (defun block-home-lambda (block)
736 (block-home-lambda-or-null block))
738 ;;; Return the IR1 physical environment for BLOCK.
739 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
740 (defun block-physenv (block)
741 (lambda-physenv (block-home-lambda block)))
743 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
744 ;;; of its original source's top level form in its compilation unit.
745 (defun source-path-tlf-number (path)
746 (declare (list path))
749 ;;; Return the (reversed) list for the PATH in the original source
750 ;;; (with the Top Level Form number last).
751 (defun source-path-original-source (path)
752 (declare (list path) (inline member))
753 (cddr (member 'original-source-start path :test #'eq)))
755 ;;; Return the Form Number of PATH's original source inside the Top
756 ;;; Level Form that contains it. This is determined by the order that
757 ;;; we walk the subforms of the top level source form.
758 (defun source-path-form-number (path)
759 (declare (list path) (inline member))
760 (cadr (member 'original-source-start path :test #'eq)))
762 ;;; Return a list of all the enclosing forms not in the original
763 ;;; source that converted to get to this form, with the immediate
764 ;;; source for node at the start of the list.
765 (defun source-path-forms (path)
766 (subseq path 0 (position 'original-source-start path)))
768 (defun tree-some (predicate tree)
769 (let ((seen (make-hash-table)))
770 (labels ((walk (tree)
771 (cond ((funcall predicate tree))
773 (not (gethash tree seen)))
774 (setf (gethash tree seen) t)
775 (or (walk (car tree))
776 (walk (cdr tree)))))))
779 ;;; Return the innermost source form for NODE.
780 (defun node-source-form (node)
781 (declare (type node node))
782 (let* ((path (node-source-path node))
783 (forms (remove-if (lambda (x)
784 (tree-some #'leaf-p x))
785 (source-path-forms path))))
786 ;; another option: if first form includes a leaf, return
787 ;; find-original-source instead.
790 (values (find-original-source path)))))
792 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
794 (defun lvar-source (lvar)
795 (let ((use (lvar-uses lvar)))
798 (values (node-source-form use) t))))
800 ;;; Return the unique node, delivering a value to LVAR.
801 #!-sb-fluid (declaim (inline lvar-use))
802 (defun lvar-use (lvar)
803 (the (not list) (lvar-uses lvar)))
805 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
806 (defun lvar-has-single-use-p (lvar)
807 (typep (lvar-uses lvar) '(not list)))
809 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
810 (declaim (ftype (sfunction (ctran) (or clambda null))
811 ctran-home-lambda-or-null))
812 (defun ctran-home-lambda-or-null (ctran)
813 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
814 ;; implementation might not be quite right, or might be uglier than
815 ;; necessary. It appears that the original Python never found a need
816 ;; to do this operation. The obvious things based on
817 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
818 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
819 ;; generalize it enough to grovel harder when the simple CMU CL
820 ;; approach fails, and furthermore realize that in some exceptional
821 ;; cases it might return NIL. -- WHN 2001-12-04
822 (cond ((ctran-use ctran)
823 (node-home-lambda (ctran-use ctran)))
825 (block-home-lambda-or-null (ctran-block ctran)))
827 (bug "confused about home lambda for ~S" ctran))))
829 ;;; Return the LAMBDA that is CTRAN's home.
830 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
831 (defun ctran-home-lambda (ctran)
832 (ctran-home-lambda-or-null ctran))
834 (declaim (inline cast-single-value-p))
835 (defun cast-single-value-p (cast)
836 (not (values-type-p (cast-asserted-type cast))))
838 #!-sb-fluid (declaim (inline lvar-single-value-p))
839 (defun lvar-single-value-p (lvar)
841 (let ((dest (lvar-dest lvar)))
846 (eq (basic-combination-fun dest) lvar))
849 (declare (notinline lvar-single-value-p))
850 (and (cast-single-value-p dest)
851 (lvar-single-value-p (node-lvar dest)))))
855 (defun principal-lvar-end (lvar)
856 (loop for prev = lvar then (node-lvar dest)
857 for dest = (and prev (lvar-dest prev))
859 finally (return (values dest prev))))
861 (defun principal-lvar-single-valuify (lvar)
862 (loop for prev = lvar then (node-lvar dest)
863 for dest = (and prev (lvar-dest prev))
865 do (setf (node-derived-type dest)
866 (make-short-values-type (list (single-value-type
867 (node-derived-type dest)))))
868 (reoptimize-lvar prev)))
870 ;;; Return a new LEXENV just like DEFAULT except for the specified
871 ;;; slot values. Values for the alist slots are APPENDed to the
872 ;;; beginning of the current value, rather than replacing it entirely.
873 (defun make-lexenv (&key (default *lexenv*)
874 funs vars blocks tags
876 (lambda (lexenv-lambda default))
877 (cleanup (lexenv-cleanup default))
878 (handled-conditions (lexenv-handled-conditions default))
879 (disabled-package-locks
880 (lexenv-disabled-package-locks default))
881 (policy (lexenv-policy default))
882 (user-data (lexenv-user-data default)))
883 (macrolet ((frob (var slot)
884 `(let ((old (,slot default)))
888 (internal-make-lexenv
889 (frob funs lexenv-funs)
890 (frob vars lexenv-vars)
891 (frob blocks lexenv-blocks)
892 (frob tags lexenv-tags)
893 (frob type-restrictions lexenv-type-restrictions)
895 cleanup handled-conditions disabled-package-locks
899 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
901 (defun make-restricted-lexenv (lexenv)
902 (flet ((fun-good-p (fun)
903 (destructuring-bind (name . thing) fun
904 (declare (ignore name))
908 (cons (aver (eq (car thing) 'macro))
911 (destructuring-bind (name . thing) var
912 (declare (ignore name))
914 ;; The evaluator will mark lexicals with :BOGUS when it
915 ;; translates an interpreter lexenv to a compiler
917 ((or leaf #!+sb-eval (member :bogus)) nil)
918 (cons (aver (eq (car thing) 'macro))
920 (heap-alien-info nil)))))
921 (internal-make-lexenv
922 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
923 (remove-if-not #'var-good-p (lexenv-vars lexenv))
926 (lexenv-type-restrictions lexenv) ; XXX
929 (lexenv-handled-conditions lexenv)
930 (lexenv-disabled-package-locks lexenv)
931 (lexenv-policy lexenv)
932 (lexenv-user-data lexenv))))
934 ;;;; flow/DFO/component hackery
936 ;;; Join BLOCK1 and BLOCK2.
937 (defun link-blocks (block1 block2)
938 (declare (type cblock block1 block2))
939 (setf (block-succ block1)
940 (if (block-succ block1)
941 (%link-blocks block1 block2)
943 (push block1 (block-pred block2))
945 (defun %link-blocks (block1 block2)
946 (declare (type cblock block1 block2))
947 (let ((succ1 (block-succ block1)))
948 (aver (not (memq block2 succ1)))
949 (cons block2 succ1)))
951 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
952 ;;; this leaves a successor with a single predecessor that ends in an
953 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
954 ;;; now be able to be propagated to the successor.
955 (defun unlink-blocks (block1 block2)
956 (declare (type cblock block1 block2))
957 (let ((succ1 (block-succ block1)))
958 (if (eq block2 (car succ1))
959 (setf (block-succ block1) (cdr succ1))
960 (do ((succ (cdr succ1) (cdr succ))
962 ((eq (car succ) block2)
963 (setf (cdr prev) (cdr succ)))
966 (let ((new-pred (delq block1 (block-pred block2))))
967 (setf (block-pred block2) new-pred)
968 (when (singleton-p new-pred)
969 (let ((pred-block (first new-pred)))
970 (when (if-p (block-last pred-block))
971 (setf (block-test-modified pred-block) t)))))
974 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
975 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
976 ;;; consequent/alternative blocks to point to NEW. We also set
977 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
978 ;;; the new successor.
979 (defun change-block-successor (block old new)
980 (declare (type cblock new old block))
981 (unlink-blocks block old)
982 (let ((last (block-last block))
983 (comp (block-component block)))
984 (setf (component-reanalyze comp) t)
987 (setf (block-test-modified block) t)
988 (let* ((succ-left (block-succ block))
989 (new (if (and (eq new (component-tail comp))
993 (unless (memq new succ-left)
994 (link-blocks block new))
995 (macrolet ((frob (slot)
996 `(when (eq (,slot last) old)
997 (setf (,slot last) new))))
999 (frob if-alternative)
1000 (when (eq (if-consequent last)
1001 (if-alternative last))
1002 (reoptimize-component (block-component block) :maybe)))))
1004 (unless (memq new (block-succ block))
1005 (link-blocks block new)))))
1009 ;;; Unlink a block from the next/prev chain. We also null out the
1011 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
1012 (defun remove-from-dfo (block)
1013 (let ((next (block-next block))
1014 (prev (block-prev block)))
1015 (setf (block-component block) nil)
1016 (setf (block-next prev) next)
1017 (setf (block-prev next) prev))
1020 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
1021 ;;; COMPONENT to be the same as for AFTER.
1022 (defun add-to-dfo (block after)
1023 (declare (type cblock block after))
1024 (let ((next (block-next after))
1025 (comp (block-component after)))
1026 (aver (not (eq (component-kind comp) :deleted)))
1027 (setf (block-component block) comp)
1028 (setf (block-next after) block)
1029 (setf (block-prev block) after)
1030 (setf (block-next block) next)
1031 (setf (block-prev next) block))
1034 ;;; List all NLX-INFOs which BLOCK can exit to.
1036 ;;; We hope that no cleanup actions are performed in the middle of
1037 ;;; BLOCK, so it is enough to look only at cleanups in the block
1038 ;;; end. The tricky thing is a special cleanup block; all its nodes
1039 ;;; have the same cleanup info, corresponding to the start, so the
1040 ;;; same approach returns safe result.
1041 (defun map-block-nlxes (fun block &optional dx-cleanup-fun)
1042 (loop for cleanup = (block-end-cleanup block)
1043 then (node-enclosing-cleanup (cleanup-mess-up cleanup))
1045 do (let ((mess-up (cleanup-mess-up cleanup)))
1046 (case (cleanup-kind cleanup)
1048 (aver (entry-p mess-up))
1049 (loop for exit in (entry-exits mess-up)
1050 for nlx-info = (exit-nlx-info exit)
1051 do (funcall fun nlx-info)))
1052 ((:catch :unwind-protect)
1053 (aver (combination-p mess-up))
1054 (let* ((arg-lvar (first (basic-combination-args mess-up)))
1055 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
1056 (funcall fun nlx-info)))
1058 (when dx-cleanup-fun
1059 (funcall dx-cleanup-fun cleanup)))))))
1061 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
1062 ;;; the head and tail which are set to T.
1063 (declaim (ftype (sfunction (component) (values)) clear-flags))
1064 (defun clear-flags (component)
1065 (let ((head (component-head component))
1066 (tail (component-tail component)))
1067 (setf (block-flag head) t)
1068 (setf (block-flag tail) t)
1069 (do-blocks (block component)
1070 (setf (block-flag block) nil)))
1073 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
1074 ;;; true in the head and tail blocks.
1075 (declaim (ftype (sfunction () component) make-empty-component))
1076 (defun make-empty-component ()
1077 (let* ((head (make-block-key :start nil :component nil))
1078 (tail (make-block-key :start nil :component nil))
1079 (res (make-component head tail)))
1080 (setf (block-flag head) t)
1081 (setf (block-flag tail) t)
1082 (setf (block-component head) res)
1083 (setf (block-component tail) res)
1084 (setf (block-next head) tail)
1085 (setf (block-prev tail) head)
1088 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
1089 ;;; The new block is added to the DFO immediately following NODE's block.
1090 (defun node-ends-block (node)
1091 (declare (type node node))
1092 (let* ((block (node-block node))
1093 (start (node-next node))
1094 (last (block-last block)))
1095 (check-type last node)
1096 (unless (eq last node)
1097 (aver (and (eq (ctran-kind start) :inside-block)
1098 (not (block-delete-p block))))
1099 (let* ((succ (block-succ block))
1101 (make-block-key :start start
1102 :component (block-component block)
1103 :succ succ :last last)))
1104 (setf (ctran-kind start) :block-start)
1105 (setf (ctran-use start) nil)
1106 (setf (block-last block) node)
1107 (setf (node-next node) nil)
1109 (setf (block-pred b)
1110 (cons new-block (remove block (block-pred b)))))
1111 (setf (block-succ block) ())
1112 (link-blocks block new-block)
1113 (add-to-dfo new-block block)
1114 (setf (component-reanalyze (block-component block)) t)
1116 (do ((ctran start (node-next (ctran-next ctran))))
1118 (setf (ctran-block ctran) new-block))
1120 (setf (block-type-asserted block) t)
1121 (setf (block-test-modified block) t))))
1126 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
1127 (defun delete-lambda-var (leaf)
1128 (declare (type lambda-var leaf))
1130 (setf (lambda-var-deleted leaf) t)
1131 ;; Iterate over all local calls flushing the corresponding argument,
1132 ;; allowing the computation of the argument to be deleted. We also
1133 ;; mark the LET for reoptimization, since it may be that we have
1134 ;; deleted its last variable.
1135 (let* ((fun (lambda-var-home leaf))
1136 (n (position leaf (lambda-vars fun))))
1137 (dolist (ref (leaf-refs fun))
1138 (let* ((lvar (node-lvar ref))
1139 (dest (and lvar (lvar-dest lvar))))
1140 (when (and (combination-p dest)
1141 (eq (basic-combination-fun dest) lvar)
1142 (eq (basic-combination-kind dest) :local))
1143 (let* ((args (basic-combination-args dest))
1145 (reoptimize-lvar arg)
1147 (setf (elt args n) nil))))))
1149 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
1150 ;; too much difficulty, since we can efficiently implement
1151 ;; write-only variables. We iterate over the SETs, marking their
1152 ;; blocks for dead code flushing, since we can delete SETs whose
1154 (dolist (set (lambda-var-sets leaf))
1155 (setf (block-flush-p (node-block set)) t))
1159 ;;; Note that something interesting has happened to VAR.
1160 (defun reoptimize-lambda-var (var)
1161 (declare (type lambda-var var))
1162 (let ((fun (lambda-var-home var)))
1163 ;; We only deal with LET variables, marking the corresponding
1164 ;; initial value arg as needing to be reoptimized.
1165 (when (and (eq (functional-kind fun) :let)
1167 (do ((args (basic-combination-args
1168 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1170 (vars (lambda-vars fun) (cdr vars)))
1171 ((eq (car vars) var)
1172 (reoptimize-lvar (car args))))))
1175 ;;; Delete a function that has no references. This need only be called
1176 ;;; on functions that never had any references, since otherwise
1177 ;;; DELETE-REF will handle the deletion.
1178 (defun delete-functional (fun)
1179 (aver (and (null (leaf-refs fun))
1180 (not (functional-entry-fun fun))))
1182 (optional-dispatch (delete-optional-dispatch fun))
1183 (clambda (delete-lambda fun)))
1186 ;;; Deal with deleting the last reference to a CLAMBDA, which means
1187 ;;; that the lambda is unreachable, so that its body may be
1188 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
1189 ;;; IR1-OPTIMIZE to delete its blocks.
1190 (defun delete-lambda (clambda)
1191 (declare (type clambda clambda))
1192 (let ((original-kind (functional-kind clambda))
1193 (bind (lambda-bind clambda)))
1194 (aver (not (member original-kind '(:deleted :toplevel))))
1195 (aver (not (functional-has-external-references-p clambda)))
1196 (aver (or (eq original-kind :zombie) bind))
1197 (setf (functional-kind clambda) :deleted)
1198 (setf (lambda-bind clambda) nil)
1200 (labels ((delete-children (lambda)
1201 (dolist (child (lambda-children lambda))
1202 (cond ((eq (functional-kind child) :deleted)
1203 (delete-children child))
1205 (delete-lambda child))))
1206 (setf (lambda-children lambda) nil)
1207 (setf (lambda-parent lambda) nil)))
1208 (delete-children clambda))
1210 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
1211 ;; that we're using the old value of the KIND slot, not the
1212 ;; current slot value, which has now been set to :DELETED.)
1215 ((:let :mv-let :assignment)
1216 (let ((bind-block (node-block bind)))
1217 (mark-for-deletion bind-block))
1218 (let ((home (lambda-home clambda)))
1219 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1220 ;; KLUDGE: In presence of NLEs we cannot always understand that
1221 ;; LET's BIND dominates its body [for a LET "its" body is not
1222 ;; quite its]; let's delete too dangerous for IR2 stuff. --
1224 (dolist (var (lambda-vars clambda))
1225 (flet ((delete-node (node)
1226 (mark-for-deletion (node-block node))))
1227 (mapc #'delete-node (leaf-refs var))
1228 (mapc #'delete-node (lambda-var-sets var)))))
1230 ;; Function has no reachable references.
1231 (dolist (ref (lambda-refs clambda))
1232 (mark-for-deletion (node-block ref)))
1233 ;; If the function isn't a LET, we unlink the function head
1234 ;; and tail from the component head and tail to indicate that
1235 ;; the code is unreachable. We also delete the function from
1236 ;; COMPONENT-LAMBDAS (it won't be there before local call
1237 ;; analysis, but no matter.) If the lambda was never
1238 ;; referenced, we give a note.
1239 (let* ((bind-block (node-block bind))
1240 (component (block-component bind-block))
1241 (return (lambda-return clambda))
1242 (return-block (and return (node-block return))))
1243 (unless (leaf-ever-used clambda)
1244 (let ((*compiler-error-context* bind))
1245 (compiler-notify 'code-deletion-note
1246 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
1247 :format-arguments (list (leaf-debug-name clambda)))))
1248 (unless (block-delete-p bind-block)
1249 (unlink-blocks (component-head component) bind-block))
1250 (when (and return-block (not (block-delete-p return-block)))
1251 (mark-for-deletion return-block)
1252 (unlink-blocks return-block (component-tail component)))
1253 (setf (component-reanalyze component) t)
1254 (let ((tails (lambda-tail-set clambda)))
1255 (setf (tail-set-funs tails)
1256 (delete clambda (tail-set-funs tails)))
1257 (setf (lambda-tail-set clambda) nil))
1258 (setf (component-lambdas component)
1259 (delq clambda (component-lambdas component))))))
1261 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
1262 ;; ENTRY-FUN so that people will know that it is not an entry
1264 (when (eq original-kind :external)
1265 (let ((fun (functional-entry-fun clambda)))
1266 (setf (functional-entry-fun fun) nil)
1267 (when (optional-dispatch-p fun)
1268 (delete-optional-dispatch fun)))))
1272 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
1273 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
1274 ;;; is used both before and after local call analysis. Afterward, all
1275 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
1276 ;;; to the XEP, leaving it with no references at all. So we look at
1277 ;;; the XEP to see whether an optional-dispatch is still really being
1278 ;;; used. But before local call analysis, there are no XEPs, and all
1279 ;;; references are direct.
1281 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
1282 ;;; entry-points, making them be normal lambdas, and then deleting the
1283 ;;; ones with no references. This deletes any e-p lambdas that were
1284 ;;; either never referenced, or couldn't be deleted when the last
1285 ;;; reference was deleted (due to their :OPTIONAL kind.)
1287 ;;; Note that the last optional entry point may alias the main entry,
1288 ;;; so when we process the main entry, its KIND may have been changed
1289 ;;; to NIL or even converted to a LETlike value.
1290 (defun delete-optional-dispatch (leaf)
1291 (declare (type optional-dispatch leaf))
1292 (let ((entry (functional-entry-fun leaf)))
1293 (unless (and entry (leaf-refs entry))
1294 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
1295 (setf (functional-kind leaf) :deleted)
1298 (unless (eq (functional-kind fun) :deleted)
1299 (aver (eq (functional-kind fun) :optional))
1300 (setf (functional-kind fun) nil)
1301 (let ((refs (leaf-refs fun)))
1303 (delete-lambda fun))
1305 (or (maybe-let-convert fun)
1306 (maybe-convert-to-assignment fun)))
1308 (maybe-convert-to-assignment fun)))))))
1310 (dolist (ep (optional-dispatch-entry-points leaf))
1311 (when (promise-ready-p ep)
1313 (when (optional-dispatch-more-entry leaf)
1314 (frob (optional-dispatch-more-entry leaf)))
1315 (let ((main (optional-dispatch-main-entry leaf)))
1317 (setf (functional-entry-fun entry) main)
1318 (setf (functional-entry-fun main) entry))
1319 (when (eq (functional-kind main) :optional)
1324 (defun note-local-functional (fun)
1325 (declare (type functional fun))
1326 (when (and (leaf-has-source-name-p fun)
1327 (eq (leaf-source-name fun) (functional-debug-name fun)))
1328 (let ((name (leaf-source-name fun)))
1329 (let ((defined-fun (gethash name *free-funs*)))
1330 (when (and defined-fun
1331 (defined-fun-p defined-fun)
1332 (eq (defined-fun-functional defined-fun) fun))
1333 (remhash name *free-funs*))))))
1335 ;;; Return functional for DEFINED-FUN which has been converted in policy
1336 ;;; corresponding to the current one, or NIL if no such functional exists.
1338 ;;; Also check that the parent of the functional is visible in the current
1340 (defun defined-fun-functional (defined-fun)
1341 (let ((functionals (defined-fun-functionals defined-fun)))
1343 (let* ((sample (car functionals))
1344 (there (lambda-parent (if (lambda-p sample)
1346 (optional-dispatch-main-entry sample)))))
1348 (labels ((lookup (here)
1349 (unless (eq here there)
1351 (lookup (lambda-parent here))
1352 ;; We looked up all the way up, and didn't find the parent
1353 ;; of the functional -- therefore it is nested in a lambda
1354 ;; we don't see, so return nil.
1355 (return-from defined-fun-functional nil)))))
1356 (lookup (lexenv-lambda *lexenv*)))))
1357 ;; Now find a functional whose policy matches the current one, if we already
1359 (let ((policy (lexenv-%policy *lexenv*)))
1360 (dolist (functional functionals)
1361 (when (equal policy (lexenv-%policy (functional-lexenv functional)))
1362 (return functional)))))))
1364 ;;; Do stuff to delete the semantic attachments of a REF node. When
1365 ;;; this leaves zero or one reference, we do a type dispatch off of
1366 ;;; the leaf to determine if a special action is appropriate.
1367 (defun delete-ref (ref)
1368 (declare (type ref ref))
1369 (let* ((leaf (ref-leaf ref))
1370 (refs (delq ref (leaf-refs leaf))))
1371 (setf (leaf-refs leaf) refs)
1376 (delete-lambda-var leaf))
1378 (ecase (functional-kind leaf)
1379 ((nil :let :mv-let :assignment :escape :cleanup)
1380 (aver (null (functional-entry-fun leaf)))
1381 (delete-lambda leaf))
1383 (unless (functional-has-external-references-p leaf)
1384 (delete-lambda leaf)))
1385 ((:deleted :zombie :optional))))
1387 (unless (eq (functional-kind leaf) :deleted)
1388 (delete-optional-dispatch leaf)))))
1391 (clambda (or (maybe-let-convert leaf)
1392 (maybe-convert-to-assignment leaf)))
1393 (lambda-var (reoptimize-lambda-var leaf))))
1396 (clambda (maybe-convert-to-assignment leaf))))))
1400 ;;; This function is called by people who delete nodes; it provides a
1401 ;;; way to indicate that the value of a lvar is no longer used. We
1402 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1403 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1404 (defun flush-dest (lvar)
1405 (declare (type (or lvar null) lvar))
1407 (when (lvar-dynamic-extent lvar)
1408 (note-no-stack-allocation lvar :flush t))
1409 (setf (lvar-dest lvar) nil)
1410 (flush-lvar-externally-checkable-type lvar)
1412 (let ((prev (node-prev use)))
1413 (let ((block (ctran-block prev)))
1414 (reoptimize-component (block-component block) t)
1415 (setf (block-attributep (block-flags block)
1416 flush-p type-asserted type-check)
1418 (setf (node-lvar use) nil))
1419 (setf (lvar-uses lvar) nil))
1422 (defun delete-dest (lvar)
1424 (let* ((dest (lvar-dest lvar))
1425 (prev (node-prev dest)))
1426 (let ((block (ctran-block prev)))
1427 (unless (block-delete-p block)
1428 (mark-for-deletion block))))))
1430 ;;; Queue the block for deletion
1431 (defun delete-block-lazily (block)
1432 (declare (type cblock block))
1433 (unless (block-delete-p block)
1434 (setf (block-delete-p block) t)
1435 (push block (component-delete-blocks (block-component block)))))
1437 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1438 ;;; blocks with the DELETE-P flag.
1439 (defun mark-for-deletion (block)
1440 (declare (type cblock block))
1441 (let* ((component (block-component block))
1442 (head (component-head component)))
1443 (labels ((helper (block)
1444 (delete-block-lazily block)
1445 (dolist (pred (block-pred block))
1446 (unless (or (block-delete-p pred)
1449 (unless (block-delete-p block)
1451 (setf (component-reanalyze component) t))))
1454 ;;; This function does what is necessary to eliminate the code in it
1455 ;;; from the IR1 representation. This involves unlinking it from its
1456 ;;; predecessors and successors and deleting various node-specific
1457 ;;; semantic information. BLOCK must be already removed from
1458 ;;; COMPONENT-DELETE-BLOCKS.
1459 (defun delete-block (block &optional silent)
1460 (declare (type cblock block))
1461 (aver (block-component block)) ; else block is already deleted!
1462 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1464 (note-block-deletion block))
1465 (setf (block-delete-p block) t)
1467 (dolist (b (block-pred block))
1468 (unlink-blocks b block)
1469 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1470 ;; broken when successors were deleted without setting the
1471 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1472 ;; doesn't happen again.
1473 (aver (not (and (null (block-succ b))
1474 (not (block-delete-p b))
1475 (not (eq b (component-head (block-component b))))))))
1476 (dolist (b (block-succ block))
1477 (unlink-blocks block b))
1479 (do-nodes-carefully (node block)
1480 (when (valued-node-p node)
1481 (delete-lvar-use node))
1483 (ref (delete-ref node))
1484 (cif (flush-dest (if-test node)))
1485 ;; The next two cases serve to maintain the invariant that a LET
1486 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1487 ;; the lambda whenever we delete any of these, but we must be
1488 ;; careful that this LET has not already been partially deleted.
1490 (when (and (eq (basic-combination-kind node) :local)
1491 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1492 (lvar-uses (basic-combination-fun node)))
1493 (let ((fun (combination-lambda node)))
1494 ;; If our REF was the second-to-last ref, and has been
1495 ;; deleted, then FUN may be a LET for some other
1497 (when (and (functional-letlike-p fun)
1498 (eq (let-combination fun) node))
1499 (delete-lambda fun))))
1500 (flush-dest (basic-combination-fun node))
1501 (dolist (arg (basic-combination-args node))
1502 (when arg (flush-dest arg))))
1504 (let ((lambda (bind-lambda node)))
1505 (unless (eq (functional-kind lambda) :deleted)
1506 (delete-lambda lambda))))
1508 (let ((value (exit-value node))
1509 (entry (exit-entry node)))
1513 (setf (entry-exits entry)
1514 (delq node (entry-exits entry))))))
1516 (dolist (exit (entry-exits node))
1517 (mark-for-deletion (node-block exit)))
1518 (let ((home (node-home-lambda node)))
1519 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1521 (flush-dest (return-result node))
1522 (delete-return node))
1524 (flush-dest (set-value node))
1525 (let ((var (set-var node)))
1526 (setf (basic-var-sets var)
1527 (delete node (basic-var-sets var)))))
1529 (flush-dest (cast-value node)))))
1531 (remove-from-dfo block)
1534 ;;; Do stuff to indicate that the return node NODE is being deleted.
1535 (defun delete-return (node)
1536 (declare (type creturn node))
1537 (let* ((fun (return-lambda node))
1538 (tail-set (lambda-tail-set fun)))
1539 (aver (lambda-return fun))
1540 (setf (lambda-return fun) nil)
1541 (when (and tail-set (not (find-if #'lambda-return
1542 (tail-set-funs tail-set))))
1543 (setf (tail-set-type tail-set) *empty-type*)))
1546 ;;; If any of the VARS in FUN was never referenced and was not
1547 ;;; declared IGNORE, then complain.
1548 (defun note-unreferenced-vars (fun)
1549 (declare (type clambda fun))
1550 (dolist (var (lambda-vars fun))
1551 (unless (or (leaf-ever-used var)
1552 (lambda-var-ignorep var))
1553 (let ((*compiler-error-context* (lambda-bind fun)))
1554 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1555 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1556 ;; requires this to be no more than a STYLE-WARNING.
1558 (compiler-style-warn "The variable ~S is defined but never used."
1559 (leaf-debug-name var))
1560 ;; There's no reason to accept this kind of equivocation
1561 ;; when compiling our own code, though.
1563 (warn "The variable ~S is defined but never used."
1564 (leaf-debug-name var)))
1565 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1568 (defvar *deletion-ignored-objects* '(t nil))
1570 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1571 ;;; our recursion so that we don't get lost in circular structures. We
1572 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1573 ;;; function referencess with variables), and we also ignore anything
1575 (defun present-in-form (obj form depth)
1576 (declare (type (integer 0 20) depth))
1577 (cond ((= depth 20) nil)
1581 (let ((first (car form))
1583 (if (member first '(quote function))
1585 (or (and (not (symbolp first))
1586 (present-in-form obj first depth))
1587 (do ((l (cdr form) (cdr l))
1589 ((or (atom l) (> n 100))
1591 (declare (fixnum n))
1592 (when (present-in-form obj (car l) depth)
1595 ;;; This function is called on a block immediately before we delete
1596 ;;; it. We check to see whether any of the code about to die appeared
1597 ;;; in the original source, and emit a note if so.
1599 ;;; If the block was in a lambda is now deleted, then we ignore the
1600 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1601 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1602 ;;; reasonable for a function to not return, and there is a different
1603 ;;; note for that case anyway.
1605 ;;; If the actual source is an atom, then we use a bunch of heuristics
1606 ;;; to guess whether this reference really appeared in the original
1608 ;;; -- If a symbol, it must be interned and not a keyword.
1609 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1610 ;;; or a character.)
1611 ;;; -- The atom must be "present" in the original source form, and
1612 ;;; present in all intervening actual source forms.
1613 (defun note-block-deletion (block)
1614 (let ((home (block-home-lambda block)))
1615 (unless (eq (functional-kind home) :deleted)
1616 (do-nodes (node nil block)
1617 (let* ((path (node-source-path node))
1618 (first (first path)))
1619 (when (or (eq first 'original-source-start)
1621 (or (not (symbolp first))
1622 (let ((pkg (symbol-package first)))
1624 (not (eq pkg (symbol-package :end))))))
1625 (not (member first *deletion-ignored-objects*))
1626 (not (typep first '(or fixnum character)))
1628 (present-in-form first x 0))
1629 (source-path-forms path))
1630 (present-in-form first (find-original-source path)
1632 (unless (return-p node)
1633 (let ((*compiler-error-context* node))
1634 (compiler-notify 'code-deletion-note
1635 :format-control "deleting unreachable code"
1636 :format-arguments nil)))
1640 ;;; Delete a node from a block, deleting the block if there are no
1641 ;;; nodes left. We remove the node from the uses of its LVAR.
1643 ;;; If the node is the last node, there must be exactly one successor.
1644 ;;; We link all of our precedessors to the successor and unlink the
1645 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1646 ;;; left, and the block is a successor of itself, then we replace the
1647 ;;; only node with a degenerate exit node. This provides a way to
1648 ;;; represent the bodyless infinite loop, given the prohibition on
1649 ;;; empty blocks in IR1.
1650 (defun unlink-node (node)
1651 (declare (type node node))
1652 (when (valued-node-p node)
1653 (delete-lvar-use node))
1655 (let* ((ctran (node-next node))
1656 (next (and ctran (ctran-next ctran)))
1657 (prev (node-prev node))
1658 (block (ctran-block prev))
1659 (prev-kind (ctran-kind prev))
1660 (last (block-last block)))
1662 (setf (block-type-asserted block) t)
1663 (setf (block-test-modified block) t)
1665 (cond ((or (eq prev-kind :inside-block)
1666 (and (eq prev-kind :block-start)
1667 (not (eq node last))))
1668 (cond ((eq node last)
1669 (setf (block-last block) (ctran-use prev))
1670 (setf (node-next (ctran-use prev)) nil))
1672 (setf (ctran-next prev) next)
1673 (setf (node-prev next) prev)
1674 (when (if-p next) ; AOP wanted
1675 (reoptimize-lvar (if-test next)))))
1676 (setf (node-prev node) nil)
1679 (aver (eq prev-kind :block-start))
1680 (aver (eq node last))
1681 (let* ((succ (block-succ block))
1682 (next (first succ)))
1683 (aver (singleton-p succ))
1685 ((eq block (first succ))
1686 (with-ir1-environment-from-node node
1687 (let ((exit (make-exit)))
1688 (setf (ctran-next prev) nil)
1689 (link-node-to-previous-ctran exit prev)
1690 (setf (block-last block) exit)))
1691 (setf (node-prev node) nil)
1694 (aver (eq (block-start-cleanup block)
1695 (block-end-cleanup block)))
1696 (unlink-blocks block next)
1697 (dolist (pred (block-pred block))
1698 (change-block-successor pred block next))
1699 (when (block-delete-p block)
1700 (let ((component (block-component block)))
1701 (setf (component-delete-blocks component)
1702 (delq block (component-delete-blocks component)))))
1703 (remove-from-dfo block)
1704 (setf (block-delete-p block) t)
1705 (setf (node-prev node) nil)
1708 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1710 (defun ctran-deleted-p (ctran)
1711 (declare (type ctran ctran))
1712 (let ((block (ctran-block ctran)))
1713 (or (not (block-component block))
1714 (block-delete-p block))))
1716 ;;; Return true if NODE has been deleted, false if it is still a valid
1718 (defun node-deleted (node)
1719 (declare (type node node))
1720 (let ((prev (node-prev node)))
1722 (ctran-deleted-p prev))))
1724 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1725 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1726 ;;; triggered by deletion.
1727 (defun delete-component (component)
1728 (declare (type component component))
1729 (aver (null (component-new-functionals component)))
1730 (setf (component-kind component) :deleted)
1731 (do-blocks (block component)
1732 (delete-block-lazily block))
1733 (dolist (fun (component-lambdas component))
1734 (unless (eq (functional-kind fun) :deleted)
1735 (setf (functional-kind fun) nil)
1736 (setf (functional-entry-fun fun) nil)
1737 (setf (leaf-refs fun) nil)
1738 (delete-functional fun)))
1739 (clean-component component)
1742 ;;; Remove all pending blocks to be deleted. Return the nearest live
1743 ;;; block after or equal to BLOCK.
1744 (defun clean-component (component &optional block)
1745 (loop while (component-delete-blocks component)
1746 ;; actual deletion of a block may queue new blocks
1747 do (let ((current (pop (component-delete-blocks component))))
1748 (when (eq block current)
1749 (setq block (block-next block)))
1750 (delete-block current)))
1753 ;;; Convert code of the form
1754 ;;; (FOO ... (FUN ...) ...)
1756 ;;; (FOO ... ... ...).
1757 ;;; In other words, replace the function combination FUN by its
1758 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1759 ;;; to blow out of whatever transform called this. Note, as the number
1760 ;;; of arguments changes, the transform must be prepared to return a
1761 ;;; lambda with a new lambda-list with the correct number of
1763 (defun splice-fun-args (lvar fun num-args)
1765 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments to feed
1766 directly to the LVAR-DEST of LVAR, which must be a combination. If FUN
1767 is :ANY, the function name is not checked."
1768 (declare (type lvar lvar)
1770 (type index num-args))
1771 (let ((outside (lvar-dest lvar))
1772 (inside (lvar-uses lvar)))
1773 (aver (combination-p outside))
1774 (unless (combination-p inside)
1775 (give-up-ir1-transform))
1776 (let ((inside-fun (combination-fun inside)))
1777 (unless (or (eq fun :any)
1778 (eq (lvar-fun-name inside-fun) fun))
1779 (give-up-ir1-transform))
1780 (let ((inside-args (combination-args inside)))
1781 (unless (= (length inside-args) num-args)
1782 (give-up-ir1-transform))
1783 (let* ((outside-args (combination-args outside))
1784 (arg-position (position lvar outside-args))
1785 (before-args (subseq outside-args 0 arg-position))
1786 (after-args (subseq outside-args (1+ arg-position))))
1787 (dolist (arg inside-args)
1788 (setf (lvar-dest arg) outside)
1789 (flush-lvar-externally-checkable-type arg))
1790 (setf (combination-args inside) nil)
1791 (setf (combination-args outside)
1792 (append before-args inside-args after-args))
1793 (change-ref-leaf (lvar-uses inside-fun)
1794 (find-free-fun 'list "???"))
1795 (setf (combination-fun-info inside) (info :function :info 'list)
1796 (combination-kind inside) :known)
1797 (setf (node-derived-type inside) *wild-type*)
1801 ;;; Eliminate keyword arguments from the call (leaving the
1802 ;;; parameters in place.
1804 ;;; (FOO ... :BAR X :QUUX Y)
1808 ;;; SPECS is a list of (:KEYWORD PARAMETER) specifications.
1809 ;;; Returns the list of specified parameters names in the
1810 ;;; order they appeared in the call. N-POSITIONAL is the
1811 ;;; number of positional arguments in th call.
1812 (defun eliminate-keyword-args (call n-positional specs)
1813 (let* ((specs (copy-tree specs))
1814 (all (combination-args call))
1815 (new-args (reverse (subseq all 0 n-positional)))
1816 (key-args (subseq all n-positional))
1819 (loop while key-args
1820 do (let* ((key (pop key-args))
1821 (val (pop key-args))
1822 (keyword (if (constant-lvar-p key)
1824 (give-up-ir1-transform)))
1825 (spec (or (assoc keyword specs :test #'eq)
1826 (give-up-ir1-transform))))
1828 (push key flushed-keys)
1829 (push (second spec) parameters)
1830 ;; In case of duplicate keys.
1831 (setf (second spec) (gensym))))
1832 (dolist (key flushed-keys)
1834 (setf (combination-args call) (reverse new-args))
1835 (reverse parameters)))
1837 (defun extract-fun-args (lvar fun num-args)
1838 (declare (type lvar lvar)
1839 (type (or symbol list) fun)
1840 (type index num-args))
1841 (let ((fun (if (listp fun) fun (list fun))))
1842 (let ((inside (lvar-uses lvar)))
1843 (unless (combination-p inside)
1844 (give-up-ir1-transform))
1845 (let ((inside-fun (combination-fun inside)))
1846 (unless (member (lvar-fun-name inside-fun) fun)
1847 (give-up-ir1-transform))
1848 (let ((inside-args (combination-args inside)))
1849 (unless (= (length inside-args) num-args)
1850 (give-up-ir1-transform))
1851 (values (lvar-fun-name inside-fun) inside-args))))))
1853 (defun flush-combination (combination)
1854 (declare (type combination combination))
1855 (flush-dest (combination-fun combination))
1856 (dolist (arg (combination-args combination))
1858 (unlink-node combination)
1864 ;;; Change the LEAF that a REF refers to.
1865 (defun change-ref-leaf (ref leaf)
1866 (declare (type ref ref) (type leaf leaf))
1867 (unless (eq (ref-leaf ref) leaf)
1868 (push ref (leaf-refs leaf))
1870 (setf (ref-leaf ref) leaf)
1871 (setf (leaf-ever-used leaf) t)
1872 (let* ((ltype (leaf-type leaf))
1873 (vltype (make-single-value-type ltype)))
1874 (if (let* ((lvar (node-lvar ref))
1875 (dest (and lvar (lvar-dest lvar))))
1876 (and (basic-combination-p dest)
1877 (eq lvar (basic-combination-fun dest))
1878 (csubtypep ltype (specifier-type 'function))))
1879 (setf (node-derived-type ref) vltype)
1880 (derive-node-type ref vltype)))
1881 (reoptimize-lvar (node-lvar ref)))
1884 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1885 (defun substitute-leaf (new-leaf old-leaf)
1886 (declare (type leaf new-leaf old-leaf))
1887 (dolist (ref (leaf-refs old-leaf))
1888 (change-ref-leaf ref new-leaf))
1891 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1892 ;;; whether to substitute
1893 (defun substitute-leaf-if (test new-leaf old-leaf)
1894 (declare (type leaf new-leaf old-leaf) (type function test))
1895 (dolist (ref (leaf-refs old-leaf))
1896 (when (funcall test ref)
1897 (change-ref-leaf ref new-leaf)))
1900 ;;; Return a LEAF which represents the specified constant object. If
1901 ;;; the object is not in *CONSTANTS*, then we create a new constant
1902 ;;; LEAF and enter it. If we are producing a fasl file, make sure that
1903 ;;; MAKE-LOAD-FORM gets used on any parts of the constant that it
1906 ;;; We are allowed to coalesce things like EQUAL strings and bit-vectors
1907 ;;; when file-compiling, but not when using COMPILE.
1908 (defun find-constant (object &optional (name nil namep))
1909 (let ((faslp (producing-fasl-file)))
1910 (labels ((make-it ()
1913 (maybe-emit-make-load-forms object name)
1914 (maybe-emit-make-load-forms object)))
1915 (make-constant object))
1916 (core-coalesce-p (x)
1917 ;; True for things which retain their identity under EQUAL,
1918 ;; so we can safely share the same CONSTANT leaf between
1919 ;; multiple references.
1920 (or (typep x '(or symbol number character))
1921 ;; Amusingly enough, we see CLAMBDAs --among other things--
1922 ;; here, from compiling things like %ALLOCATE-CLOSUREs forms.
1923 ;; No point in stuffing them in the hash-table.
1924 (and (typep x 'instance)
1925 (not (or (leaf-p x) (node-p x))))))
1926 (file-coalesce-p (x)
1927 ;; CLHS 3.2.4.2.2: We are also allowed to coalesce various
1928 ;; other things when file-compiling.
1929 (or (core-coalesce-p x)
1931 (if (eq +code-coverage-unmarked+ (cdr x))
1932 ;; These are already coalesced, and the CAR should
1933 ;; always be OK, so no need to check.
1935 (unless (maybe-cyclic-p x) ; safe for EQUAL?
1937 ((atom y) (file-coalesce-p y))
1938 (unless (file-coalesce-p (car y))
1940 ;; We *could* coalesce base-strings as well,
1941 ;; but we'd need a separate hash-table for
1942 ;; that, since we are not allowed to coalesce
1943 ;; base-strings with non-base-strings.
1946 ;; in the cross-compiler, we coalesce
1947 ;; all strings with the same contents,
1948 ;; because we will end up dumping them
1949 ;; as base-strings anyway. In the
1950 ;; real compiler, we're not allowed to
1951 ;; coalesce regardless of string
1952 ;; specialized element type, so we
1953 ;; KLUDGE by coalescing only character
1954 ;; strings (the common case) and
1955 ;; punting on the other types.
1959 (vector character))))))
1961 (if faslp (file-coalesce-p x) (core-coalesce-p x))))
1962 (if (and (boundp '*constants*) (coalescep object))
1963 (or (gethash object *constants*)
1964 (setf (gethash object *constants*)
1968 ;;; Return true if VAR would have to be closed over if environment
1969 ;;; analysis ran now (i.e. if there are any uses that have a different
1970 ;;; home lambda than VAR's home.)
1971 (defun closure-var-p (var)
1972 (declare (type lambda-var var))
1973 (let ((home (lambda-var-home var)))
1974 (cond ((eq (functional-kind home) :deleted)
1976 (t (let ((home (lambda-home home)))
1979 :key #'node-home-lambda
1981 (or (frob (leaf-refs var))
1982 (frob (basic-var-sets var)))))))))
1984 ;;; If there is a non-local exit noted in ENTRY's environment that
1985 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1986 (defun find-nlx-info (exit)
1987 (declare (type exit exit))
1988 (let* ((entry (exit-entry exit))
1989 (cleanup (entry-cleanup entry))
1990 (block (first (block-succ (node-block exit)))))
1991 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1992 (when (and (eq (nlx-info-block nlx) block)
1993 (eq (nlx-info-cleanup nlx) cleanup))
1996 (defun nlx-info-lvar (nlx)
1997 (declare (type nlx-info nlx))
1998 (node-lvar (block-last (nlx-info-target nlx))))
2000 ;;;; functional hackery
2002 (declaim (ftype (sfunction (functional) clambda) main-entry))
2003 (defun main-entry (functional)
2004 (etypecase functional
2005 (clambda functional)
2007 (optional-dispatch-main-entry functional))))
2009 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
2010 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
2011 ;;; optional with null default and no SUPPLIED-P. There must be a
2012 ;;; &REST arg with no references.
2013 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
2014 (defun looks-like-an-mv-bind (functional)
2015 (and (optional-dispatch-p functional)
2016 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
2018 (let ((info (lambda-var-arg-info (car arg))))
2019 (unless info (return nil))
2020 (case (arg-info-kind info)
2022 (when (or (arg-info-supplied-p info) (arg-info-default info))
2025 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
2029 ;;; Return true if function is an external entry point. This is true
2030 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
2031 ;;; (:TOPLEVEL kind.)
2033 (declare (type functional fun))
2034 (not (null (member (functional-kind fun) '(:external :toplevel)))))
2036 ;;; If LVAR's only use is a non-notinline global function reference,
2037 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
2038 ;;; is true, then we don't care if the leaf is NOTINLINE.
2039 (defun lvar-fun-name (lvar &optional notinline-ok)
2040 (declare (type lvar lvar))
2041 (let ((use (lvar-uses lvar)))
2043 (let ((leaf (ref-leaf use)))
2044 (if (and (global-var-p leaf)
2045 (eq (global-var-kind leaf) :global-function)
2046 (or (not (defined-fun-p leaf))
2047 (not (eq (defined-fun-inlinep leaf) :notinline))
2049 (leaf-source-name leaf)
2053 (defun lvar-fun-debug-name (lvar)
2054 (declare (type lvar lvar))
2055 (let ((uses (lvar-uses lvar)))
2057 (leaf-debug-name (ref-leaf use))))
2060 (mapcar #'name1 uses)))))
2062 ;;; Return the source name of a combination -- or signals an error
2063 ;;; if the function leaf is anonymous.
2064 (defun combination-fun-source-name (combination &optional (errorp t))
2065 (let ((leaf (ref-leaf (lvar-uses (combination-fun combination)))))
2066 (if (or errorp (leaf-has-source-name-p leaf))
2067 (values (leaf-source-name leaf) t)
2070 ;;; Return the COMBINATION node that is the call to the LET FUN.
2071 (defun let-combination (fun)
2072 (declare (type clambda fun))
2073 (aver (functional-letlike-p fun))
2074 (lvar-dest (node-lvar (first (leaf-refs fun)))))
2076 ;;; Return the initial value lvar for a LET variable, or NIL if there
2078 (defun let-var-initial-value (var)
2079 (declare (type lambda-var var))
2080 (let ((fun (lambda-var-home var)))
2081 (elt (combination-args (let-combination fun))
2082 (position-or-lose var (lambda-vars fun)))))
2084 ;;; Return the LAMBDA that is called by the local CALL.
2085 (defun combination-lambda (call)
2086 (declare (type basic-combination call))
2087 (aver (eq (basic-combination-kind call) :local))
2088 (ref-leaf (lvar-uses (basic-combination-fun call))))
2090 (defvar *inline-expansion-limit* 200
2092 "an upper limit on the number of inline function calls that will be expanded
2093 in any given code object (single function or block compilation)")
2095 ;;; Check whether NODE's component has exceeded its inline expansion
2096 ;;; limit, and warn if so, returning NIL.
2097 (defun inline-expansion-ok (node)
2098 (let ((expanded (incf (component-inline-expansions
2100 (node-block node))))))
2101 (cond ((> expanded *inline-expansion-limit*) nil)
2102 ((= expanded *inline-expansion-limit*)
2103 ;; FIXME: If the objective is to stop the recursive
2104 ;; expansion of inline functions, wouldn't it be more
2105 ;; correct to look back through surrounding expansions
2106 ;; (which are, I think, stored in the *CURRENT-PATH*, and
2107 ;; possibly stored elsewhere too) and suppress expansion
2108 ;; and print this warning when the function being proposed
2109 ;; for inline expansion is found there? (I don't like the
2110 ;; arbitrary numerical limit in principle, and I think
2111 ;; it'll be a nuisance in practice if we ever want the
2112 ;; compiler to be able to use WITH-COMPILATION-UNIT on
2113 ;; arbitrarily huge blocks of code. -- WHN)
2114 (let ((*compiler-error-context* node))
2115 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
2116 probably trying to~% ~
2117 inline a recursive function."
2118 *inline-expansion-limit*))
2122 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
2123 (defun assure-functional-live-p (functional)
2124 (declare (type functional functional))
2126 ;; looks LET-converted
2127 (functional-somewhat-letlike-p functional)
2128 ;; It's possible for a LET-converted function to end up
2129 ;; deleted later. In that case, for the purposes of this
2130 ;; analysis, it is LET-converted: LET-converted functionals
2131 ;; are too badly trashed to expand them inline, and deleted
2132 ;; LET-converted functionals are even worse.
2133 (memq (functional-kind functional) '(:deleted :zombie))))
2134 (throw 'locall-already-let-converted functional)))
2136 (defun assure-leaf-live-p (leaf)
2139 (when (lambda-var-deleted leaf)
2140 (throw 'locall-already-let-converted leaf)))
2142 (assure-functional-live-p leaf))))
2145 (defun call-full-like-p (call)
2146 (declare (type combination call))
2147 (let ((kind (basic-combination-kind call)))
2149 (and (eq kind :known)
2150 (let ((info (basic-combination-fun-info call)))
2152 (not (fun-info-ir2-convert info))
2153 (dolist (template (fun-info-templates info) t)
2154 (when (eq (template-ltn-policy template) :fast-safe)
2155 (multiple-value-bind (val win)
2156 (valid-fun-use call (template-type template))
2157 (when (or val (not win)) (return nil)))))))))))
2161 ;;; Apply a function to some arguments, returning a list of the values
2162 ;;; resulting of the evaluation. If an error is signalled during the
2163 ;;; application, then we produce a warning message using WARN-FUN and
2164 ;;; return NIL as our second value to indicate this. NODE is used as
2165 ;;; the error context for any error message, and CONTEXT is a string
2166 ;;; that is spliced into the warning.
2167 (declaim (ftype (sfunction ((or symbol function) list node function string)
2168 (values list boolean))
2170 (defun careful-call (function args node warn-fun context)
2172 (multiple-value-list
2173 (handler-case (apply function args)
2175 (let ((*compiler-error-context* node))
2176 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
2177 (return-from careful-call (values nil nil))))))
2180 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
2183 ((deffrob (basic careful compiler transform)
2185 (defun ,careful (specifier)
2186 (handler-case (,basic specifier)
2187 (sb!kernel::arg-count-error (condition)
2188 (values nil (list (format nil "~A" condition))))
2189 (simple-error (condition)
2190 (values nil (list* (simple-condition-format-control condition)
2191 (simple-condition-format-arguments condition))))))
2192 (defun ,compiler (specifier)
2193 (multiple-value-bind (type error-args) (,careful specifier)
2195 (apply #'compiler-error error-args))))
2196 (defun ,transform (specifier)
2197 (multiple-value-bind (type error-args) (,careful specifier)
2199 (apply #'give-up-ir1-transform
2201 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
2202 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
2205 ;;;; utilities used at run-time for parsing &KEY args in IR1
2207 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
2208 ;;; the lvar for the value of the &KEY argument KEY in the list of
2209 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
2210 ;;; otherwise. The legality and constantness of the keywords should
2211 ;;; already have been checked.
2212 (declaim (ftype (sfunction (list keyword) (or lvar null))
2214 (defun find-keyword-lvar (args key)
2215 (do ((arg args (cddr arg)))
2217 (when (eq (lvar-value (first arg)) key)
2218 (return (second arg)))))
2220 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2221 ;;; verify that alternating lvars in ARGS are constant and that there
2222 ;;; is an even number of args.
2223 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
2224 (defun check-key-args-constant (args)
2225 (do ((arg args (cddr arg)))
2227 (unless (and (rest arg)
2228 (constant-lvar-p (first arg)))
2231 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2232 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
2233 ;;; and that only keywords present in the list KEYS are supplied.
2234 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
2235 (defun check-transform-keys (args keys)
2236 (and (check-key-args-constant args)
2237 (do ((arg args (cddr arg)))
2239 (unless (member (lvar-value (first arg)) keys)
2244 ;;; Called by the expansion of the EVENT macro.
2245 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
2246 (defun %event (info node)
2247 (incf (event-info-count info))
2248 (when (and (>= (event-info-level info) *event-note-threshold*)
2249 (policy (or node *lexenv*)
2250 (= inhibit-warnings 0)))
2251 (let ((*compiler-error-context* node))
2252 (compiler-notify (event-info-description info))))
2254 (let ((action (event-info-action info)))
2255 (when action (funcall action node))))
2258 (defun make-cast (value type policy)
2259 (declare (type lvar value)
2261 (type policy policy))
2262 (%make-cast :asserted-type type
2263 :type-to-check (maybe-weaken-check type policy)
2265 :derived-type (coerce-to-values type)))
2267 (defun cast-type-check (cast)
2268 (declare (type cast cast))
2269 (when (cast-reoptimize cast)
2270 (ir1-optimize-cast cast t))
2271 (cast-%type-check cast))
2273 (defun note-single-valuified-lvar (lvar)
2274 (declare (type (or lvar null) lvar))
2276 (let ((use (lvar-uses lvar)))
2278 (let ((leaf (ref-leaf use)))
2279 (when (and (lambda-var-p leaf)
2280 (null (rest (leaf-refs leaf))))
2281 (reoptimize-lambda-var leaf))))
2282 ((or (listp use) (combination-p use))
2283 (do-uses (node lvar)
2284 (setf (node-reoptimize node) t)
2285 (setf (block-reoptimize (node-block node)) t)
2286 (reoptimize-component (node-component node) :maybe)))))))
2288 ;;; Return true if LVAR's only use is a reference to a global function
2289 ;;; designator with one of the specified NAMES, that hasn't been
2290 ;;; declared NOTINLINE.
2291 (defun lvar-fun-is (lvar names)
2292 (declare (type lvar lvar) (list names))
2293 (let ((use (lvar-uses lvar)))
2295 (let* ((*lexenv* (node-lexenv use))
2296 (leaf (ref-leaf use))
2298 (cond ((global-var-p leaf)
2300 (and (eq (global-var-kind leaf) :global-function)
2301 (car (member (leaf-source-name leaf) names
2304 (let ((value (constant-value leaf)))
2305 (car (if (functionp value)
2310 (fdefinition name)))
2314 :test #'equal))))))))
2316 (not (fun-lexically-notinline-p name)))))))
2318 ;;; Return true if LVAR's only use is a call to one of the named functions
2319 ;;; (or any function if none are specified) with the specified number of
2320 ;;; of arguments (or any number if number is not specified)
2321 (defun lvar-matches (lvar &key fun-names arg-count)
2322 (let ((use (lvar-uses lvar)))
2323 (and (combination-p use)
2325 (multiple-value-bind (name ok)
2326 (combination-fun-source-name use nil)
2327 (and ok (member name fun-names :test #'eq))))
2329 (= arg-count (length (combination-args use)))))))
2331 ;;; True if the optional has a rest-argument.
2332 (defun optional-rest-p (opt)
2333 (dolist (var (optional-dispatch-arglist opt) nil)
2334 (let* ((info (when (lambda-var-p var)
2335 (lambda-var-arg-info var)))
2337 (arg-info-kind info))))
2338 (when (eq :rest kind)
2341 ;;; Don't substitute single-ref variables on high-debug / low speed, to
2342 ;;; improve the debugging experience. ...but don't bother keeping those
2343 ;;; from system lambdas.
2344 (defun preserve-single-use-debug-var-p (call var)
2345 (and (policy call (eql preserve-single-use-debug-variables 3))
2346 (or (not (lambda-var-p var))
2347 (not (lambda-system-lambda-p (lambda-var-home var))))))