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 ;;;; lvar substitution
154 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
155 ;;; NIL. We do not flush OLD's DEST.
156 (defun substitute-lvar (new old)
157 (declare (type lvar old new))
158 (aver (not (lvar-dest new)))
159 (let ((dest (lvar-dest old)))
162 (cif (setf (if-test dest) new))
163 (cset (setf (set-value dest) new))
164 (creturn (setf (return-result dest) new))
165 (exit (setf (exit-value dest) new))
167 (if (eq old (basic-combination-fun dest))
168 (setf (basic-combination-fun dest) new)
169 (setf (basic-combination-args dest)
170 (nsubst new old (basic-combination-args dest)))))
171 (cast (setf (cast-value dest) new)))
173 (setf (lvar-dest old) nil)
174 (setf (lvar-dest new) dest)
175 (flush-lvar-externally-checkable-type new))
178 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
179 ;;; arbitary number of uses. NEW is supposed to be "later" than OLD.
180 (defun substitute-lvar-uses (new old propagate-dx)
181 (declare (type lvar old)
182 (type (or lvar null) new)
183 (type boolean propagate-dx))
187 (%delete-lvar-use node)
188 (add-lvar-use node new))
189 (reoptimize-lvar new)
190 (awhen (and propagate-dx (lvar-dynamic-extent old))
191 (setf (lvar-dynamic-extent old) nil)
192 (unless (lvar-dynamic-extent new)
193 (setf (lvar-dynamic-extent new) it)
194 (setf (cleanup-info it) (subst new old (cleanup-info it)))))
195 (when (lvar-dynamic-extent new)
197 (node-ends-block node))))
198 (t (flush-dest old)))
202 ;;;; block starting/creation
204 ;;; Return the block that CTRAN is the start of, making a block if
205 ;;; necessary. This function is called by IR1 translators which may
206 ;;; cause a CTRAN to be used more than once. Every CTRAN which may be
207 ;;; used more than once must start a block by the time that anyone
208 ;;; does a USE-CTRAN on it.
210 ;;; We also throw the block into the next/prev list for the
211 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
213 (defun ctran-starts-block (ctran)
214 (declare (type ctran ctran))
215 (ecase (ctran-kind ctran)
217 (aver (not (ctran-block ctran)))
218 (let* ((next (component-last-block *current-component*))
219 (prev (block-prev next))
220 (new-block (make-block ctran)))
221 (setf (block-next new-block) next
222 (block-prev new-block) prev
223 (block-prev next) new-block
224 (block-next prev) new-block
225 (ctran-block ctran) new-block
226 (ctran-kind ctran) :block-start)
227 (aver (not (ctran-use ctran)))
230 (ctran-block ctran))))
232 ;;; Ensure that CTRAN is the start of a block so that the use set can
233 ;;; be freely manipulated.
234 (defun ensure-block-start (ctran)
235 (declare (type ctran ctran))
236 (let ((kind (ctran-kind ctran)))
240 (setf (ctran-block ctran)
241 (make-block-key :start ctran))
242 (setf (ctran-kind ctran) :block-start))
244 (node-ends-block (ctran-use ctran)))))
247 ;;; CTRAN must be the last ctran in an incomplete block; finish the
248 ;;; block and start a new one if necessary.
249 (defun start-block (ctran)
250 (declare (type ctran ctran))
251 (aver (not (ctran-next ctran)))
252 (ecase (ctran-kind ctran)
254 (let ((block (ctran-block ctran))
255 (node (ctran-use ctran)))
256 (aver (not (block-last block)))
258 (setf (block-last block) node)
259 (setf (node-next node) nil)
260 (setf (ctran-use ctran) nil)
261 (setf (ctran-kind ctran) :unused)
262 (setf (ctran-block ctran) nil)
263 (link-blocks block (ctran-starts-block ctran))))
268 ;;; Filter values of LVAR through FORM, which must be an ordinary/mv
269 ;;; call. First argument must be 'DUMMY, which will be replaced with
270 ;;; LVAR. In case of an ordinary call the function should not have
271 ;;; return type NIL. We create a new "filtered" lvar.
273 ;;; TODO: remove preconditions.
274 (defun filter-lvar (lvar form)
275 (declare (type lvar lvar) (type list form))
276 (let* ((dest (lvar-dest lvar))
277 (ctran (node-prev dest)))
278 (with-ir1-environment-from-node dest
280 (ensure-block-start ctran)
281 (let* ((old-block (ctran-block ctran))
282 (new-start (make-ctran))
283 (filtered-lvar (make-lvar))
284 (new-block (ctran-starts-block new-start)))
286 ;; Splice in the new block before DEST, giving the new block
287 ;; all of DEST's predecessors.
288 (dolist (block (block-pred old-block))
289 (change-block-successor block old-block new-block))
291 (ir1-convert new-start ctran filtered-lvar form)
293 ;; KLUDGE: Comments at the head of this function in CMU CL
294 ;; said that somewhere in here we
295 ;; Set the new block's start and end cleanups to the *start*
296 ;; cleanup of PREV's block. This overrides the incorrect
297 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
298 ;; Unfortunately I can't find any code which corresponds to this.
299 ;; Perhaps it was a stale comment? Or perhaps I just don't
300 ;; understand.. -- WHN 19990521
302 ;; Replace 'DUMMY with the LVAR. (We can find 'DUMMY because
303 ;; no LET conversion has been done yet.) The [mv-]combination
304 ;; code from the call in the form will be the use of the new
305 ;; check lvar. We substitute for the first argument of
307 (let* ((node (lvar-use filtered-lvar))
308 (args (basic-combination-args node))
309 (victim (first args)))
310 (aver (eq (constant-value (ref-leaf (lvar-use victim)))
313 (substitute-lvar filtered-lvar lvar)
314 (substitute-lvar lvar victim)
317 ;; Invoking local call analysis converts this call to a LET.
318 (locall-analyze-component *current-component*))))
321 ;;; Delete NODE and VALUE. It may result in some calls becoming tail.
322 (defun delete-filter (node lvar value)
323 (aver (eq (lvar-dest value) node))
324 (aver (eq (node-lvar node) lvar))
325 (cond (lvar (collect ((merges))
326 (when (return-p (lvar-dest lvar))
328 (when (and (basic-combination-p use)
329 (eq (basic-combination-kind use) :local))
331 (substitute-lvar-uses lvar value
332 (and lvar (eq (lvar-uses lvar) node)))
333 (%delete-lvar-use node)
336 (dolist (merge (merges))
337 (merge-tail-sets merge)))))
338 (t (flush-dest value)
339 (unlink-node node))))
341 ;;; Make a CAST and insert it into IR1 before node NEXT.
342 (defun insert-cast-before (next lvar type policy)
343 (declare (type node next) (type lvar lvar) (type ctype type))
344 (with-ir1-environment-from-node next
345 (let* ((ctran (node-prev next))
346 (cast (make-cast lvar type policy))
347 (internal-ctran (make-ctran)))
348 (setf (ctran-next ctran) cast
349 (node-prev cast) ctran)
350 (use-ctran cast internal-ctran)
351 (link-node-to-previous-ctran next internal-ctran)
352 (setf (lvar-dest lvar) cast)
353 (reoptimize-lvar lvar)
354 (when (return-p next)
355 (node-ends-block cast))
356 (setf (block-attributep (block-flags (node-block cast))
357 type-check type-asserted)
361 ;;;; miscellaneous shorthand functions
363 ;;; Return the home (i.e. enclosing non-LET) CLAMBDA for NODE. Since
364 ;;; the LEXENV-LAMBDA may be deleted, we must chain up the
365 ;;; LAMBDA-CALL-LEXENV thread until we find a CLAMBDA that isn't
366 ;;; deleted, and then return its home.
367 (defun node-home-lambda (node)
368 (declare (type node node))
369 (do ((fun (lexenv-lambda (node-lexenv node))
370 (lexenv-lambda (lambda-call-lexenv fun))))
371 ((not (memq (functional-kind fun) '(:deleted :zombie)))
373 (when (eq (lambda-home fun) fun)
376 #!-sb-fluid (declaim (inline node-block))
377 (defun node-block (node)
378 (ctran-block (node-prev node)))
379 (declaim (ftype (sfunction (node) component) node-component))
380 (defun node-component (node)
381 (block-component (node-block node)))
382 (declaim (ftype (sfunction (node) physenv) node-physenv))
383 (defun node-physenv (node)
384 (lambda-physenv (node-home-lambda node)))
385 #!-sb-fluid (declaim (inline node-dest))
386 (defun node-dest (node)
387 (awhen (node-lvar node) (lvar-dest it)))
389 #!-sb-fluid (declaim (inline node-stack-allocate-p))
390 (defun node-stack-allocate-p (node)
391 (awhen (node-lvar node)
392 (lvar-dynamic-extent it)))
394 (declaim (ftype (sfunction (node (member nil t :truly) &optional (or null component))
395 boolean) use-good-for-dx-p))
396 (declaim (ftype (sfunction (lvar (member nil t :truly) &optional (or null component))
397 boolean) lvar-good-for-dx-p))
398 (defun use-good-for-dx-p (use dx &optional component)
399 ;; FIXME: Can casts point to LVARs in other components?
400 ;; RECHECK-DYNAMIC-EXTENT-LVARS assumes that they can't -- that is, that the
401 ;; PRINCIPAL-LVAR is always in the same component as the original one. It
402 ;; would be either good to have an explanation of why casts don't point
403 ;; across components, or an explanation of when they do it. ...in the
404 ;; meanwhile AVER that our assumption holds true.
405 (aver (or (not component) (eq component (node-component use))))
406 (or (dx-combination-p use dx)
408 (not (cast-type-check use))
409 (lvar-good-for-dx-p (cast-value use) dx component))
410 (and (trivial-lambda-var-ref-p use)
411 (let ((uses (lvar-uses (trivial-lambda-var-ref-lvar use))))
413 (lvar-good-for-dx-p (trivial-lambda-var-ref-lvar use) dx component))))))
415 (defun lvar-good-for-dx-p (lvar dx &optional component)
416 (let ((uses (lvar-uses lvar)))
419 (use-good-for-dx-p use dx component))
421 (use-good-for-dx-p uses dx component))))
423 (defun known-dx-combination-p (use dx)
424 (and (eq (combination-kind use) :known)
425 (let ((info (combination-fun-info use)))
426 (or (awhen (fun-info-stack-allocate-result info)
428 (awhen (fun-info-result-arg info)
429 (let ((args (combination-args use)))
430 (lvar-good-for-dx-p (if (zerop it)
435 (defun dx-combination-p (use dx)
436 (and (combination-p use)
438 ;; Known, and can do DX.
439 (known-dx-combination-p use dx)
440 ;; Possibly a not-yet-eliminated lambda which ends up returning the
441 ;; results of an actual known DX combination.
442 (let* ((fun (combination-fun use))
443 (ref (principal-lvar-use fun))
444 (clambda (when (ref-p ref)
446 (creturn (when (lambda-p clambda)
447 (lambda-return clambda)))
448 (result-use (when (return-p creturn)
449 (principal-lvar-use (return-result creturn)))))
450 ;; FIXME: We should be able to deal with multiple uses here as well.
451 (and (dx-combination-p result-use dx)
452 (combination-args-flow-cleanly-p use result-use dx))))))
454 (defun combination-args-flow-cleanly-p (combination1 combination2 dx)
455 (labels ((recurse (combination)
456 (or (eq combination combination2)
457 (if (known-dx-combination-p combination dx)
458 (let ((dest (lvar-dest (combination-lvar combination))))
459 (and (combination-p dest)
461 (let* ((fun1 (combination-fun combination))
462 (ref1 (principal-lvar-use fun1))
463 (clambda1 (when (ref-p ref1) (ref-leaf ref1))))
464 (when (lambda-p clambda1)
465 (dolist (var (lambda-vars clambda1) t)
466 (dolist (var-ref (lambda-var-refs var))
467 (let ((dest (lvar-dest (ref-lvar var-ref))))
468 (unless (and (combination-p dest) (recurse dest))
469 (return-from combination-args-flow-cleanly-p nil)))))))))))
470 (recurse combination1)))
472 (defun trivial-lambda-var-ref-p (use)
474 (let ((var (ref-leaf use)))
475 ;; lambda-var, no SETS
476 (when (and (lambda-var-p var) (not (lambda-var-sets var)))
477 (let ((home (lambda-var-home var))
478 (refs (lambda-var-refs var)))
479 ;; bound by a system lambda, no other REFS
480 (when (and (lambda-system-lambda-p home)
481 (eq use (car refs)) (not (cdr refs)))
482 ;; the LAMBDA this var is bound by has only a single REF, going
484 (let* ((lambda-refs (lambda-refs home))
485 (primary (car lambda-refs)))
487 (not (cdr lambda-refs))
488 (combination-p (lvar-dest (ref-lvar primary)))))))))))
490 (defun trivial-lambda-var-ref-lvar (use)
491 (let* ((this (ref-leaf use))
492 (home (lambda-var-home this)))
493 (multiple-value-bind (fun vars)
494 (values home (lambda-vars home))
495 (let* ((combination (lvar-dest (ref-lvar (car (lambda-refs fun)))))
496 (args (combination-args combination)))
497 (assert (= (length vars) (length args)))
498 (loop for var in vars
503 (declaim (inline block-to-be-deleted-p))
504 (defun block-to-be-deleted-p (block)
505 (or (block-delete-p block)
506 (eq (functional-kind (block-home-lambda block)) :deleted)))
508 ;;; Checks whether NODE is in a block to be deleted
509 (declaim (inline node-to-be-deleted-p))
510 (defun node-to-be-deleted-p (node)
511 (block-to-be-deleted-p (node-block node)))
513 (declaim (ftype (sfunction (clambda) cblock) lambda-block))
514 (defun lambda-block (clambda)
515 (node-block (lambda-bind clambda)))
516 (declaim (ftype (sfunction (clambda) component) lambda-component))
517 (defun lambda-component (clambda)
518 (block-component (lambda-block clambda)))
520 (declaim (ftype (sfunction (cblock) node) block-start-node))
521 (defun block-start-node (block)
522 (ctran-next (block-start block)))
524 ;;; Return the enclosing cleanup for environment of the first or last
526 (defun block-start-cleanup (block)
527 (node-enclosing-cleanup (block-start-node block)))
528 (defun block-end-cleanup (block)
529 (node-enclosing-cleanup (block-last block)))
531 ;;; Return the non-LET LAMBDA that holds BLOCK's code, or NIL
532 ;;; if there is none.
534 ;;; There can legitimately be no home lambda in dead code early in the
535 ;;; IR1 conversion process, e.g. when IR1-converting the SETQ form in
536 ;;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
537 ;;; where the block is just a placeholder during parsing and doesn't
538 ;;; actually correspond to code which will be written anywhere.
539 (declaim (ftype (sfunction (cblock) (or clambda null)) block-home-lambda-or-null))
540 (defun block-home-lambda-or-null (block)
541 (if (node-p (block-last block))
542 ;; This is the old CMU CL way of doing it.
543 (node-home-lambda (block-last block))
544 ;; Now that SBCL uses this operation more aggressively than CMU
545 ;; CL did, the old CMU CL way of doing it can fail in two ways.
546 ;; 1. It can fail in a few cases even when a meaningful home
547 ;; lambda exists, e.g. in IR1-CONVERT of one of the legs of
549 ;; 2. It can fail when converting a form which is born orphaned
550 ;; so that it never had a meaningful home lambda, e.g. a form
551 ;; which follows a RETURN-FROM or GO form.
552 (let ((pred-list (block-pred block)))
553 ;; To deal with case 1, we reason that
554 ;; previous-in-target-execution-order blocks should be in the
555 ;; same lambda, and that they seem in practice to be
556 ;; previous-in-compilation-order blocks too, so we look back
557 ;; to find one which is sufficiently initialized to tell us
558 ;; what the home lambda is.
560 ;; We could get fancy about this, flooding through the
561 ;; graph of all the previous blocks, but in practice it
562 ;; seems to work just to grab the first previous block and
564 (node-home-lambda (block-last (first pred-list)))
565 ;; In case 2, we end up with an empty PRED-LIST and
566 ;; have to punt: There's no home lambda.
569 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
570 (declaim (ftype (sfunction (cblock) clambda) block-home-lambda))
571 (defun block-home-lambda (block)
572 (block-home-lambda-or-null block))
574 ;;; Return the IR1 physical environment for BLOCK.
575 (declaim (ftype (sfunction (cblock) physenv) block-physenv))
576 (defun block-physenv (block)
577 (lambda-physenv (block-home-lambda block)))
579 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
580 ;;; of its original source's top level form in its compilation unit.
581 (defun source-path-tlf-number (path)
582 (declare (list path))
585 ;;; Return the (reversed) list for the PATH in the original source
586 ;;; (with the Top Level Form number last).
587 (defun source-path-original-source (path)
588 (declare (list path) (inline member))
589 (cddr (member 'original-source-start path :test #'eq)))
591 ;;; Return the Form Number of PATH's original source inside the Top
592 ;;; Level Form that contains it. This is determined by the order that
593 ;;; we walk the subforms of the top level source form.
594 (defun source-path-form-number (path)
595 (declare (list path) (inline member))
596 (cadr (member 'original-source-start path :test #'eq)))
598 ;;; Return a list of all the enclosing forms not in the original
599 ;;; source that converted to get to this form, with the immediate
600 ;;; source for node at the start of the list.
601 (defun source-path-forms (path)
602 (subseq path 0 (position 'original-source-start path)))
604 ;;; Return the innermost source form for NODE.
605 (defun node-source-form (node)
606 (declare (type node node))
607 (let* ((path (node-source-path node))
608 (forms (source-path-forms path)))
611 (values (find-original-source path)))))
613 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
615 (defun lvar-source (lvar)
616 (let ((use (lvar-uses lvar)))
619 (values (node-source-form use) t))))
621 ;;; Return the unique node, delivering a value to LVAR.
622 #!-sb-fluid (declaim (inline lvar-use))
623 (defun lvar-use (lvar)
624 (the (not list) (lvar-uses lvar)))
626 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
627 (defun lvar-has-single-use-p (lvar)
628 (typep (lvar-uses lvar) '(not list)))
630 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
631 (declaim (ftype (sfunction (ctran) (or clambda null))
632 ctran-home-lambda-or-null))
633 (defun ctran-home-lambda-or-null (ctran)
634 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
635 ;; implementation might not be quite right, or might be uglier than
636 ;; necessary. It appears that the original Python never found a need
637 ;; to do this operation. The obvious things based on
638 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
639 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
640 ;; generalize it enough to grovel harder when the simple CMU CL
641 ;; approach fails, and furthermore realize that in some exceptional
642 ;; cases it might return NIL. -- WHN 2001-12-04
643 (cond ((ctran-use ctran)
644 (node-home-lambda (ctran-use ctran)))
646 (block-home-lambda-or-null (ctran-block ctran)))
648 (bug "confused about home lambda for ~S" ctran))))
650 ;;; Return the LAMBDA that is CTRAN's home.
651 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
652 (defun ctran-home-lambda (ctran)
653 (ctran-home-lambda-or-null ctran))
655 (declaim (inline cast-single-value-p))
656 (defun cast-single-value-p (cast)
657 (not (values-type-p (cast-asserted-type cast))))
659 #!-sb-fluid (declaim (inline lvar-single-value-p))
660 (defun lvar-single-value-p (lvar)
662 (let ((dest (lvar-dest lvar)))
667 (eq (basic-combination-fun dest) lvar))
670 (declare (notinline lvar-single-value-p))
671 (and (cast-single-value-p dest)
672 (lvar-single-value-p (node-lvar dest)))))
676 (defun principal-lvar-end (lvar)
677 (loop for prev = lvar then (node-lvar dest)
678 for dest = (and prev (lvar-dest prev))
680 finally (return (values dest prev))))
682 (defun principal-lvar-single-valuify (lvar)
683 (loop for prev = lvar then (node-lvar dest)
684 for dest = (and prev (lvar-dest prev))
686 do (setf (node-derived-type dest)
687 (make-short-values-type (list (single-value-type
688 (node-derived-type dest)))))
689 (reoptimize-lvar prev)))
691 ;;; Return a new LEXENV just like DEFAULT except for the specified
692 ;;; slot values. Values for the alist slots are NCONCed to the
693 ;;; beginning of the current value, rather than replacing it entirely.
694 (defun make-lexenv (&key (default *lexenv*)
695 funs vars blocks tags
697 (lambda (lexenv-lambda default))
698 (cleanup (lexenv-cleanup default))
699 (handled-conditions (lexenv-handled-conditions default))
700 (disabled-package-locks
701 (lexenv-disabled-package-locks default))
702 (policy (lexenv-policy default)))
703 (macrolet ((frob (var slot)
704 `(let ((old (,slot default)))
708 (internal-make-lexenv
709 (frob funs lexenv-funs)
710 (frob vars lexenv-vars)
711 (frob blocks lexenv-blocks)
712 (frob tags lexenv-tags)
713 (frob type-restrictions lexenv-type-restrictions)
714 lambda cleanup handled-conditions
715 disabled-package-locks policy)))
717 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
719 (defun make-restricted-lexenv (lexenv)
720 (flet ((fun-good-p (fun)
721 (destructuring-bind (name . thing) fun
722 (declare (ignore name))
726 (cons (aver (eq (car thing) 'macro))
729 (destructuring-bind (name . thing) var
730 (declare (ignore name))
732 ;; The evaluator will mark lexicals with :BOGUS when it
733 ;; translates an interpreter lexenv to a compiler
735 ((or leaf #!+sb-eval (member :bogus)) nil)
736 (cons (aver (eq (car thing) 'macro))
738 (heap-alien-info nil)))))
739 (internal-make-lexenv
740 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
741 (remove-if-not #'var-good-p (lexenv-vars lexenv))
744 (lexenv-type-restrictions lexenv) ; XXX
747 (lexenv-handled-conditions lexenv)
748 (lexenv-disabled-package-locks lexenv)
749 (lexenv-policy lexenv))))
751 ;;;; flow/DFO/component hackery
753 ;;; Join BLOCK1 and BLOCK2.
754 (defun link-blocks (block1 block2)
755 (declare (type cblock block1 block2))
756 (setf (block-succ block1)
757 (if (block-succ block1)
758 (%link-blocks block1 block2)
760 (push block1 (block-pred block2))
762 (defun %link-blocks (block1 block2)
763 (declare (type cblock block1 block2))
764 (let ((succ1 (block-succ block1)))
765 (aver (not (memq block2 succ1)))
766 (cons block2 succ1)))
768 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
769 ;;; this leaves a successor with a single predecessor that ends in an
770 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
771 ;;; now be able to be propagated to the successor.
772 (defun unlink-blocks (block1 block2)
773 (declare (type cblock block1 block2))
774 (let ((succ1 (block-succ block1)))
775 (if (eq block2 (car succ1))
776 (setf (block-succ block1) (cdr succ1))
777 (do ((succ (cdr succ1) (cdr succ))
779 ((eq (car succ) block2)
780 (setf (cdr prev) (cdr succ)))
783 (let ((new-pred (delq block1 (block-pred block2))))
784 (setf (block-pred block2) new-pred)
785 (when (singleton-p new-pred)
786 (let ((pred-block (first new-pred)))
787 (when (if-p (block-last pred-block))
788 (setf (block-test-modified pred-block) t)))))
791 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
792 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
793 ;;; consequent/alternative blocks to point to NEW. We also set
794 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
795 ;;; the new successor.
796 (defun change-block-successor (block old new)
797 (declare (type cblock new old block))
798 (unlink-blocks block old)
799 (let ((last (block-last block))
800 (comp (block-component block)))
801 (setf (component-reanalyze comp) t)
804 (setf (block-test-modified block) t)
805 (let* ((succ-left (block-succ block))
806 (new (if (and (eq new (component-tail comp))
810 (unless (memq new succ-left)
811 (link-blocks block new))
812 (macrolet ((frob (slot)
813 `(when (eq (,slot last) old)
814 (setf (,slot last) new))))
816 (frob if-alternative)
817 (when (eq (if-consequent last)
818 (if-alternative last))
819 (reoptimize-component (block-component block) :maybe)))))
821 (unless (memq new (block-succ block))
822 (link-blocks block new)))))
826 ;;; Unlink a block from the next/prev chain. We also null out the
828 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
829 (defun remove-from-dfo (block)
830 (let ((next (block-next block))
831 (prev (block-prev block)))
832 (setf (block-component block) nil)
833 (setf (block-next prev) next)
834 (setf (block-prev next) prev))
837 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
838 ;;; COMPONENT to be the same as for AFTER.
839 (defun add-to-dfo (block after)
840 (declare (type cblock block after))
841 (let ((next (block-next after))
842 (comp (block-component after)))
843 (aver (not (eq (component-kind comp) :deleted)))
844 (setf (block-component block) comp)
845 (setf (block-next after) block)
846 (setf (block-prev block) after)
847 (setf (block-next block) next)
848 (setf (block-prev next) block))
851 ;;; List all NLX-INFOs which BLOCK can exit to.
853 ;;; We hope that no cleanup actions are performed in the middle of
854 ;;; BLOCK, so it is enough to look only at cleanups in the block
855 ;;; end. The tricky thing is a special cleanup block; all its nodes
856 ;;; have the same cleanup info, corresponding to the start, so the
857 ;;; same approach returns safe result.
858 (defun map-block-nlxes (fun block &optional dx-cleanup-fun)
859 (loop for cleanup = (block-end-cleanup block)
860 then (node-enclosing-cleanup (cleanup-mess-up cleanup))
862 do (let ((mess-up (cleanup-mess-up cleanup)))
863 (case (cleanup-kind cleanup)
865 (aver (entry-p mess-up))
866 (loop for exit in (entry-exits mess-up)
867 for nlx-info = (exit-nlx-info exit)
868 do (funcall fun nlx-info)))
869 ((:catch :unwind-protect)
870 (aver (combination-p mess-up))
871 (let* ((arg-lvar (first (basic-combination-args mess-up)))
872 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
873 (funcall fun nlx-info)))
876 (funcall dx-cleanup-fun cleanup)))))))
878 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
879 ;;; the head and tail which are set to T.
880 (declaim (ftype (sfunction (component) (values)) clear-flags))
881 (defun clear-flags (component)
882 (let ((head (component-head component))
883 (tail (component-tail component)))
884 (setf (block-flag head) t)
885 (setf (block-flag tail) t)
886 (do-blocks (block component)
887 (setf (block-flag block) nil)))
890 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
891 ;;; true in the head and tail blocks.
892 (declaim (ftype (sfunction () component) make-empty-component))
893 (defun make-empty-component ()
894 (let* ((head (make-block-key :start nil :component nil))
895 (tail (make-block-key :start nil :component nil))
896 (res (make-component head tail)))
897 (setf (block-flag head) t)
898 (setf (block-flag tail) t)
899 (setf (block-component head) res)
900 (setf (block-component tail) res)
901 (setf (block-next head) tail)
902 (setf (block-prev tail) head)
905 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
906 ;;; The new block is added to the DFO immediately following NODE's block.
907 (defun node-ends-block (node)
908 (declare (type node node))
909 (let* ((block (node-block node))
910 (start (node-next node))
911 (last (block-last block)))
912 (check-type last node)
913 (unless (eq last node)
914 (aver (and (eq (ctran-kind start) :inside-block)
915 (not (block-delete-p block))))
916 (let* ((succ (block-succ block))
918 (make-block-key :start start
919 :component (block-component block)
920 :succ succ :last last)))
921 (setf (ctran-kind start) :block-start)
922 (setf (ctran-use start) nil)
923 (setf (block-last block) node)
924 (setf (node-next node) nil)
927 (cons new-block (remove block (block-pred b)))))
928 (setf (block-succ block) ())
929 (link-blocks block new-block)
930 (add-to-dfo new-block block)
931 (setf (component-reanalyze (block-component block)) t)
933 (do ((ctran start (node-next (ctran-next ctran))))
935 (setf (ctran-block ctran) new-block))
937 (setf (block-type-asserted block) t)
938 (setf (block-test-modified block) t))))
943 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
944 (defun delete-lambda-var (leaf)
945 (declare (type lambda-var leaf))
947 ;; Iterate over all local calls flushing the corresponding argument,
948 ;; allowing the computation of the argument to be deleted. We also
949 ;; mark the LET for reoptimization, since it may be that we have
950 ;; deleted its last variable.
951 (let* ((fun (lambda-var-home leaf))
952 (n (position leaf (lambda-vars fun))))
953 (dolist (ref (leaf-refs fun))
954 (let* ((lvar (node-lvar ref))
955 (dest (and lvar (lvar-dest lvar))))
956 (when (and (combination-p dest)
957 (eq (basic-combination-fun dest) lvar)
958 (eq (basic-combination-kind dest) :local))
959 (let* ((args (basic-combination-args dest))
961 (reoptimize-lvar arg)
963 (setf (elt args n) nil))))))
965 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
966 ;; too much difficulty, since we can efficiently implement
967 ;; write-only variables. We iterate over the SETs, marking their
968 ;; blocks for dead code flushing, since we can delete SETs whose
970 (dolist (set (lambda-var-sets leaf))
971 (setf (block-flush-p (node-block set)) t))
975 ;;; Note that something interesting has happened to VAR.
976 (defun reoptimize-lambda-var (var)
977 (declare (type lambda-var var))
978 (let ((fun (lambda-var-home var)))
979 ;; We only deal with LET variables, marking the corresponding
980 ;; initial value arg as needing to be reoptimized.
981 (when (and (eq (functional-kind fun) :let)
983 (do ((args (basic-combination-args
984 (lvar-dest (node-lvar (first (leaf-refs fun)))))
986 (vars (lambda-vars fun) (cdr vars)))
988 (reoptimize-lvar (car args))))))
991 ;;; Delete a function that has no references. This need only be called
992 ;;; on functions that never had any references, since otherwise
993 ;;; DELETE-REF will handle the deletion.
994 (defun delete-functional (fun)
995 (aver (and (null (leaf-refs fun))
996 (not (functional-entry-fun fun))))
998 (optional-dispatch (delete-optional-dispatch fun))
999 (clambda (delete-lambda fun)))
1002 ;;; Deal with deleting the last reference to a CLAMBDA, which means
1003 ;;; that the lambda is unreachable, so that its body may be
1004 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
1005 ;;; IR1-OPTIMIZE to delete its blocks.
1006 (defun delete-lambda (clambda)
1007 (declare (type clambda clambda))
1008 (let ((original-kind (functional-kind clambda))
1009 (bind (lambda-bind clambda)))
1010 (aver (not (member original-kind '(:deleted :toplevel))))
1011 (aver (not (functional-has-external-references-p clambda)))
1012 (aver (or (eq original-kind :zombie) bind))
1013 (setf (functional-kind clambda) :deleted)
1014 (setf (lambda-bind clambda) nil)
1016 (labels ((delete-children (lambda)
1017 (dolist (child (lambda-children lambda))
1018 (cond ((eq (functional-kind child) :deleted)
1019 (delete-children child))
1021 (delete-lambda child))))
1022 (setf (lambda-children lambda) nil)
1023 (setf (lambda-parent lambda) nil)))
1024 (delete-children clambda))
1026 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
1027 ;; that we're using the old value of the KIND slot, not the
1028 ;; current slot value, which has now been set to :DELETED.)
1031 ((:let :mv-let :assignment)
1032 (let ((bind-block (node-block bind)))
1033 (mark-for-deletion bind-block))
1034 (let ((home (lambda-home clambda)))
1035 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1036 ;; KLUDGE: In presence of NLEs we cannot always understand that
1037 ;; LET's BIND dominates its body [for a LET "its" body is not
1038 ;; quite its]; let's delete too dangerous for IR2 stuff. --
1040 (dolist (var (lambda-vars clambda))
1041 (flet ((delete-node (node)
1042 (mark-for-deletion (node-block node))))
1043 (mapc #'delete-node (leaf-refs var))
1044 (mapc #'delete-node (lambda-var-sets var)))))
1046 ;; Function has no reachable references.
1047 (dolist (ref (lambda-refs clambda))
1048 (mark-for-deletion (node-block ref)))
1049 ;; If the function isn't a LET, we unlink the function head
1050 ;; and tail from the component head and tail to indicate that
1051 ;; the code is unreachable. We also delete the function from
1052 ;; COMPONENT-LAMBDAS (it won't be there before local call
1053 ;; analysis, but no matter.) If the lambda was never
1054 ;; referenced, we give a note.
1055 (let* ((bind-block (node-block bind))
1056 (component (block-component bind-block))
1057 (return (lambda-return clambda))
1058 (return-block (and return (node-block return))))
1059 (unless (leaf-ever-used clambda)
1060 (let ((*compiler-error-context* bind))
1061 (compiler-notify 'code-deletion-note
1062 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
1063 :format-arguments (list (leaf-debug-name clambda)))))
1064 (unless (block-delete-p bind-block)
1065 (unlink-blocks (component-head component) bind-block))
1066 (when (and return-block (not (block-delete-p return-block)))
1067 (mark-for-deletion return-block)
1068 (unlink-blocks return-block (component-tail component)))
1069 (setf (component-reanalyze component) t)
1070 (let ((tails (lambda-tail-set clambda)))
1071 (setf (tail-set-funs tails)
1072 (delete clambda (tail-set-funs tails)))
1073 (setf (lambda-tail-set clambda) nil))
1074 (setf (component-lambdas component)
1075 (delq clambda (component-lambdas component))))))
1077 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
1078 ;; ENTRY-FUN so that people will know that it is not an entry
1080 (when (eq original-kind :external)
1081 (let ((fun (functional-entry-fun clambda)))
1082 (setf (functional-entry-fun fun) nil)
1083 (when (optional-dispatch-p fun)
1084 (delete-optional-dispatch fun)))))
1088 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
1089 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
1090 ;;; is used both before and after local call analysis. Afterward, all
1091 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
1092 ;;; to the XEP, leaving it with no references at all. So we look at
1093 ;;; the XEP to see whether an optional-dispatch is still really being
1094 ;;; used. But before local call analysis, there are no XEPs, and all
1095 ;;; references are direct.
1097 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
1098 ;;; entry-points, making them be normal lambdas, and then deleting the
1099 ;;; ones with no references. This deletes any e-p lambdas that were
1100 ;;; either never referenced, or couldn't be deleted when the last
1101 ;;; reference was deleted (due to their :OPTIONAL kind.)
1103 ;;; Note that the last optional entry point may alias the main entry,
1104 ;;; so when we process the main entry, its KIND may have been changed
1105 ;;; to NIL or even converted to a LETlike value.
1106 (defun delete-optional-dispatch (leaf)
1107 (declare (type optional-dispatch leaf))
1108 (let ((entry (functional-entry-fun leaf)))
1109 (unless (and entry (leaf-refs entry))
1110 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
1111 (setf (functional-kind leaf) :deleted)
1114 (unless (eq (functional-kind fun) :deleted)
1115 (aver (eq (functional-kind fun) :optional))
1116 (setf (functional-kind fun) nil)
1117 (let ((refs (leaf-refs fun)))
1119 (delete-lambda fun))
1121 (or (maybe-let-convert fun)
1122 (maybe-convert-to-assignment fun)))
1124 (maybe-convert-to-assignment fun)))))))
1126 (dolist (ep (optional-dispatch-entry-points leaf))
1127 (when (promise-ready-p ep)
1129 (when (optional-dispatch-more-entry leaf)
1130 (frob (optional-dispatch-more-entry leaf)))
1131 (let ((main (optional-dispatch-main-entry leaf)))
1133 (setf (functional-entry-fun entry) main)
1134 (setf (functional-entry-fun main) entry))
1135 (when (eq (functional-kind main) :optional)
1140 (defun note-local-functional (fun)
1141 (declare (type functional fun))
1142 (when (and (leaf-has-source-name-p fun)
1143 (eq (leaf-source-name fun) (functional-debug-name fun)))
1144 (let ((name (leaf-source-name fun)))
1145 (let ((defined-fun (gethash name *free-funs*)))
1146 (when (and defined-fun
1147 (defined-fun-p defined-fun)
1148 (eq (defined-fun-functional defined-fun) fun))
1149 (remhash name *free-funs*))))))
1151 ;;; Return functional for DEFINED-FUN which has been converted in policy
1152 ;;; corresponding to the current one, or NIL if no such functional exists.
1153 (defun defined-fun-functional (defined-fun)
1154 (let ((policy (lexenv-%policy *lexenv*)))
1155 (dolist (functional (defined-fun-functionals defined-fun))
1156 (when (equal policy (lexenv-%policy (functional-lexenv functional)))
1157 (return functional)))))
1159 ;;; Do stuff to delete the semantic attachments of a REF node. When
1160 ;;; this leaves zero or one reference, we do a type dispatch off of
1161 ;;; the leaf to determine if a special action is appropriate.
1162 (defun delete-ref (ref)
1163 (declare (type ref ref))
1164 (let* ((leaf (ref-leaf ref))
1165 (refs (delq ref (leaf-refs leaf))))
1166 (setf (leaf-refs leaf) refs)
1171 (delete-lambda-var leaf))
1173 (ecase (functional-kind leaf)
1174 ((nil :let :mv-let :assignment :escape :cleanup)
1175 (aver (null (functional-entry-fun leaf)))
1176 (delete-lambda leaf))
1178 (delete-lambda leaf))
1179 ((:deleted :zombie :optional))))
1181 (unless (eq (functional-kind leaf) :deleted)
1182 (delete-optional-dispatch leaf)))))
1185 (clambda (or (maybe-let-convert leaf)
1186 (maybe-convert-to-assignment leaf)))
1187 (lambda-var (reoptimize-lambda-var leaf))))
1190 (clambda (maybe-convert-to-assignment leaf))))))
1194 ;;; This function is called by people who delete nodes; it provides a
1195 ;;; way to indicate that the value of a lvar is no longer used. We
1196 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1197 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1198 (defun flush-dest (lvar)
1199 (declare (type (or lvar null) lvar))
1201 (setf (lvar-dest lvar) nil)
1202 (flush-lvar-externally-checkable-type lvar)
1204 (let ((prev (node-prev use)))
1205 (let ((block (ctran-block prev)))
1206 (reoptimize-component (block-component block) t)
1207 (setf (block-attributep (block-flags block)
1208 flush-p type-asserted type-check)
1210 (setf (node-lvar use) nil))
1211 (setf (lvar-uses lvar) nil))
1214 (defun delete-dest (lvar)
1216 (let* ((dest (lvar-dest lvar))
1217 (prev (node-prev dest)))
1218 (let ((block (ctran-block prev)))
1219 (unless (block-delete-p block)
1220 (mark-for-deletion block))))))
1222 ;;; Queue the block for deletion
1223 (defun delete-block-lazily (block)
1224 (declare (type cblock block))
1225 (unless (block-delete-p block)
1226 (setf (block-delete-p block) t)
1227 (push block (component-delete-blocks (block-component block)))))
1229 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1230 ;;; blocks with the DELETE-P flag.
1231 (defun mark-for-deletion (block)
1232 (declare (type cblock block))
1233 (let* ((component (block-component block))
1234 (head (component-head component)))
1235 (labels ((helper (block)
1236 (delete-block-lazily block)
1237 (dolist (pred (block-pred block))
1238 (unless (or (block-delete-p pred)
1241 (unless (block-delete-p block)
1243 (setf (component-reanalyze component) t))))
1246 ;;; This function does what is necessary to eliminate the code in it
1247 ;;; from the IR1 representation. This involves unlinking it from its
1248 ;;; predecessors and successors and deleting various node-specific
1249 ;;; semantic information. BLOCK must be already removed from
1250 ;;; COMPONENT-DELETE-BLOCKS.
1251 (defun delete-block (block &optional silent)
1252 (declare (type cblock block))
1253 (aver (block-component block)) ; else block is already deleted!
1254 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1256 (note-block-deletion block))
1257 (setf (block-delete-p block) t)
1259 (dolist (b (block-pred block))
1260 (unlink-blocks b block)
1261 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1262 ;; broken when successors were deleted without setting the
1263 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1264 ;; doesn't happen again.
1265 (aver (not (and (null (block-succ b))
1266 (not (block-delete-p b))
1267 (not (eq b (component-head (block-component b))))))))
1268 (dolist (b (block-succ block))
1269 (unlink-blocks block b))
1271 (do-nodes-carefully (node block)
1272 (when (valued-node-p node)
1273 (delete-lvar-use node))
1275 (ref (delete-ref node))
1276 (cif (flush-dest (if-test node)))
1277 ;; The next two cases serve to maintain the invariant that a LET
1278 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1279 ;; the lambda whenever we delete any of these, but we must be
1280 ;; careful that this LET has not already been partially deleted.
1282 (when (and (eq (basic-combination-kind node) :local)
1283 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1284 (lvar-uses (basic-combination-fun node)))
1285 (let ((fun (combination-lambda node)))
1286 ;; If our REF was the second-to-last ref, and has been
1287 ;; deleted, then FUN may be a LET for some other
1289 (when (and (functional-letlike-p fun)
1290 (eq (let-combination fun) node))
1291 (delete-lambda fun))))
1292 (flush-dest (basic-combination-fun node))
1293 (dolist (arg (basic-combination-args node))
1294 (when arg (flush-dest arg))))
1296 (let ((lambda (bind-lambda node)))
1297 (unless (eq (functional-kind lambda) :deleted)
1298 (delete-lambda lambda))))
1300 (let ((value (exit-value node))
1301 (entry (exit-entry node)))
1305 (setf (entry-exits entry)
1306 (delq node (entry-exits entry))))))
1308 (dolist (exit (entry-exits node))
1309 (mark-for-deletion (node-block exit)))
1310 (let ((home (node-home-lambda node)))
1311 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1313 (flush-dest (return-result node))
1314 (delete-return node))
1316 (flush-dest (set-value node))
1317 (let ((var (set-var node)))
1318 (setf (basic-var-sets var)
1319 (delete node (basic-var-sets var)))))
1321 (flush-dest (cast-value node)))))
1323 (remove-from-dfo block)
1326 ;;; Do stuff to indicate that the return node NODE is being deleted.
1327 (defun delete-return (node)
1328 (declare (type creturn node))
1329 (let* ((fun (return-lambda node))
1330 (tail-set (lambda-tail-set fun)))
1331 (aver (lambda-return fun))
1332 (setf (lambda-return fun) nil)
1333 (when (and tail-set (not (find-if #'lambda-return
1334 (tail-set-funs tail-set))))
1335 (setf (tail-set-type tail-set) *empty-type*)))
1338 ;;; If any of the VARS in FUN was never referenced and was not
1339 ;;; declared IGNORE, then complain.
1340 (defun note-unreferenced-vars (fun)
1341 (declare (type clambda fun))
1342 (dolist (var (lambda-vars fun))
1343 (unless (or (leaf-ever-used var)
1344 (lambda-var-ignorep var))
1345 (let ((*compiler-error-context* (lambda-bind fun)))
1346 (unless (policy *compiler-error-context* (= inhibit-warnings 3))
1347 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1348 ;; requires this to be no more than a STYLE-WARNING.
1350 (compiler-style-warn "The variable ~S is defined but never used."
1351 (leaf-debug-name var))
1352 ;; There's no reason to accept this kind of equivocation
1353 ;; when compiling our own code, though.
1355 (warn "The variable ~S is defined but never used."
1356 (leaf-debug-name var)))
1357 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1360 (defvar *deletion-ignored-objects* '(t nil))
1362 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1363 ;;; our recursion so that we don't get lost in circular structures. We
1364 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1365 ;;; function referencess with variables), and we also ignore anything
1367 (defun present-in-form (obj form depth)
1368 (declare (type (integer 0 20) depth))
1369 (cond ((= depth 20) nil)
1373 (let ((first (car form))
1375 (if (member first '(quote function))
1377 (or (and (not (symbolp first))
1378 (present-in-form obj first depth))
1379 (do ((l (cdr form) (cdr l))
1381 ((or (atom l) (> n 100))
1383 (declare (fixnum n))
1384 (when (present-in-form obj (car l) depth)
1387 ;;; This function is called on a block immediately before we delete
1388 ;;; it. We check to see whether any of the code about to die appeared
1389 ;;; in the original source, and emit a note if so.
1391 ;;; If the block was in a lambda is now deleted, then we ignore the
1392 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1393 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1394 ;;; reasonable for a function to not return, and there is a different
1395 ;;; note for that case anyway.
1397 ;;; If the actual source is an atom, then we use a bunch of heuristics
1398 ;;; to guess whether this reference really appeared in the original
1400 ;;; -- If a symbol, it must be interned and not a keyword.
1401 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1402 ;;; or a character.)
1403 ;;; -- The atom must be "present" in the original source form, and
1404 ;;; present in all intervening actual source forms.
1405 (defun note-block-deletion (block)
1406 (let ((home (block-home-lambda block)))
1407 (unless (eq (functional-kind home) :deleted)
1408 (do-nodes (node nil block)
1409 (let* ((path (node-source-path node))
1410 (first (first path)))
1411 (when (or (eq first 'original-source-start)
1413 (or (not (symbolp first))
1414 (let ((pkg (symbol-package first)))
1416 (not (eq pkg (symbol-package :end))))))
1417 (not (member first *deletion-ignored-objects*))
1418 (not (typep first '(or fixnum character)))
1420 (present-in-form first x 0))
1421 (source-path-forms path))
1422 (present-in-form first (find-original-source path)
1424 (unless (return-p node)
1425 (let ((*compiler-error-context* node))
1426 (compiler-notify 'code-deletion-note
1427 :format-control "deleting unreachable code"
1428 :format-arguments nil)))
1432 ;;; Delete a node from a block, deleting the block if there are no
1433 ;;; nodes left. We remove the node from the uses of its LVAR.
1435 ;;; If the node is the last node, there must be exactly one successor.
1436 ;;; We link all of our precedessors to the successor and unlink the
1437 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1438 ;;; left, and the block is a successor of itself, then we replace the
1439 ;;; only node with a degenerate exit node. This provides a way to
1440 ;;; represent the bodyless infinite loop, given the prohibition on
1441 ;;; empty blocks in IR1.
1442 (defun unlink-node (node)
1443 (declare (type node node))
1444 (when (valued-node-p node)
1445 (delete-lvar-use node))
1447 (let* ((ctran (node-next node))
1448 (next (and ctran (ctran-next ctran)))
1449 (prev (node-prev node))
1450 (block (ctran-block prev))
1451 (prev-kind (ctran-kind prev))
1452 (last (block-last block)))
1454 (setf (block-type-asserted block) t)
1455 (setf (block-test-modified block) t)
1457 (cond ((or (eq prev-kind :inside-block)
1458 (and (eq prev-kind :block-start)
1459 (not (eq node last))))
1460 (cond ((eq node last)
1461 (setf (block-last block) (ctran-use prev))
1462 (setf (node-next (ctran-use prev)) nil))
1464 (setf (ctran-next prev) next)
1465 (setf (node-prev next) prev)
1466 (when (if-p next) ; AOP wanted
1467 (reoptimize-lvar (if-test next)))))
1468 (setf (node-prev node) nil)
1471 (aver (eq prev-kind :block-start))
1472 (aver (eq node last))
1473 (let* ((succ (block-succ block))
1474 (next (first succ)))
1475 (aver (singleton-p succ))
1477 ((eq block (first succ))
1478 (with-ir1-environment-from-node node
1479 (let ((exit (make-exit)))
1480 (setf (ctran-next prev) nil)
1481 (link-node-to-previous-ctran exit prev)
1482 (setf (block-last block) exit)))
1483 (setf (node-prev node) nil)
1486 (aver (eq (block-start-cleanup block)
1487 (block-end-cleanup block)))
1488 (unlink-blocks block next)
1489 (dolist (pred (block-pred block))
1490 (change-block-successor pred block next))
1491 (when (block-delete-p block)
1492 (let ((component (block-component block)))
1493 (setf (component-delete-blocks component)
1494 (delq block (component-delete-blocks component)))))
1495 (remove-from-dfo block)
1496 (setf (block-delete-p block) t)
1497 (setf (node-prev node) nil)
1500 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1502 (defun ctran-deleted-p (ctran)
1503 (declare (type ctran ctran))
1504 (let ((block (ctran-block ctran)))
1505 (or (not (block-component block))
1506 (block-delete-p block))))
1508 ;;; Return true if NODE has been deleted, false if it is still a valid
1510 (defun node-deleted (node)
1511 (declare (type node node))
1512 (let ((prev (node-prev node)))
1514 (ctran-deleted-p prev))))
1516 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1517 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1518 ;;; triggered by deletion.
1519 (defun delete-component (component)
1520 (declare (type component component))
1521 (aver (null (component-new-functionals component)))
1522 (setf (component-kind component) :deleted)
1523 (do-blocks (block component)
1524 (delete-block-lazily block))
1525 (dolist (fun (component-lambdas component))
1526 (unless (eq (functional-kind fun) :deleted)
1527 (setf (functional-kind fun) nil)
1528 (setf (functional-entry-fun fun) nil)
1529 (setf (leaf-refs fun) nil)
1530 (delete-functional fun)))
1531 (clean-component component)
1534 ;;; Remove all pending blocks to be deleted. Return the nearest live
1535 ;;; block after or equal to BLOCK.
1536 (defun clean-component (component &optional block)
1537 (loop while (component-delete-blocks component)
1538 ;; actual deletion of a block may queue new blocks
1539 do (let ((current (pop (component-delete-blocks component))))
1540 (when (eq block current)
1541 (setq block (block-next block)))
1542 (delete-block current)))
1545 ;;; Convert code of the form
1546 ;;; (FOO ... (FUN ...) ...)
1548 ;;; (FOO ... ... ...).
1549 ;;; In other words, replace the function combination FUN by its
1550 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1551 ;;; to blow out of whatever transform called this. Note, as the number
1552 ;;; of arguments changes, the transform must be prepared to return a
1553 ;;; lambda with a new lambda-list with the correct number of
1555 (defun splice-fun-args (lvar fun num-args)
1557 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments to feed
1558 directly to the LVAR-DEST of LVAR, which must be a combination. If FUN
1559 is :ANY, the function name is not checked."
1560 (declare (type lvar lvar)
1562 (type index num-args))
1563 (let ((outside (lvar-dest lvar))
1564 (inside (lvar-uses lvar)))
1565 (aver (combination-p outside))
1566 (unless (combination-p inside)
1567 (give-up-ir1-transform))
1568 (let ((inside-fun (combination-fun inside)))
1569 (unless (or (eq fun :any)
1570 (eq (lvar-fun-name inside-fun) fun))
1571 (give-up-ir1-transform))
1572 (let ((inside-args (combination-args inside)))
1573 (unless (= (length inside-args) num-args)
1574 (give-up-ir1-transform))
1575 (let* ((outside-args (combination-args outside))
1576 (arg-position (position lvar outside-args))
1577 (before-args (subseq outside-args 0 arg-position))
1578 (after-args (subseq outside-args (1+ arg-position))))
1579 (dolist (arg inside-args)
1580 (setf (lvar-dest arg) outside)
1581 (flush-lvar-externally-checkable-type arg))
1582 (setf (combination-args inside) nil)
1583 (setf (combination-args outside)
1584 (append before-args inside-args after-args))
1585 (change-ref-leaf (lvar-uses inside-fun)
1586 (find-free-fun 'list "???"))
1587 (setf (combination-fun-info inside) (info :function :info 'list)
1588 (combination-kind inside) :known)
1589 (setf (node-derived-type inside) *wild-type*)
1593 ;;; Eliminate keyword arguments from the call (leaving the
1594 ;;; parameters in place.
1596 ;;; (FOO ... :BAR X :QUUX Y)
1600 ;;; SPECS is a list of (:KEYWORD PARAMETER) specifications.
1601 ;;; Returns the list of specified parameters names in the
1602 ;;; order they appeared in the call. N-POSITIONAL is the
1603 ;;; number of positional arguments in th call.
1604 (defun eliminate-keyword-args (call n-positional specs)
1605 (let* ((specs (copy-tree specs))
1606 (all (combination-args call))
1607 (new-args (reverse (subseq all 0 n-positional)))
1608 (key-args (subseq all n-positional))
1610 (loop while key-args
1611 do (let* ((key (pop key-args))
1612 (val (pop key-args))
1613 (keyword (if (constant-lvar-p key)
1615 (give-up-ir1-transform)))
1616 (spec (or (assoc keyword specs :test #'eq)
1617 (give-up-ir1-transform))))
1620 (push (second spec) parameters)
1621 ;; In case of duplicate keys.
1622 (setf (second spec) (gensym))))
1623 (setf (combination-args call) (reverse new-args))
1624 (reverse parameters)))
1626 (defun extract-fun-args (lvar fun num-args)
1627 (declare (type lvar lvar)
1628 (type (or symbol list) fun)
1629 (type index num-args))
1630 (let ((fun (if (listp fun) fun (list fun))))
1631 (let ((inside (lvar-uses lvar)))
1632 (unless (combination-p inside)
1633 (give-up-ir1-transform))
1634 (let ((inside-fun (combination-fun inside)))
1635 (unless (member (lvar-fun-name inside-fun) fun)
1636 (give-up-ir1-transform))
1637 (let ((inside-args (combination-args inside)))
1638 (unless (= (length inside-args) num-args)
1639 (give-up-ir1-transform))
1640 (values (lvar-fun-name inside-fun) inside-args))))))
1642 (defun flush-combination (combination)
1643 (declare (type combination combination))
1644 (flush-dest (combination-fun combination))
1645 (dolist (arg (combination-args combination))
1647 (unlink-node combination)
1653 ;;; Change the LEAF that a REF refers to.
1654 (defun change-ref-leaf (ref leaf)
1655 (declare (type ref ref) (type leaf leaf))
1656 (unless (eq (ref-leaf ref) leaf)
1657 (push ref (leaf-refs leaf))
1659 (setf (ref-leaf ref) leaf)
1660 (setf (leaf-ever-used leaf) t)
1661 (let* ((ltype (leaf-type leaf))
1662 (vltype (make-single-value-type ltype)))
1663 (if (let* ((lvar (node-lvar ref))
1664 (dest (and lvar (lvar-dest lvar))))
1665 (and (basic-combination-p dest)
1666 (eq lvar (basic-combination-fun dest))
1667 (csubtypep ltype (specifier-type 'function))))
1668 (setf (node-derived-type ref) vltype)
1669 (derive-node-type ref vltype)))
1670 (reoptimize-lvar (node-lvar ref)))
1673 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1674 (defun substitute-leaf (new-leaf old-leaf)
1675 (declare (type leaf new-leaf old-leaf))
1676 (dolist (ref (leaf-refs old-leaf))
1677 (change-ref-leaf ref new-leaf))
1680 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
1681 ;;; whether to substitute
1682 (defun substitute-leaf-if (test new-leaf old-leaf)
1683 (declare (type leaf new-leaf old-leaf) (type function test))
1684 (dolist (ref (leaf-refs old-leaf))
1685 (when (funcall test ref)
1686 (change-ref-leaf ref new-leaf)))
1689 ;;; Return a LEAF which represents the specified constant object. If
1690 ;;; the object is not in *CONSTANTS*, then we create a new constant
1691 ;;; LEAF and enter it. If we are producing a fasl file, make sure that
1692 ;;; MAKE-LOAD-FORM gets used on any parts of the constant that it
1695 ;;; We are allowed to coalesce things like EQUAL strings and bit-vectors
1696 ;;; when file-compiling, but not when using COMPILE.
1697 (defun find-constant (object &optional (name nil namep))
1698 (let ((faslp (producing-fasl-file)))
1699 (labels ((make-it ()
1702 (maybe-emit-make-load-forms object name)
1703 (maybe-emit-make-load-forms object)))
1704 (make-constant object))
1705 (core-coalesce-p (x)
1706 ;; True for things which retain their identity under EQUAL,
1707 ;; so we can safely share the same CONSTANT leaf between
1708 ;; multiple references.
1709 (or (typep x '(or symbol number character))
1710 ;; Amusingly enough, we see CLAMBDAs --among other things--
1711 ;; here, from compiling things like %ALLOCATE-CLOSUREs forms.
1712 ;; No point in stuffing them in the hash-table.
1713 (and (typep x 'instance)
1714 (not (or (leaf-p x) (node-p x))))))
1715 (file-coalesce-p (x)
1716 ;; CLHS 3.2.4.2.2: We are also allowed to coalesce various
1717 ;; other things when file-compiling.
1718 (or (core-coalesce-p x)
1720 (if (eq +code-coverage-unmarked+ (cdr x))
1721 ;; These are already coalesced, and the CAR should
1722 ;; always be OK, so no need to check.
1724 (unless (maybe-cyclic-p x) ; safe for EQUAL?
1726 ((atom y) (file-coalesce-p y))
1727 (unless (file-coalesce-p (car y))
1729 ;; We *could* coalesce base-strings as well,
1730 ;; but we'd need a separate hash-table for
1731 ;; that, since we are not allowed to coalesce
1732 ;; base-strings with non-base-strings.
1735 ;; in the cross-compiler, we coalesce
1736 ;; all strings with the same contents,
1737 ;; because we will end up dumping them
1738 ;; as base-strings anyway. In the
1739 ;; real compiler, we're not allowed to
1740 ;; coalesce regardless of string
1741 ;; specialized element type, so we
1742 ;; KLUDGE by coalescing only character
1743 ;; strings (the common case) and
1744 ;; punting on the other types.
1748 (vector character))))))
1750 (if faslp (file-coalesce-p x) (core-coalesce-p x))))
1751 (if (and (boundp '*constants*) (coalescep object))
1752 (or (gethash object *constants*)
1753 (setf (gethash object *constants*)
1757 ;;; Return true if VAR would have to be closed over if environment
1758 ;;; analysis ran now (i.e. if there are any uses that have a different
1759 ;;; home lambda than VAR's home.)
1760 (defun closure-var-p (var)
1761 (declare (type lambda-var var))
1762 (let ((home (lambda-var-home var)))
1763 (cond ((eq (functional-kind home) :deleted)
1765 (t (let ((home (lambda-home home)))
1768 :key #'node-home-lambda
1770 (or (frob (leaf-refs var))
1771 (frob (basic-var-sets var)))))))))
1773 ;;; If there is a non-local exit noted in ENTRY's environment that
1774 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1775 (defun find-nlx-info (exit)
1776 (declare (type exit exit))
1777 (let* ((entry (exit-entry exit))
1778 (cleanup (entry-cleanup entry))
1779 (block (first (block-succ (node-block exit)))))
1780 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
1781 (when (and (eq (nlx-info-block nlx) block)
1782 (eq (nlx-info-cleanup nlx) cleanup))
1785 (defun nlx-info-lvar (nlx)
1786 (declare (type nlx-info nlx))
1787 (node-lvar (block-last (nlx-info-target nlx))))
1789 ;;;; functional hackery
1791 (declaim (ftype (sfunction (functional) clambda) main-entry))
1792 (defun main-entry (functional)
1793 (etypecase functional
1794 (clambda functional)
1796 (optional-dispatch-main-entry functional))))
1798 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1799 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1800 ;;; optional with null default and no SUPPLIED-P. There must be a
1801 ;;; &REST arg with no references.
1802 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
1803 (defun looks-like-an-mv-bind (functional)
1804 (and (optional-dispatch-p functional)
1805 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1807 (let ((info (lambda-var-arg-info (car arg))))
1808 (unless info (return nil))
1809 (case (arg-info-kind info)
1811 (when (or (arg-info-supplied-p info) (arg-info-default info))
1814 (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1818 ;;; Return true if function is an external entry point. This is true
1819 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
1820 ;;; (:TOPLEVEL kind.)
1822 (declare (type functional fun))
1823 (not (null (member (functional-kind fun) '(:external :toplevel)))))
1825 ;;; If LVAR's only use is a non-notinline global function reference,
1826 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1827 ;;; is true, then we don't care if the leaf is NOTINLINE.
1828 (defun lvar-fun-name (lvar &optional notinline-ok)
1829 (declare (type lvar lvar))
1830 (let ((use (lvar-uses lvar)))
1832 (let ((leaf (ref-leaf use)))
1833 (if (and (global-var-p leaf)
1834 (eq (global-var-kind leaf) :global-function)
1835 (or (not (defined-fun-p leaf))
1836 (not (eq (defined-fun-inlinep leaf) :notinline))
1838 (leaf-source-name leaf)
1842 (defun lvar-fun-debug-name (lvar)
1843 (declare (type lvar lvar))
1844 (let ((uses (lvar-uses lvar)))
1846 (leaf-debug-name (ref-leaf use))))
1849 (mapcar #'name1 uses)))))
1851 ;;; Return the source name of a combination. (This is an idiom
1852 ;;; which was used in CMU CL. I gather it always works. -- WHN)
1853 (defun combination-fun-source-name (combination)
1854 (let ((ref (lvar-uses (combination-fun combination))))
1855 (leaf-source-name (ref-leaf ref))))
1857 ;;; Return the COMBINATION node that is the call to the LET FUN.
1858 (defun let-combination (fun)
1859 (declare (type clambda fun))
1860 (aver (functional-letlike-p fun))
1861 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1863 ;;; Return the initial value lvar for a LET variable, or NIL if there
1865 (defun let-var-initial-value (var)
1866 (declare (type lambda-var var))
1867 (let ((fun (lambda-var-home var)))
1868 (elt (combination-args (let-combination fun))
1869 (position-or-lose var (lambda-vars fun)))))
1871 ;;; Return the LAMBDA that is called by the local CALL.
1872 (defun combination-lambda (call)
1873 (declare (type basic-combination call))
1874 (aver (eq (basic-combination-kind call) :local))
1875 (ref-leaf (lvar-uses (basic-combination-fun call))))
1877 (defvar *inline-expansion-limit* 200
1879 "an upper limit on the number of inline function calls that will be expanded
1880 in any given code object (single function or block compilation)")
1882 ;;; Check whether NODE's component has exceeded its inline expansion
1883 ;;; limit, and warn if so, returning NIL.
1884 (defun inline-expansion-ok (node)
1885 (let ((expanded (incf (component-inline-expansions
1887 (node-block node))))))
1888 (cond ((> expanded *inline-expansion-limit*) nil)
1889 ((= expanded *inline-expansion-limit*)
1890 ;; FIXME: If the objective is to stop the recursive
1891 ;; expansion of inline functions, wouldn't it be more
1892 ;; correct to look back through surrounding expansions
1893 ;; (which are, I think, stored in the *CURRENT-PATH*, and
1894 ;; possibly stored elsewhere too) and suppress expansion
1895 ;; and print this warning when the function being proposed
1896 ;; for inline expansion is found there? (I don't like the
1897 ;; arbitrary numerical limit in principle, and I think
1898 ;; it'll be a nuisance in practice if we ever want the
1899 ;; compiler to be able to use WITH-COMPILATION-UNIT on
1900 ;; arbitrarily huge blocks of code. -- WHN)
1901 (let ((*compiler-error-context* node))
1902 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
1903 probably trying to~% ~
1904 inline a recursive function."
1905 *inline-expansion-limit*))
1909 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
1910 (defun assure-functional-live-p (functional)
1911 (declare (type functional functional))
1913 ;; looks LET-converted
1914 (functional-somewhat-letlike-p functional)
1915 ;; It's possible for a LET-converted function to end up
1916 ;; deleted later. In that case, for the purposes of this
1917 ;; analysis, it is LET-converted: LET-converted functionals
1918 ;; are too badly trashed to expand them inline, and deleted
1919 ;; LET-converted functionals are even worse.
1920 (memq (functional-kind functional) '(:deleted :zombie))))
1921 (throw 'locall-already-let-converted functional)))
1923 (defun call-full-like-p (call)
1924 (declare (type combination call))
1925 (let ((kind (basic-combination-kind call)))
1927 (and (eq kind :known)
1928 (let ((info (basic-combination-fun-info call)))
1930 (not (fun-info-ir2-convert info))
1931 (dolist (template (fun-info-templates info) t)
1932 (when (eq (template-ltn-policy template) :fast-safe)
1933 (multiple-value-bind (val win)
1934 (valid-fun-use call (template-type template))
1935 (when (or val (not win)) (return nil)))))))))))
1939 ;;; Apply a function to some arguments, returning a list of the values
1940 ;;; resulting of the evaluation. If an error is signalled during the
1941 ;;; application, then we produce a warning message using WARN-FUN and
1942 ;;; return NIL as our second value to indicate this. NODE is used as
1943 ;;; the error context for any error message, and CONTEXT is a string
1944 ;;; that is spliced into the warning.
1945 (declaim (ftype (sfunction ((or symbol function) list node function string)
1946 (values list boolean))
1948 (defun careful-call (function args node warn-fun context)
1950 (multiple-value-list
1951 (handler-case (apply function args)
1953 (let ((*compiler-error-context* node))
1954 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
1955 (return-from careful-call (values nil nil))))))
1958 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
1961 ((deffrob (basic careful compiler transform)
1963 (defun ,careful (specifier)
1964 (handler-case (,basic specifier)
1965 (sb!kernel::arg-count-error (condition)
1966 (values nil (list (format nil "~A" condition))))
1967 (simple-error (condition)
1968 (values nil (list* (simple-condition-format-control condition)
1969 (simple-condition-format-arguments condition))))))
1970 (defun ,compiler (specifier)
1971 (multiple-value-bind (type error-args) (,careful specifier)
1973 (apply #'compiler-error error-args))))
1974 (defun ,transform (specifier)
1975 (multiple-value-bind (type error-args) (,careful specifier)
1977 (apply #'give-up-ir1-transform
1979 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
1980 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
1983 ;;;; utilities used at run-time for parsing &KEY args in IR1
1985 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1986 ;;; the lvar for the value of the &KEY argument KEY in the list of
1987 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
1988 ;;; otherwise. The legality and constantness of the keywords should
1989 ;;; already have been checked.
1990 (declaim (ftype (sfunction (list keyword) (or lvar null))
1992 (defun find-keyword-lvar (args key)
1993 (do ((arg args (cddr arg)))
1995 (when (eq (lvar-value (first arg)) key)
1996 (return (second arg)))))
1998 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1999 ;;; verify that alternating lvars in ARGS are constant and that there
2000 ;;; is an even number of args.
2001 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
2002 (defun check-key-args-constant (args)
2003 (do ((arg args (cddr arg)))
2005 (unless (and (rest arg)
2006 (constant-lvar-p (first arg)))
2009 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2010 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
2011 ;;; and that only keywords present in the list KEYS are supplied.
2012 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
2013 (defun check-transform-keys (args keys)
2014 (and (check-key-args-constant args)
2015 (do ((arg args (cddr arg)))
2017 (unless (member (lvar-value (first arg)) keys)
2022 ;;; Called by the expansion of the EVENT macro.
2023 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
2024 (defun %event (info node)
2025 (incf (event-info-count info))
2026 (when (and (>= (event-info-level info) *event-note-threshold*)
2027 (policy (or node *lexenv*)
2028 (= inhibit-warnings 0)))
2029 (let ((*compiler-error-context* node))
2030 (compiler-notify (event-info-description info))))
2032 (let ((action (event-info-action info)))
2033 (when action (funcall action node))))
2036 (defun make-cast (value type policy)
2037 (declare (type lvar value)
2039 (type policy policy))
2040 (%make-cast :asserted-type type
2041 :type-to-check (maybe-weaken-check type policy)
2043 :derived-type (coerce-to-values type)))
2045 (defun cast-type-check (cast)
2046 (declare (type cast cast))
2047 (when (cast-reoptimize cast)
2048 (ir1-optimize-cast cast t))
2049 (cast-%type-check cast))
2051 (defun note-single-valuified-lvar (lvar)
2052 (declare (type (or lvar null) lvar))
2054 (let ((use (lvar-uses lvar)))
2056 (let ((leaf (ref-leaf use)))
2057 (when (and (lambda-var-p leaf)
2058 (null (rest (leaf-refs leaf))))
2059 (reoptimize-lambda-var leaf))))
2060 ((or (listp use) (combination-p use))
2061 (do-uses (node lvar)
2062 (setf (node-reoptimize node) t)
2063 (setf (block-reoptimize (node-block node)) t)
2064 (reoptimize-component (node-component node) :maybe)))))))
2066 ;;; Return true if LVAR's only use is a non-NOTINLINE reference to a
2067 ;;; global function with one of the specified NAMES.
2068 (defun lvar-fun-is (lvar names)
2069 (declare (type lvar lvar) (list names))
2070 (let ((use (lvar-uses lvar)))
2072 (let ((leaf (ref-leaf use)))
2073 (and (global-var-p leaf)
2074 (eq (global-var-kind leaf) :global-function)
2075 (not (null (member (leaf-source-name leaf) names
2076 :test #'equal))))))))
2078 (defun lvar-matches (lvar &key fun-names arg-count)
2079 (let ((use (lvar-use lvar)))
2080 (and (combination-p use)
2082 (member (combination-fun-source-name use)
2083 fun-names :test #'eq))
2085 (= arg-count (length (combination-args use)))))))