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