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