Maintain LAMBDA-CALLS-OR-CLOSES in DELETE-REF.
[sbcl.git] / src / compiler / ir1util.lisp
blob44348747d27d429ae90d909a1f9c9ee3ba03e3f3
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* ((lvar (combination-lvar combination))
744 (dest (and lvar
745 (lvar-dest lvar))))
746 (and (combination-p dest)
747 (recurse dest)))
748 (let* ((fun1 (combination-fun combination))
749 (ref1 (principal-lvar-use fun1))
750 (clambda1 (when (ref-p ref1) (ref-leaf ref1))))
751 (when (lambda-p clambda1)
752 (dolist (var (lambda-vars clambda1) t)
753 (dolist (var-ref (lambda-var-refs var))
754 (let* ((lvar (ref-lvar var-ref))
755 (dest (and lvar (principal-lvar-dest lvar))))
756 (unless (or (not dest)
757 (and (combination-p dest) (recurse dest)))
758 (return-from combination-args-flow-cleanly-p nil)))))))))))
759 (recurse combination1)))
761 (defun ref-good-for-dx-p (ref)
762 (let* ((lvar (ref-lvar ref))
763 (dest (when lvar (lvar-dest lvar))))
764 (and (combination-p dest)
765 (eq :known (combination-kind dest))
766 (awhen (combination-fun-info dest)
767 (or (ir1-attributep (fun-info-attributes it) dx-safe)
768 (and (not (combination-lvar dest))
769 (awhen (fun-info-result-arg it)
770 (eql lvar (nth it (combination-args dest))))))))))
772 (defun trivial-lambda-var-ref-p (use)
773 (and (ref-p use)
774 (let ((var (ref-leaf use)))
775 ;; lambda-var, no SETS, not explicitly indefinite-extent.
776 (when (and (lambda-var-p var) (not (lambda-var-sets var))
777 (neq :indefinite (lambda-var-extent var)))
778 (let ((home (lambda-var-home var))
779 (refs (lambda-var-refs var)))
780 ;; bound by a non-XEP system lambda, no other REFS that aren't
781 ;; DX-SAFE, or are result-args when the result is discarded.
782 (when (and (lambda-system-lambda-p home)
783 (neq :external (lambda-kind home))
784 (dolist (ref refs t)
785 (unless (or (eq use ref) (ref-good-for-dx-p ref))
786 (return nil))))
787 ;; the LAMBDA this var is bound by has only a single REF, going
788 ;; to a combination
789 (let* ((lambda-refs (lambda-refs home))
790 (primary (car lambda-refs)))
791 (and (ref-p primary)
792 (not (cdr lambda-refs))
793 (combination-p (lvar-dest (ref-lvar primary)))))))))))
795 (defun trivial-lambda-var-ref-lvar (use)
796 (let* ((this (ref-leaf use))
797 (fun (lambda-var-home this))
798 (vars (lambda-vars fun))
799 (combination (lvar-dest (ref-lvar (car (lambda-refs fun)))))
800 (args (combination-args combination)))
801 (aver (= (length vars) (length args)))
802 (loop for var in vars
803 for arg in args
804 when (eq var this)
805 return arg)))
807 (defun lambda-var-ref-lvar (ref)
808 (let ((var (ref-leaf ref)))
809 (when (and (lambda-var-p var)
810 (not (lambda-var-sets var)))
811 (let* ((fun (lambda-var-home var))
812 (vars (lambda-vars fun))
813 (lvar (ref-lvar (car (lambda-refs fun))))
814 (combination (and lvar
815 (lvar-dest lvar))))
816 (when (combination-p combination)
817 (loop for v in vars
818 for arg in (combination-args combination)
819 when (eq v var)
820 return arg))))))
822 ;;; This needs to play nice with LVAR-GOOD-FOR-DX-P and friends.
823 (defun handle-nested-dynamic-extent-lvars (dx lvar &optional recheck-component)
824 (let ((uses (lvar-uses lvar)))
825 ;; DX value generators must end their blocks: see UPDATE-UVL-LIVE-SETS.
826 ;; Uses of mupltiple-use LVARs already end their blocks, so we just need
827 ;; to process uses of single-use LVARs.
828 (when (node-p uses)
829 (when (node-to-be-deleted-p uses)
830 (return-from handle-nested-dynamic-extent-lvars))
831 (node-ends-block uses))
832 ;; If this LVAR's USE is good for DX, it is either a CAST, or it
833 ;; must be a regular combination whose arguments are potentially DX as well.
834 (flet ((recurse (use)
835 (etypecase use
836 (cast
837 (handle-nested-dynamic-extent-lvars
838 dx (cast-value use) recheck-component))
839 (combination
840 (loop for arg in (combination-args use)
841 ;; deleted args show up as NIL here
842 when (and arg
843 (lvar-good-for-dx-p arg dx recheck-component))
844 append (handle-nested-dynamic-extent-lvars
845 dx arg recheck-component)))
846 (ref
847 (let* ((other (trivial-lambda-var-ref-lvar use)))
848 (unless (eq other lvar)
849 (handle-nested-dynamic-extent-lvars
850 dx other recheck-component)))))))
851 (cons (cons dx lvar)
852 (if (listp uses)
853 (loop for use in uses
854 when (use-good-for-dx-p use dx recheck-component)
855 nconc (recurse use))
856 (when (use-good-for-dx-p uses dx recheck-component)
857 (recurse uses)))))))
859 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
860 ;;; of its original source's top level form in its compilation unit.
861 (defun source-path-tlf-number (path)
862 (declare (list path))
863 (car (last path)))
865 ;;; Return the (reversed) list for the PATH in the original source
866 ;;; (with the Top Level Form number last).
867 (declaim (ftype (sfunction (list) list) source-path-original-source))
868 (defun source-path-original-source (path)
869 (declare (list path) (inline member))
870 (cddr (member 'original-source-start path :test #'eq)))
872 ;;; Return the Form Number of PATH's original source inside the Top
873 ;;; Level Form that contains it. This is determined by the order that
874 ;;; we walk the subforms of the top level source form.
875 (declaim (ftype (sfunction (list) (or null index)) source-path-form-number))
876 (defun source-path-form-number (path)
877 (declare (inline member))
878 (cadr (member 'original-source-start path :test #'eq)))
880 ;;; Return a list of all the enclosing forms not in the original
881 ;;; source that converted to get to this form, with the immediate
882 ;;; source for node at the start of the list.
883 (defun source-path-forms (path)
884 (subseq path 0 (position 'original-source-start path)))
886 (defun tree-some (predicate tree)
887 (let ((seen (make-hash-table)))
888 (labels ((walk (tree)
889 (cond ((funcall predicate tree))
890 ((and (consp tree)
891 (not (gethash tree seen)))
892 (setf (gethash tree seen) t)
893 (or (walk (car tree))
894 (walk (cdr tree)))))))
895 (walk tree))))
897 ;;; Return the innermost source form for NODE.
898 (defun node-source-form (node)
899 (declare (type node node))
900 (let* ((path (node-source-path node))
901 (forms (remove-if (lambda (x)
902 (tree-some #'leaf-p x))
903 (source-path-forms path))))
904 ;; another option: if first form includes a leaf, return
905 ;; find-original-source instead.
906 (if forms
907 (first forms)
908 (values (find-original-source path)))))
910 ;;; Return NODE-SOURCE-FORM, T if lvar has a single use, otherwise
911 ;;; NIL, NIL.
912 (defun lvar-source (lvar)
913 (let ((use (lvar-uses lvar)))
914 (if (listp use)
915 (values nil nil)
916 (values (node-source-form use) t))))
918 (defun common-suffix (x y)
919 (let ((mismatch (mismatch x y :from-end t)))
920 (if mismatch
921 (subseq x mismatch)
922 x)))
924 ;;; If the LVAR has a single use, return NODE-SOURCE-FORM as a
925 ;;; singleton. Otherwise, return a list of the lowest common
926 ;;; ancestor source form of all the uses (if it can be found),
927 ;;; followed by all the uses' source forms.
928 (defun lvar-all-sources (lvar)
929 (let ((use (principal-lvar-use lvar)))
930 (if (listp use)
931 (let ((forms '())
932 (path (node-source-path (first use))))
933 (dolist (use use (cons (if (find 'original-source-start path)
934 (find-original-source path)
935 "a hairy form")
936 forms))
937 (pushnew (node-source-form use) forms)
938 (setf path (common-suffix path
939 (node-source-path use)))))
940 (list (node-source-form use)))))
942 ;;; Return the LAMBDA that is CTRAN's home, or NIL if there is none.
943 (declaim (ftype (sfunction (ctran) (or clambda null))
944 ctran-home-lambda-or-null))
945 (defun ctran-home-lambda-or-null (ctran)
946 ;; KLUDGE: This function is a post-CMU-CL hack by WHN, and this
947 ;; implementation might not be quite right, or might be uglier than
948 ;; necessary. It appears that the original Python never found a need
949 ;; to do this operation. The obvious things based on
950 ;; NODE-HOME-LAMBDA of CTRAN-USE usually work; then if that fails,
951 ;; BLOCK-HOME-LAMBDA of CTRAN-BLOCK works, given that we
952 ;; generalize it enough to grovel harder when the simple CMU CL
953 ;; approach fails, and furthermore realize that in some exceptional
954 ;; cases it might return NIL. -- WHN 2001-12-04
955 (cond ((ctran-use ctran)
956 (node-home-lambda (ctran-use ctran)))
957 ((ctran-block ctran)
958 (block-home-lambda-or-null (ctran-block ctran)))
960 (bug "confused about home lambda for ~S" ctran))))
962 ;;; Return the LAMBDA that is CTRAN's home.
963 (declaim (ftype (sfunction (ctran) clambda) ctran-home-lambda))
964 (defun ctran-home-lambda (ctran)
965 (ctran-home-lambda-or-null ctran))
967 (declaim (inline cast-single-value-p))
968 (defun cast-single-value-p (cast)
969 (not (values-type-p (cast-asserted-type cast))))
971 #!-sb-fluid (declaim (inline lvar-single-value-p))
972 (defun lvar-single-value-p (lvar)
973 (or (not lvar) (%lvar-single-value-p lvar)))
974 (defun %lvar-single-value-p (lvar)
975 (let ((dest (lvar-dest lvar)))
976 (typecase dest
977 ((or creturn exit)
978 nil)
979 (mv-combination
980 (eq (basic-combination-fun dest) lvar))
981 (cast
982 (and (cast-single-value-p dest)
983 (acond ((node-lvar dest) (%lvar-single-value-p it))
984 (t t))))
985 (t t))))
987 (defun principal-lvar-end (lvar)
988 (loop for prev = lvar then (node-lvar dest)
989 for dest = (and prev (lvar-dest prev))
990 while (cast-p dest)
991 finally (return (values dest prev))))
993 (defun principal-lvar-single-valuify (lvar)
994 (loop for prev = lvar then (node-lvar dest)
995 for dest = (and prev (lvar-dest prev))
996 while (cast-p dest)
997 do (setf (node-derived-type dest)
998 (make-short-values-type (list (single-value-type
999 (node-derived-type dest)))))
1000 (reoptimize-lvar prev)))
1002 ;;; Return a new LEXENV just like DEFAULT except for the specified
1003 ;;; slot values. Values for the alist slots are APPENDed to the
1004 ;;; beginning of the current value, rather than replacing it entirely.
1005 (defun make-lexenv (&key (default *lexenv*)
1006 funs vars blocks tags
1007 type-restrictions
1008 (lambda (lexenv-lambda default))
1009 (cleanup (lexenv-cleanup default))
1010 (handled-conditions (lexenv-handled-conditions default))
1011 (disabled-package-locks
1012 (lexenv-disabled-package-locks default))
1013 (policy (lexenv-policy default))
1014 (user-data (lexenv-user-data default))
1015 (flushable (lexenv-flushable default)))
1016 (macrolet ((frob (var slot)
1017 `(let ((old (,slot default)))
1018 (if ,var
1019 (append ,var old)
1020 old))))
1021 (internal-make-lexenv
1022 (frob funs lexenv-funs)
1023 (frob vars lexenv-vars)
1024 (frob blocks lexenv-blocks)
1025 (frob tags lexenv-tags)
1026 (frob type-restrictions lexenv-type-restrictions)
1027 (frob flushable lexenv-flushable)
1028 lambda
1029 cleanup handled-conditions disabled-package-locks
1030 policy
1031 user-data
1032 default)))
1034 ;;; Makes a LEXENV, suitable for using in a MACROLET introduced
1035 ;;; macroexpander
1036 (defun make-restricted-lexenv (lexenv)
1037 (flet ((fun-good-p (fun)
1038 (destructuring-bind (name . thing) fun
1039 (declare (ignore name))
1040 (etypecase thing
1041 (functional nil)
1042 (global-var t)
1043 (cons (aver (eq (car thing) 'macro))
1044 t))))
1045 (var-good-p (var)
1046 (destructuring-bind (name . thing) var
1047 (declare (ignore name))
1048 (etypecase thing
1049 ;; The evaluator will mark lexicals with :BOGUS when it
1050 ;; translates an interpreter lexenv to a compiler
1051 ;; lexenv.
1052 ((or leaf #!+sb-eval (member :bogus)) nil)
1053 (cons (aver (eq (car thing) 'macro))
1055 (heap-alien-info nil)))))
1056 (internal-make-lexenv
1057 (remove-if-not #'fun-good-p (lexenv-funs lexenv))
1058 (remove-if-not #'var-good-p (lexenv-vars lexenv))
1061 (lexenv-type-restrictions lexenv) ; XXX
1065 (lexenv-handled-conditions lexenv)
1066 (lexenv-disabled-package-locks lexenv)
1067 (lexenv-policy lexenv)
1068 (lexenv-user-data lexenv)
1069 lexenv)))
1071 ;;;; flow/DFO/component hackery
1073 ;;; Join BLOCK1 and BLOCK2.
1074 (defun link-blocks (block1 block2)
1075 (declare (type cblock block1 block2))
1076 (setf (block-succ block1)
1077 (if (block-succ block1)
1078 (%link-blocks block1 block2)
1079 (list block2)))
1080 (push block1 (block-pred block2))
1081 (values))
1082 (defun %link-blocks (block1 block2)
1083 (declare (type cblock block1 block2))
1084 (let ((succ1 (block-succ block1)))
1085 (aver (not (memq block2 succ1)))
1086 (cons block2 succ1)))
1088 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2.
1089 (defun unlink-blocks (block1 block2)
1090 (declare (type cblock block1 block2))
1091 (let ((succ1 (block-succ block1)))
1092 (if (eq block2 (car succ1))
1093 (setf (block-succ block1) (cdr succ1))
1094 (do ((succ (cdr succ1) (cdr succ))
1095 (prev succ1 succ))
1096 ((eq (car succ) block2)
1097 (setf (cdr prev) (cdr succ)))
1098 (aver succ))))
1100 (setf (block-pred block2)
1101 (delq block1 (block-pred block2)))
1102 (values))
1104 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
1105 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
1106 ;;; consequent/alternative blocks to point to NEW.
1107 (defun change-block-successor (block old new)
1108 (declare (type cblock new old block))
1109 (unlink-blocks block old)
1110 (let ((last (block-last block))
1111 (comp (block-component block)))
1112 (setf (component-reanalyze comp) t)
1113 (typecase last
1114 (cif
1115 (let* ((succ-left (block-succ block))
1116 (new (if (and (eq new (component-tail comp))
1117 succ-left)
1118 (first succ-left)
1119 new)))
1120 (unless (memq new succ-left)
1121 (link-blocks block new))
1122 (macrolet ((frob (slot)
1123 `(when (eq (,slot last) old)
1124 (setf (,slot last) new))))
1125 (frob if-consequent)
1126 (frob if-alternative)
1127 (when (eq (if-consequent last)
1128 (if-alternative last))
1129 (reoptimize-component (block-component block) :maybe)))))
1131 (unless (memq new (block-succ block))
1132 (link-blocks block new)))))
1134 (values))
1136 ;;; Unlink a block from the next/prev chain. We also null out the
1137 ;;; COMPONENT.
1138 (declaim (ftype (sfunction (cblock) (values)) remove-from-dfo))
1139 (defun remove-from-dfo (block)
1140 (let ((next (block-next block))
1141 (prev (block-prev block)))
1142 (setf (block-component block) nil)
1143 (setf (block-next prev) next)
1144 (setf (block-prev next) prev))
1145 (values))
1147 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
1148 ;;; COMPONENT to be the same as for AFTER.
1149 (defun add-to-dfo (block after)
1150 (declare (type cblock block after))
1151 (let ((next (block-next after))
1152 (comp (block-component after)))
1153 (aver (not (eq (component-kind comp) :deleted)))
1154 (setf (block-component block) comp)
1155 (setf (block-next after) block)
1156 (setf (block-prev block) after)
1157 (setf (block-next block) next)
1158 (setf (block-prev next) block))
1159 (values))
1161 ;;; List all NLX-INFOs which BLOCK can exit to.
1163 ;;; We hope that no cleanup actions are performed in the middle of
1164 ;;; BLOCK, so it is enough to look only at cleanups in the block
1165 ;;; end. The tricky thing is a special cleanup block; all its nodes
1166 ;;; have the same cleanup info, corresponding to the start, so the
1167 ;;; same approach returns safe result.
1168 (defun map-block-nlxes (fun block &optional dx-cleanup-fun)
1169 (do-nested-cleanups (cleanup (block-end-lexenv block))
1170 (let ((mess-up (cleanup-mess-up cleanup)))
1171 (case (cleanup-kind cleanup)
1172 ((:block :tagbody)
1173 (aver (entry-p mess-up))
1174 (loop for exit in (entry-exits mess-up)
1175 for nlx-info = (exit-nlx-info exit)
1176 do (funcall fun nlx-info)))
1177 ((:catch :unwind-protect)
1178 (aver (combination-p mess-up))
1179 (let* ((arg-lvar (first (basic-combination-args mess-up)))
1180 (nlx-info (constant-value (ref-leaf (lvar-use arg-lvar)))))
1181 (funcall fun nlx-info)))
1182 ((:dynamic-extent)
1183 (when dx-cleanup-fun
1184 (funcall dx-cleanup-fun cleanup)))))))
1186 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
1187 ;;; the head and tail which are set to T.
1188 (declaim (ftype (sfunction (component) (values)) clear-flags))
1189 (defun clear-flags (component)
1190 (let ((head (component-head component))
1191 (tail (component-tail component)))
1192 (setf (block-flag head) t)
1193 (setf (block-flag tail) t)
1194 (do-blocks (block component)
1195 (setf (block-flag block) nil)))
1196 (values))
1198 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
1199 ;;; true in the head and tail blocks.
1200 (declaim (ftype (sfunction () component) make-empty-component))
1201 (defun make-empty-component ()
1202 (let* ((head (make-block-key :start nil :component nil))
1203 (tail (make-block-key :start nil :component nil))
1204 (res (make-component head tail)))
1205 (setf (block-flag head) t)
1206 (setf (block-flag tail) t)
1207 (setf (block-component head) res)
1208 (setf (block-component tail) res)
1209 (setf (block-next head) tail)
1210 (setf (block-prev tail) head)
1211 res))
1213 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
1214 ;;; The new block is added to the DFO immediately following NODE's block.
1215 ;;; Returns the new block if it's created.
1216 (defun node-ends-block (node)
1217 (declare (type node node))
1218 (let* ((block (node-block node))
1219 (start (node-next node))
1220 (last (block-last block)))
1221 (check-type last node)
1222 (unless (eq last node)
1223 (aver (and (eq (ctran-kind start) :inside-block)
1224 (not (block-delete-p block))))
1225 (let* ((succ (block-succ block))
1226 (new-block
1227 (make-block-key :start start
1228 :component (block-component block)
1229 :succ succ :last last)))
1230 (setf (ctran-kind start) :block-start)
1231 (setf (ctran-use start) nil)
1232 (setf (block-last block) node)
1233 (setf (node-next node) nil)
1234 (dolist (b succ)
1235 (setf (block-pred b)
1236 (cons new-block (remove block (block-pred b)))))
1237 (setf (block-succ block) ())
1238 (link-blocks block new-block)
1239 (add-to-dfo new-block block)
1240 (setf (component-reanalyze (block-component block)) t)
1242 (do ((ctran start (node-next (ctran-next ctran))))
1243 ((not ctran))
1244 (setf (ctran-block ctran) new-block))
1245 new-block))))
1247 ;;;; deleting stuff
1249 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR.
1250 (defun delete-lambda-var (leaf)
1251 (declare (type lambda-var leaf))
1253 (setf (lambda-var-deleted leaf) t)
1254 ;; Iterate over all local calls flushing the corresponding argument,
1255 ;; allowing the computation of the argument to be deleted. We also
1256 ;; mark the LET for reoptimization, since it may be that we have
1257 ;; deleted its last variable.
1258 (let* ((fun (lambda-var-home leaf))
1259 (n (position leaf (lambda-vars fun))))
1260 (dolist (ref (leaf-refs fun))
1261 (let* ((lvar (node-lvar ref))
1262 (dest (and lvar (lvar-dest lvar))))
1263 (when (and (basic-combination-p dest)
1264 (eq (basic-combination-fun dest) lvar)
1265 (eq (basic-combination-kind dest) :local))
1266 (if (mv-combination-p dest)
1267 ;; Let FLUSH-DEAD-CODE deal with it
1268 ;; since it's a bit tricky to delete multiple-valued
1269 ;; args and existing code doesn't expect to see NIL in
1270 ;; mv-combination-args.
1271 (setf (block-flush-p (node-block dest)) t)
1272 (let* ((args (basic-combination-args dest))
1273 (arg (elt args n)))
1274 (reoptimize-lvar arg)
1275 (flush-dest arg)
1276 (setf (elt args n) nil)))))))
1278 ;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
1279 ;; too much difficulty, since we can efficiently implement
1280 ;; write-only variables. We iterate over the SETs, marking their
1281 ;; blocks for dead code flushing, since we can delete SETs whose
1282 ;; value is unused.
1283 (dolist (set (lambda-var-sets leaf))
1284 (setf (block-flush-p (node-block set)) t))
1286 (values))
1288 ;;; Note that something interesting has happened to VAR.
1289 (defun reoptimize-lambda-var (var)
1290 (declare (type lambda-var var))
1291 (let ((fun (lambda-var-home var)))
1292 ;; We only deal with LET variables, marking the corresponding
1293 ;; initial value arg as needing to be reoptimized.
1294 (when (and (eq (functional-kind fun) :let)
1295 (leaf-refs var))
1296 (do ((args (basic-combination-args
1297 (lvar-dest (node-lvar (first (leaf-refs fun)))))
1298 (cdr args))
1299 (vars (lambda-vars fun) (cdr vars)))
1300 ((eq (car vars) var)
1301 (reoptimize-lvar (car args))))))
1302 (values))
1304 ;;; Delete a function that has no references. This need only be called
1305 ;;; on functions that never had any references, since otherwise
1306 ;;; DELETE-REF will handle the deletion.
1307 (defun delete-functional (fun)
1308 (aver (and (null (leaf-refs fun))
1309 (not (functional-entry-fun fun))))
1310 (etypecase fun
1311 (optional-dispatch (delete-optional-dispatch fun))
1312 (clambda (delete-lambda fun)))
1313 (values))
1315 ;;; Deal with deleting the last reference to a CLAMBDA, which means
1316 ;;; that the lambda is unreachable, so that its body may be
1317 ;;; deleted. We set FUNCTIONAL-KIND to :DELETED and rely on
1318 ;;; IR1-OPTIMIZE to delete its blocks.
1319 (defun delete-lambda (clambda)
1320 (declare (type clambda clambda))
1321 (let ((original-kind (functional-kind clambda))
1322 (bind (lambda-bind clambda)))
1323 (aver (not (member original-kind '(:deleted :toplevel))))
1324 (aver (not (functional-has-external-references-p clambda)))
1325 (aver (or (eq original-kind :zombie) bind))
1326 (setf (functional-kind clambda) :deleted)
1327 (setf (lambda-bind clambda) nil)
1329 (labels ((delete-children (lambda)
1330 (dolist (child (lambda-children lambda))
1331 (cond ((eq (functional-kind child) :deleted)
1332 (delete-children child))
1334 (delete-lambda child))))
1335 (setf (lambda-children lambda) nil)
1336 (setf (lambda-parent lambda) nil)))
1337 (delete-children clambda))
1339 ;; (The IF test is (FUNCTIONAL-SOMEWHAT-LETLIKE-P CLAMBDA), except
1340 ;; that we're using the old value of the KIND slot, not the
1341 ;; current slot value, which has now been set to :DELETED.)
1342 (case original-kind
1343 (:zombie)
1344 ((:let :mv-let :assignment)
1345 (let ((bind-block (node-block bind)))
1346 (mark-for-deletion bind-block))
1347 (let ((home (lambda-home clambda)))
1348 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1349 ;; KLUDGE: In presence of NLEs we cannot always understand that
1350 ;; LET's BIND dominates its body [for a LET "its" body is not
1351 ;; quite its]; let's delete too dangerous for IR2 stuff. --
1352 ;; APD, 2004-01-01
1353 (dolist (var (lambda-vars clambda))
1354 (flet ((delete-node (node)
1355 (mark-for-deletion (node-block node))))
1356 (mapc #'delete-node (leaf-refs var))
1357 (mapc #'delete-node (lambda-var-sets var)))))
1359 ;; Function has no reachable references.
1360 (dolist (ref (lambda-refs clambda))
1361 (mark-for-deletion (node-block ref)))
1362 ;; If the function isn't a LET, we unlink the function head
1363 ;; and tail from the component head and tail to indicate that
1364 ;; the code is unreachable. We also delete the function from
1365 ;; COMPONENT-LAMBDAS (it won't be there before local call
1366 ;; analysis, but no matter.) If the lambda was never
1367 ;; referenced, we give a note.
1368 (let* ((bind-block (node-block bind))
1369 (component (block-component bind-block))
1370 (return (lambda-return clambda))
1371 (return-block (and return (node-block return))))
1372 (unless (leaf-ever-used clambda)
1373 (let ((*compiler-error-context* bind))
1374 (compiler-notify 'code-deletion-note
1375 :format-control "deleting unused function~:[.~;~:*~% ~S~]"
1376 :format-arguments (list (leaf-debug-name clambda)))))
1377 (unless (block-delete-p bind-block)
1378 (unlink-blocks (component-head component) bind-block))
1379 (when (and return-block (not (block-delete-p return-block)))
1380 (mark-for-deletion return-block)
1381 (unlink-blocks return-block (component-tail component)))
1382 (setf (component-reanalyze component) t)
1383 (let ((tails (lambda-tail-set clambda)))
1384 (setf (tail-set-funs tails)
1385 (delete clambda (tail-set-funs tails)))
1386 (setf (lambda-tail-set clambda) nil))
1387 (setf (component-lambdas component)
1388 (delq clambda (component-lambdas component))))))
1390 ;; If the lambda is an XEP, then we null out the ENTRY-FUN in its
1391 ;; ENTRY-FUN so that people will know that it is not an entry
1392 ;; point anymore.
1393 (when (eq original-kind :external)
1394 (let ((fun (functional-entry-fun clambda)))
1395 (setf (functional-entry-fun fun) nil)
1396 (when (optional-dispatch-p fun)
1397 (delete-optional-dispatch fun)))))
1399 (values))
1401 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
1402 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
1403 ;;; is used both before and after local call analysis. Afterward, all
1404 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
1405 ;;; to the XEP, leaving it with no references at all. So we look at
1406 ;;; the XEP to see whether an optional-dispatch is still really being
1407 ;;; used. But before local call analysis, there are no XEPs, and all
1408 ;;; references are direct.
1410 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
1411 ;;; entry-points, making them be normal lambdas, and then deleting the
1412 ;;; ones with no references. This deletes any e-p lambdas that were
1413 ;;; either never referenced, or couldn't be deleted when the last
1414 ;;; reference was deleted (due to their :OPTIONAL kind.)
1416 ;;; Note that the last optional entry point may alias the main entry,
1417 ;;; so when we process the main entry, its KIND may have been changed
1418 ;;; to NIL or even converted to a LETlike value.
1419 (defun delete-optional-dispatch (leaf)
1420 (declare (type optional-dispatch leaf))
1421 (let ((entry (functional-entry-fun leaf)))
1422 (unless (and entry
1423 (or (leaf-refs entry)
1424 (eq (functional-kind entry) :external)))
1425 (aver (or (not entry) (eq (functional-kind entry) :deleted)))
1426 (setf (functional-kind leaf) :deleted)
1428 (flet ((frob (fun)
1429 (unless (eq (functional-kind fun) :deleted)
1430 (aver (eq (functional-kind fun) :optional))
1431 (setf (functional-kind fun) nil)
1432 (let ((refs (leaf-refs fun)))
1433 (cond ((null refs)
1434 (delete-lambda fun))
1435 ((null (rest refs))
1436 (or (maybe-let-convert fun)
1437 (maybe-convert-to-assignment fun)))
1439 (maybe-convert-to-assignment fun)))))))
1441 (dolist (ep (optional-dispatch-entry-points leaf))
1442 (when (promise-ready-p ep)
1443 (frob (force ep))))
1444 (when (optional-dispatch-more-entry leaf)
1445 (frob (optional-dispatch-more-entry leaf)))
1446 (let ((main (optional-dispatch-main-entry leaf)))
1447 (when entry
1448 (setf (functional-entry-fun entry) main)
1449 (setf (functional-entry-fun main) entry))
1450 (when (eq (functional-kind main) :optional)
1451 (frob main))))))
1453 (values))
1455 ;;; This is called by locall-analyze-fun-1 after it convers a call to
1456 ;;; FUN into a local call.
1457 ;;; Presumably, the function can be no longer reused by new calls to
1458 ;;; FUN, so the whole thing has to be removed from *FREE-FUNS*
1459 (defun note-local-functional (fun)
1460 (declare (type functional fun))
1461 (when (and (leaf-has-source-name-p fun)
1462 (eq (leaf-source-name fun) (functional-debug-name fun)))
1463 (let* ((name (leaf-source-name fun))
1464 (defined-fun (gethash name *free-funs*)))
1465 (when (defined-fun-p defined-fun)
1466 (remhash name *free-funs*)))))
1468 ;;; Return functional for DEFINED-FUN which has been converted in policy
1469 ;;; corresponding to the current one, or NIL if no such functional exists.
1471 ;;; Also check that the parent of the functional is visible in the current
1472 ;;; environment and is in the current component.
1473 (defun defined-fun-functional (defined-fun)
1474 (let ((functionals (defined-fun-functionals defined-fun)))
1475 (when functionals
1476 (let* ((sample (car functionals))
1477 (there (lambda-parent (if (lambda-p sample)
1478 sample
1479 (optional-dispatch-main-entry sample)))))
1480 (when there
1481 (labels ((lookup (here)
1482 (unless (eq here there)
1483 (if here
1484 (lookup (lambda-parent here))
1485 ;; We looked up all the way up, and didn't find the parent
1486 ;; of the functional -- therefore it is nested in a lambda
1487 ;; we don't see, so return nil.
1488 (return-from defined-fun-functional nil)))))
1489 (lookup (lexenv-lambda *lexenv*)))))
1490 ;; Now find a functional whose policy matches the current one, if we already
1491 ;; have one.
1492 (let ((policy (lexenv-%policy *lexenv*)))
1493 (dolist (functional functionals)
1494 (when (and (not (memq (functional-kind functional) '(:deleted :zombie)))
1495 (policy= policy (lexenv-%policy (functional-lexenv functional)))
1496 ;; Is it in the same component
1497 (let ((home-lambda (lambda-home (main-entry functional))))
1498 (and (not (memq (functional-kind home-lambda) '(:deleted :zombie)))
1499 (eq (lambda-component home-lambda)
1500 *current-component*))))
1501 (return functional)))))))
1503 ;;; Do stuff to delete the semantic attachments of a REF node. When
1504 ;;; this leaves zero or one reference, we do a type dispatch off of
1505 ;;; the leaf to determine if a special action is appropriate.
1506 (defun delete-ref (ref)
1507 (declare (type ref ref))
1508 (let* ((leaf (ref-leaf ref))
1509 (refs (delq ref (leaf-refs leaf)))
1510 (home (node-home-lambda ref)))
1511 (setf (leaf-refs leaf) refs)
1512 (when (and (typep leaf '(or clambda lambda-var))
1513 (not (find home refs :key #'node-home-lambda)))
1514 ;; It was the last reference from this lambda, remove it
1515 (sset-delete leaf (lambda-calls-or-closes home)))
1516 (cond ((null refs)
1517 (typecase leaf
1518 (lambda-var
1519 (delete-lambda-var leaf))
1520 (clambda
1521 (ecase (functional-kind leaf)
1522 ((nil :let :mv-let :assignment :escape :cleanup)
1523 (aver (null (functional-entry-fun leaf)))
1524 (delete-lambda leaf))
1525 (:external
1526 (unless (functional-has-external-references-p leaf)
1527 (delete-lambda leaf)))
1528 ((:deleted :zombie :optional))))
1529 (optional-dispatch
1530 (unless (eq (functional-kind leaf) :deleted)
1531 (delete-optional-dispatch leaf)))))
1532 ((null (rest refs))
1533 (typecase leaf
1534 (clambda (or (maybe-let-convert leaf)
1535 (maybe-convert-to-assignment leaf)))
1536 (lambda-var (reoptimize-lambda-var leaf))))
1538 (typecase leaf
1539 (clambda (maybe-convert-to-assignment leaf))))))
1541 (values))
1543 ;;; This function is called to unlink a node from its LVAR;
1544 ;;; we assume that the LVAR's USE list has already been updated,
1545 ;;; and that we only have to mark the node as up for dead code
1546 ;;; elimination, and to clear it LVAR slot.
1547 (defun flush-node (node)
1548 (declare (type node node))
1549 (let* ((prev (node-prev node))
1550 (block (ctran-block prev)))
1551 (reoptimize-component (block-component block) t)
1552 (setf (block-attributep (block-flags block)
1553 flush-p type-check)
1555 (setf (node-lvar node) nil))
1557 ;;; This function is called by people who delete nodes; it provides a
1558 ;;; way to indicate that the value of a lvar is no longer used. We
1559 ;;; null out the LVAR-DEST, set FLUSH-P in the blocks containing uses
1560 ;;; of LVAR and set COMPONENT-REOPTIMIZE.
1561 (defun flush-dest (lvar)
1562 (declare (type (or lvar null) lvar))
1563 (unless (null lvar)
1564 (when (lvar-dynamic-extent lvar)
1565 (note-no-stack-allocation lvar :flush t))
1566 (setf (lvar-dest lvar) nil)
1567 (flush-lvar-externally-checkable-type lvar)
1568 (do-uses (use lvar)
1569 (flush-node use))
1570 (setf (lvar-uses lvar) nil))
1571 (values))
1573 (defun delete-dest (lvar)
1574 (when lvar
1575 (let* ((dest (lvar-dest lvar))
1576 (prev (node-prev dest)))
1577 (let ((block (ctran-block prev)))
1578 (unless (block-delete-p block)
1579 (mark-for-deletion block))))))
1581 ;;; Queue the block for deletion
1582 (defun delete-block-lazily (block)
1583 (declare (type cblock block))
1584 (unless (block-delete-p block)
1585 (setf (block-delete-p block) t)
1586 (push block (component-delete-blocks (block-component block)))))
1588 ;;; Do a graph walk backward from BLOCK, marking all predecessor
1589 ;;; blocks with the DELETE-P flag.
1590 (defun mark-for-deletion (block)
1591 (declare (type cblock block))
1592 (let* ((component (block-component block))
1593 (head (component-head component)))
1594 (labels ((helper (block)
1595 (delete-block-lazily block)
1596 (dolist (pred (block-pred block))
1597 (unless (or (block-delete-p pred)
1598 (eq pred head))
1599 (helper pred)))))
1600 (unless (block-delete-p block)
1601 (helper block)
1602 (setf (component-reanalyze component) t))))
1603 (values))
1605 ;;; This function does what is necessary to eliminate the code in it
1606 ;;; from the IR1 representation. This involves unlinking it from its
1607 ;;; predecessors and successors and deleting various node-specific
1608 ;;; semantic information. BLOCK must be already removed from
1609 ;;; COMPONENT-DELETE-BLOCKS.
1610 (defun delete-block (block &optional silent)
1611 (declare (type cblock block))
1612 (unless (block-component block)
1613 ;; Already deleted
1614 (return-from delete-block))
1615 #!+high-security (aver (not (memq block (component-delete-blocks (block-component block)))))
1616 (unless silent
1617 (note-block-deletion block))
1618 (setf (block-delete-p block) t)
1620 (dolist (b (block-pred block))
1621 (unlink-blocks b block)
1622 ;; In bug 147 the almost-all-blocks-have-a-successor invariant was
1623 ;; broken when successors were deleted without setting the
1624 ;; BLOCK-DELETE-P flags of their predececessors. Make sure that
1625 ;; doesn't happen again.
1626 (aver (not (and (null (block-succ b))
1627 (not (block-delete-p b))
1628 (not (eq b (component-head (block-component b))))))))
1629 (dolist (b (block-succ block))
1630 (unlink-blocks block b))
1632 (do-nodes-carefully (node block)
1633 (when (valued-node-p node)
1634 (delete-lvar-use node))
1635 (etypecase node
1636 (ref (delete-ref node))
1637 (cif (flush-dest (if-test node)))
1638 ;; The next two cases serve to maintain the invariant that a LET
1639 ;; always has a well-formed COMBINATION, REF and BIND. We delete
1640 ;; the lambda whenever we delete any of these, but we must be
1641 ;; careful that this LET has not already been partially deleted.
1642 (basic-combination
1643 (when (and (eq (basic-combination-kind node) :local)
1644 ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
1645 (lvar-uses (basic-combination-fun node)))
1646 (let ((fun (combination-lambda node)))
1647 ;; If our REF was the second-to-last ref, and has been
1648 ;; deleted, then FUN may be a LET for some other
1649 ;; combination.
1650 (when (and (functional-letlike-p fun)
1651 (eq (let-combination fun) node))
1652 (delete-lambda fun))))
1653 (flush-dest (basic-combination-fun node))
1654 (dolist (arg (basic-combination-args node))
1655 (when arg (flush-dest arg))))
1656 (bind
1657 (let ((lambda (bind-lambda node)))
1658 (unless (eq (functional-kind lambda) :deleted)
1659 (delete-lambda lambda))))
1660 (exit
1661 (let ((value (exit-value node))
1662 (entry (exit-entry node)))
1663 (when value
1664 (flush-dest value))
1665 (when entry
1666 (setf (entry-exits entry)
1667 (delq node (entry-exits entry))))))
1668 (entry
1669 (dolist (exit (entry-exits node))
1670 (mark-for-deletion (node-block exit)))
1671 (let ((home (node-home-lambda node)))
1672 (setf (lambda-entries home) (delq node (lambda-entries home)))))
1673 (creturn
1674 (flush-dest (return-result node))
1675 (delete-return node))
1676 (cset
1677 (flush-dest (set-value node))
1678 (let ((var (set-var node)))
1679 (setf (basic-var-sets var)
1680 (delete node (basic-var-sets var)))))
1681 (dependent-cast
1682 (loop for dep in (dependent-cast-deps node)
1683 when (lvar-p dep)
1684 do (setf (lvar-dependent-casts dep)
1685 (delq node (lvar-dependent-casts dep))))
1686 (flush-dest (cast-value node)))
1687 (cast
1688 (flush-dest (cast-value node)))))
1690 (remove-from-dfo block)
1691 (values))
1693 ;;; Do stuff to indicate that the return node NODE is being deleted.
1694 (defun delete-return (node)
1695 (declare (type creturn node))
1696 (let ((fun (return-lambda node)))
1697 (when fun ;; could become replaced by MOVE-RETURN-STUFF
1698 (let ((tail-set (lambda-tail-set fun)))
1699 (aver (lambda-return fun))
1700 (setf (lambda-return fun) nil)
1701 (when (and tail-set (not (find-if #'lambda-return
1702 (tail-set-funs tail-set))))
1703 (setf (tail-set-type tail-set) *empty-type*)))))
1704 (values))
1706 ;;; If any of the VARS in FUN was never referenced and was not
1707 ;;; declared IGNORE, then complain.
1708 (defun note-unreferenced-vars (vars policy)
1709 (dolist (var vars)
1710 (unless (or (leaf-ever-used var)
1711 (lambda-var-ignorep var))
1712 (unless (policy policy (= inhibit-warnings 3))
1713 ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
1714 ;; requires this to be no more than a STYLE-WARNING.
1715 #-sb-xc-host
1716 (compiler-style-warn "The variable ~S is defined but never used."
1717 (leaf-debug-name var))
1718 ;; There's no reason to accept this kind of equivocation
1719 ;; when compiling our own code, though.
1720 #+sb-xc-host
1721 (warn "The variable ~S is defined but never used."
1722 (leaf-debug-name var)))
1723 (setf (leaf-ever-used var) t)))) ; to avoid repeated warnings? -- WHN
1725 (defun note-unreferenced-fun-vars (fun)
1726 (declare (type clambda fun))
1727 (let ((*compiler-error-context* (lambda-bind fun)))
1728 (note-unreferenced-vars (lambda-vars fun)
1729 *compiler-error-context*))
1730 (values))
1732 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
1733 ;;; our recursion so that we don't get lost in circular structures. We
1734 ;;; ignore the car of forms if they are a symbol (to prevent confusing
1735 ;;; function referencess with variables), and we also ignore anything
1736 ;;; inside ' or #'.
1737 (defun present-in-form (obj form depth)
1738 (declare (type (integer 0 20) depth))
1739 (cond ((= depth 20) nil)
1740 ((eq obj form) t)
1741 ((atom form) nil)
1743 (let ((first (car form))
1744 (depth (1+ depth)))
1745 (if (member first '(quote function))
1747 (or (and (not (symbolp first))
1748 (present-in-form obj first depth))
1749 (do ((l (cdr form) (cdr l))
1750 (n 0 (1+ n)))
1751 ((or (atom l) (> n 100))
1752 nil)
1753 (declare (fixnum n))
1754 (when (present-in-form obj (car l) depth)
1755 (return t)))))))))
1757 ;;; This function is called on a block immediately before we delete
1758 ;;; it. We check to see whether any of the code about to die appeared
1759 ;;; in the original source, and emit a note if so.
1761 ;;; If the block was in a lambda is now deleted, then we ignore the
1762 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
1763 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
1764 ;;; reasonable for a function to not return, and there is a different
1765 ;;; note for that case anyway.
1767 ;;; If the actual source is an atom, then we use a bunch of heuristics
1768 ;;; to guess whether this reference really appeared in the original
1769 ;;; source:
1770 ;;; -- If a symbol, it must be interned and not a keyword.
1771 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
1772 ;;; or a character.)
1773 ;;; -- The atom must be "present" in the original source form, and
1774 ;;; present in all intervening actual source forms.
1775 (defun note-block-deletion (block)
1776 (let ((home (block-home-lambda block)))
1777 (unless (eq (functional-kind home) :deleted)
1778 (do-nodes (node nil block)
1779 (let* ((path (node-source-path node))
1780 (first (first path)))
1781 (when (and (not (return-p node))
1782 ;; CASTs are just value filters and do not
1783 ;; represent code and they can be moved around
1784 ;; making CASTs from the original source code
1785 ;; appear in code inserted by the compiler, generating
1786 ;; false deletion notes.
1787 ;; And if a block with the original source gets
1788 ;; deleted the node that produces the value for
1789 ;; the CAST will get a note, no need to note
1790 ;; twice.
1791 (not (cast-p node))
1792 ;; Nothing interesting in BIND nodes
1793 (not (bind-p node))
1794 (or (eq first 'original-source-start)
1795 (and (atom first)
1796 (or (not (symbolp first))
1797 (let ((pkg (symbol-package first)))
1798 (and pkg
1799 (not (eq pkg (symbol-package :end))))))
1800 (not (member first '(t nil)))
1801 (not (typep first '(or fixnum character)))
1802 (every (lambda (x)
1803 (present-in-form first x 0))
1804 (source-path-forms path))
1805 (present-in-form first (find-original-source path)
1806 0))))
1807 (let ((*compiler-error-context* node))
1808 (compiler-notify 'code-deletion-note
1809 :format-control "deleting unreachable code"
1810 :format-arguments nil))
1811 (return))))))
1812 (values))
1814 ;;; Delete a node from a block, deleting the block if there are no
1815 ;;; nodes left. We remove the node from the uses of its LVAR.
1817 ;;; If the node is the last node, there must be exactly one successor.
1818 ;;; We link all of our precedessors to the successor and unlink the
1819 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
1820 ;;; left, and the block is a successor of itself, then we replace the
1821 ;;; only node with a degenerate exit node. This provides a way to
1822 ;;; represent the bodyless infinite loop, given the prohibition on
1823 ;;; empty blocks in IR1.
1824 (defun unlink-node (node)
1825 (declare (type node node))
1826 (when (valued-node-p node)
1827 (delete-lvar-use node))
1829 (let* ((ctran (node-next node))
1830 (next (and ctran (ctran-next ctran)))
1831 (prev (node-prev node))
1832 (block (ctran-block prev))
1833 (prev-kind (ctran-kind prev))
1834 (last (block-last block)))
1835 (cond ((or (eq prev-kind :inside-block)
1836 (and (eq prev-kind :block-start)
1837 (not (eq node last))))
1838 (cond ((eq node last)
1839 (setf (block-last block) (ctran-use prev))
1840 (setf (node-next (ctran-use prev)) nil))
1842 (setf (ctran-next prev) next)
1843 (setf (node-prev next) prev)
1844 (when (if-p next) ; AOP wanted
1845 (reoptimize-lvar (if-test next)))))
1846 (setf (node-prev node) nil)
1847 nil)
1849 (aver (eq prev-kind :block-start))
1850 (aver (eq node last))
1851 (let* ((succ (block-succ block))
1852 (next (first succ)))
1853 (aver (singleton-p succ))
1854 (cond
1855 ((eq block (first succ))
1856 (with-ir1-environment-from-node node
1857 (let ((exit (make-exit)))
1858 (setf (ctran-next prev) nil)
1859 (link-node-to-previous-ctran exit prev)
1860 (setf (block-last block) exit)))
1861 (setf (node-prev node) nil)
1862 nil)
1864 (aver (eq (block-start-cleanup block)
1865 (block-end-cleanup block)))
1866 (unlink-blocks block next)
1867 (dolist (pred (block-pred block))
1868 (change-block-successor pred block next))
1869 (when (block-delete-p block)
1870 (let ((component (block-component block)))
1871 (setf (component-delete-blocks component)
1872 (delq block (component-delete-blocks component)))))
1873 (remove-from-dfo block)
1874 (setf (block-delete-p block) t)
1875 (setf (node-prev node) nil)
1876 t)))))))
1878 ;;; Return true if CTRAN has been deleted, false if it is still a valid
1879 ;;; part of IR1.
1880 (defun ctran-deleted-p (ctran)
1881 (declare (type ctran ctran))
1882 (let ((block (ctran-block ctran)))
1883 (or (not (block-component block))
1884 (block-delete-p block))))
1886 ;;; Return true if NODE has been deleted, false if it is still a valid
1887 ;;; part of IR1.
1888 (defun node-deleted (node)
1889 (declare (type node node))
1890 (let ((prev (node-prev node)))
1891 (or (not prev)
1892 (ctran-deleted-p prev))))
1894 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1895 ;;; marking the blocks as DELETE-P to prevent weird stuff from being
1896 ;;; triggered by deletion.
1897 (defun delete-component (component)
1898 (declare (type component component))
1899 (aver (null (component-new-functionals component)))
1900 (setf (component-kind component) :deleted)
1901 (do-blocks (block component)
1902 (delete-block-lazily block))
1903 (dolist (fun (component-lambdas component))
1904 (unless (eq (functional-kind fun) :deleted)
1905 (setf (functional-kind fun) nil)
1906 (setf (functional-entry-fun fun) nil)
1907 (setf (leaf-refs fun) nil)
1908 (delete-functional fun)))
1909 (clean-component component)
1910 (values))
1912 ;;; Remove all pending blocks to be deleted. Return the nearest live
1913 ;;; block after or equal to BLOCK.
1914 (defun clean-component (component &optional block)
1915 (loop while (component-delete-blocks component)
1916 ;; actual deletion of a block may queue new blocks
1917 do (let ((current (pop (component-delete-blocks component))))
1918 (when (eq block current)
1919 (setq block (block-next block)))
1920 (delete-block current)))
1921 block)
1923 ;;; Convert code of the form
1924 ;;; (FOO ... (FUN ...) ...)
1925 ;;; to
1926 ;;; (FOO ... ... ...).
1927 ;;; In other words, replace the function combination FUN by its
1928 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1929 ;;; to blow out of whatever transform called this. Note, as the number
1930 ;;; of arguments changes, the transform must be prepared to return a
1931 ;;; lambda with a new lambda-list with the correct number of
1932 ;;; arguments.
1933 (defun splice-fun-args (lvar fun num-args)
1934 "If LVAR is a call to FUN with NUM-ARGS args, change those arguments to feed
1935 directly to the LVAR-DEST of LVAR, which must be a combination. If FUN
1936 is :ANY, the function name is not checked."
1937 (declare (type lvar lvar)
1938 (type symbol fun)
1939 (type index num-args))
1940 (let ((outside (lvar-dest lvar))
1941 (inside (lvar-uses lvar)))
1942 (aver (combination-p outside))
1943 (unless (combination-p inside)
1944 (give-up-ir1-transform))
1945 (let ((inside-fun (combination-fun inside)))
1946 (unless (or (eq fun :any)
1947 (eq (lvar-fun-name inside-fun) fun))
1948 (give-up-ir1-transform))
1949 (let ((inside-args (combination-args inside)))
1950 (unless (= (length inside-args) num-args)
1951 (give-up-ir1-transform))
1952 (let* ((outside-args (combination-args outside))
1953 (arg-position (position lvar outside-args))
1954 (before-args (subseq outside-args 0 arg-position))
1955 (after-args (subseq outside-args (1+ arg-position))))
1956 (dolist (arg inside-args)
1957 (setf (lvar-dest arg) outside)
1958 (flush-lvar-externally-checkable-type arg))
1959 (setf (combination-args inside) nil)
1960 (setf (combination-args outside)
1961 (append before-args inside-args after-args))
1962 (change-ref-leaf (lvar-uses inside-fun)
1963 (find-free-fun 'list "???"))
1964 (setf (combination-fun-info inside) (info :function :info 'list)
1965 (combination-kind inside) :known)
1966 (setf (node-derived-type inside) *wild-type*)
1967 (flush-dest lvar)
1968 inside-args)))))
1970 ;;; Eliminate keyword arguments from the call (leaving the
1971 ;;; parameters in place.
1973 ;;; (FOO ... :BAR X :QUUX Y)
1974 ;;; becomes
1975 ;;; (FOO ... X Y)
1977 ;;; SPECS is a list of (:KEYWORD PARAMETER) specifications.
1978 ;;; Returns the list of specified parameters names in the
1979 ;;; order they appeared in the call. N-POSITIONAL is the
1980 ;;; number of positional arguments in th call.
1981 (defun eliminate-keyword-args (call n-positional specs)
1982 (let* ((specs (copy-tree specs))
1983 (all (combination-args call))
1984 (new-args (reverse (subseq all 0 n-positional)))
1985 (key-args (subseq all n-positional))
1986 (parameters nil)
1987 (flushed-keys nil))
1988 (loop while key-args
1989 do (let* ((key (pop key-args))
1990 (val (pop key-args))
1991 (keyword (if (constant-lvar-p key)
1992 (lvar-value key)
1993 (give-up-ir1-transform)))
1994 (spec (or (assoc keyword specs :test #'eq)
1995 (give-up-ir1-transform))))
1996 (push val new-args)
1997 (push key flushed-keys)
1998 (push (second spec) parameters)
1999 ;; In case of duplicate keys.
2000 (setf (second spec) (gensym))))
2001 (dolist (key flushed-keys)
2002 (flush-dest key))
2003 (setf (combination-args call) (reverse new-args))
2004 (reverse parameters)))
2006 (defun extract-fun-args (lvar fun num-args)
2007 (declare (type lvar lvar)
2008 (type (or symbol list) fun)
2009 (type index num-args))
2010 (let ((inside (lvar-uses lvar)))
2011 (unless (combination-p inside)
2012 (give-up-ir1-transform))
2013 (let ((inside-fun (combination-fun inside)))
2014 (unless (member (lvar-fun-name inside-fun) (ensure-list fun))
2015 (give-up-ir1-transform))
2016 (let ((inside-args (combination-args inside)))
2017 (unless (= (length inside-args) num-args)
2018 (give-up-ir1-transform))
2019 (values (lvar-fun-name inside-fun) inside-args)))))
2021 (defun flush-combination (combination)
2022 (declare (type combination combination))
2023 (flush-dest (combination-fun combination))
2024 (dolist (arg (combination-args combination))
2025 (flush-dest arg))
2026 (unlink-node combination)
2027 (values))
2030 ;;;; leaf hackery
2032 ;;; Change the LEAF that a REF refers to.
2033 (defun change-ref-leaf (ref leaf &key recklessly)
2034 (declare (type ref ref) (type leaf leaf))
2035 (unless (eq (ref-leaf ref) leaf)
2036 (push ref (leaf-refs leaf))
2037 (delete-ref ref)
2038 (setf (ref-leaf ref) leaf)
2039 (setf (leaf-ever-used leaf) t)
2040 (let* ((ltype (leaf-type leaf))
2041 (vltype (make-single-value-type ltype)))
2042 (if (let* ((lvar (node-lvar ref))
2043 (dest (and lvar (lvar-dest lvar))))
2044 (and (basic-combination-p dest)
2045 (eq lvar (basic-combination-fun dest))
2046 (csubtypep ltype (specifier-type 'function))))
2047 (setf (node-derived-type ref) vltype)
2048 (derive-node-type ref vltype :from-scratch recklessly)))
2049 (reoptimize-lvar (node-lvar ref)))
2050 (values))
2052 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
2053 (defun substitute-leaf (new-leaf old-leaf)
2054 (declare (type leaf new-leaf old-leaf))
2055 (dolist (ref (leaf-refs old-leaf))
2056 (change-ref-leaf ref new-leaf))
2057 (values))
2059 ;;; like SUBSITUTE-LEAF, only there is a predicate on the REF to tell
2060 ;;; whether to substitute
2061 (defun substitute-leaf-if (test new-leaf old-leaf)
2062 (declare (type leaf new-leaf old-leaf) (type function test))
2063 (dolist (ref (leaf-refs old-leaf))
2064 (when (funcall test ref)
2065 (change-ref-leaf ref new-leaf)))
2066 (values))
2068 ;;; Return a LEAF which represents the specified constant object. If
2069 ;;; the object is not in *CONSTANTS*, then we create a new constant
2070 ;;; LEAF and enter it. If we are producing a fasl file, make sure that
2071 ;;; MAKE-LOAD-FORM gets used on any parts of the constant that it
2072 ;;; needs to be.
2074 ;;; We are allowed to coalesce things like EQUAL strings and bit-vectors
2075 ;;; when file-compiling, but not when using COMPILE.
2076 (defun find-constant (object &optional (name nil namep))
2077 (let ((faslp (producing-fasl-file)))
2078 (labels ((make-it ()
2079 (when faslp
2080 (if namep
2081 (maybe-emit-make-load-forms object name)
2082 (maybe-emit-make-load-forms object)))
2083 (make-constant object))
2084 (core-coalesce-p (x)
2085 ;; True for things which retain their identity under EQUAL,
2086 ;; so we can safely share the same CONSTANT leaf between
2087 ;; multiple references.
2088 (or (typep x '(or symbol number character))
2089 ;; Amusingly enough, we see CLAMBDAs --among other things--
2090 ;; here, from compiling things like %ALLOCATE-CLOSUREs forms.
2091 ;; No point in stuffing them in the hash-table.
2092 (and (typep x 'instance)
2093 (not (or (leaf-p x) (node-p x))))))
2094 (cons-coalesce-p (x)
2095 (if (eq +code-coverage-unmarked+ (cdr x))
2096 ;; These are already coalesced, and the CAR should
2097 ;; always be OK, so no need to check.
2099 (when (coalesce-tree-p x)
2100 (labels ((descend (x)
2101 (do ((y x (cdr y)))
2102 ((atom y) (atom-colesce-p y))
2103 ;; Don't just call file-coalesce-p, because it'll
2104 ;; invoke COALESCE-TREE-P repeatedly
2105 (let ((car (car y)))
2106 (unless (if (consp car)
2107 (descend car)
2108 (atom-colesce-p car))
2109 (return nil))))))
2110 (descend x)))))
2111 (atom-colesce-p (x)
2112 (or (core-coalesce-p x)
2113 ;; We *could* coalesce base-strings as well,
2114 ;; but we'd need a separate hash-table for
2115 ;; that, since we are not allowed to coalesce
2116 ;; base-strings with non-base-strings.
2117 (typep x
2118 '(or bit-vector
2119 ;; in the cross-compiler, we coalesce
2120 ;; all strings with the same contents,
2121 ;; because we will end up dumping them
2122 ;; as base-strings anyway. In the
2123 ;; real compiler, we're not allowed to
2124 ;; coalesce regardless of string
2125 ;; specialized element type, so we
2126 ;; KLUDGE by coalescing only character
2127 ;; strings (the common case) and
2128 ;; punting on the other types.
2129 #+sb-xc-host
2130 string
2131 #-sb-xc-host
2132 (vector character)))))
2133 (file-coalesce-p (x)
2134 ;; CLHS 3.2.4.2.2: We are also allowed to coalesce various
2135 ;; other things when file-compiling.
2136 (if (consp x)
2137 (cons-coalesce-p x)
2138 (atom-colesce-p x)))
2139 (coalescep (x)
2140 (if faslp (file-coalesce-p x) (core-coalesce-p x))))
2141 ;; When compiling to core we don't coalesce strings, because
2142 ;; "The functions eval and compile are required to ensure that literal objects
2143 ;; referenced within the resulting interpreted or compiled code objects are
2144 ;; the _same_ as the corresponding objects in the source code."
2145 ;; but in a dumped image, if gc_coalesce_string_literals is 1 then GC will
2146 ;; coalesce similar immutable strings to save memory,
2147 ;; even if not technically permitted. According to CLHS 3.7.1
2148 ;; "The consequences are undefined if literal objects are destructively modified
2149 ;; For this purpose, the following operations are considered destructive:
2150 ;; array - Storing a new value into some element of the array ..."
2151 ;; so a string, once used as a literal in source, becomes logically immutable.
2152 #-sb-xc-host
2153 (when (and (not faslp) (simple-string-p object))
2154 (logically-readonlyize object nil))
2155 (if (and (boundp '*constants*) (coalescep object))
2156 (ensure-gethash object *constants* (make-it))
2157 (make-it)))))
2159 ;;; Return true if VAR would have to be closed over if environment
2160 ;;; analysis ran now (i.e. if there are any uses that have a different
2161 ;;; home lambda than VAR's home.)
2162 (defun closure-var-p (var)
2163 (declare (type lambda-var var))
2164 (let ((home (lambda-var-home var)))
2165 (cond ((eq (functional-kind home) :deleted)
2166 nil)
2167 (t (let ((home (lambda-home home)))
2168 (flet ((frob (l)
2169 (find home l
2170 :key #'node-home-lambda
2171 :test #'neq)))
2172 (or (frob (leaf-refs var))
2173 (frob (basic-var-sets var)))))))))
2175 ;;; If there is a non-local exit noted in ENTRY's environment that
2176 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
2177 (defun find-nlx-info (exit)
2178 (declare (type exit exit))
2179 (let* ((entry (exit-entry exit))
2180 (cleanup (entry-cleanup entry))
2181 (block (first (block-succ (node-block exit)))))
2182 (dolist (nlx (physenv-nlx-info (node-physenv entry)) nil)
2183 (when (and (eq (nlx-info-block nlx) block)
2184 (eq (nlx-info-cleanup nlx) cleanup))
2185 (return nlx)))))
2187 (defun nlx-info-lvar (nlx)
2188 (declare (type nlx-info nlx))
2189 (node-lvar (block-last (nlx-info-target nlx))))
2191 ;;;; functional hackery
2193 (declaim (ftype (sfunction (functional) clambda) main-entry))
2194 (defun main-entry (functional)
2195 (etypecase functional
2196 (clambda functional)
2197 (optional-dispatch
2198 (optional-dispatch-main-entry functional))))
2200 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
2201 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
2202 ;;; optional with null default and no SUPPLIED-P. There must be a
2203 ;;; &REST arg with no references.
2204 (declaim (ftype (sfunction (functional) boolean) looks-like-an-mv-bind))
2205 (defun looks-like-an-mv-bind (functional)
2206 (and (optional-dispatch-p functional)
2207 (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
2208 ((null arg) nil)
2209 (let ((info (lambda-var-arg-info (car arg))))
2210 (unless info (return nil))
2211 (case (arg-info-kind info)
2212 (:optional
2213 (when (or (arg-info-supplied-p info) (arg-info-default info))
2214 (return nil)))
2215 (:rest
2216 (return (and (null (cdr arg))
2217 (null (leaf-refs (car arg)))
2218 ;; Type checking will require reading the
2219 ;; variable, but it's done in one of the
2220 ;; dispatch functions making it invisible
2221 ;; to LEAF-REFS
2222 (or (neq (leaf-where-from (car arg)) :declared)
2223 (values (csubtypep (specifier-type 'list)
2224 (leaf-type (car arg))))))))
2226 (return nil)))))))
2228 (defun call-all-args-fixed-p (call)
2229 (loop for arg in (basic-combination-args call)
2230 always (numberp (nth-value 1 (values-types
2231 (lvar-derived-type arg))))))
2233 ;;; Return true if function is an external entry point. This is true
2234 ;;; of normal XEPs (:EXTERNAL kind) and also of top level lambdas
2235 ;;; (:TOPLEVEL kind.)
2236 (defun xep-p (fun)
2237 (declare (type functional fun))
2238 (not (null (member (functional-kind fun) '(:external :toplevel)))))
2240 ;;; If LVAR's only use is a non-notinline global function reference,
2241 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
2242 ;;; is true, then we don't care if the leaf is NOTINLINE.
2243 (defun lvar-fun-name (lvar &optional notinline-ok)
2244 (declare (type lvar lvar))
2245 (let ((use (principal-lvar-use lvar)))
2246 (if (ref-p use)
2247 (let ((leaf (ref-leaf use)))
2248 (if (and (global-var-p leaf)
2249 (eq (global-var-kind leaf) :global-function)
2250 (or (not (defined-fun-p leaf))
2251 (not (eq (defined-fun-inlinep leaf) :notinline))
2252 notinline-ok))
2253 (leaf-source-name leaf)
2254 nil))
2255 nil)))
2257 ;;; As above, but allow a quoted symbol also,
2258 ;;; in which case we don't check for notinline-ness,
2259 ;;; so be careful how you use this.
2260 ;;; Also note that Case 2 in LVAR-FUN-IS for dealing with #.#'NAME
2261 ;;; has no equivalent here.
2262 (defun lvar-fun-name* (lvar)
2263 (if (constant-lvar-p lvar) (lvar-value lvar) (lvar-fun-name lvar)))
2265 (defun lvar-fun-debug-name (lvar)
2266 (declare (type lvar lvar))
2267 (let ((uses (lvar-uses lvar)))
2268 (flet ((name1 (use)
2269 (leaf-debug-name (ref-leaf use))))
2270 (if (ref-p uses)
2271 (name1 uses)
2272 (mapcar #'name1 uses)))))
2274 ;;; Return the source name of a combination -- or signals an error
2275 ;;; if the function leaf is anonymous.
2276 (defun combination-fun-source-name (combination &optional (errorp t))
2277 (let ((uses (principal-lvar-use (combination-fun combination)))
2278 leaf)
2279 (cond ((and (ref-p uses)
2280 (leaf-has-source-name-p (setf leaf (ref-leaf uses))))
2281 (values (leaf-source-name leaf) t))
2282 (errorp
2283 (aver (not "COMBINATION-FUN is not a ref to a nameful leaf")))
2285 (values nil nil)))))
2287 (defun combination-fun-debug-name (combination)
2288 (let ((uses (principal-lvar-use (combination-fun combination))))
2289 (when (ref-p uses)
2290 (let ((leaf (ref-leaf uses)))
2291 (typecase leaf
2292 (functional
2293 (functional-debug-name leaf))
2295 (and (leaf-has-source-name-p leaf)
2296 (leaf-source-name leaf))))))))
2298 ;;; Return the COMBINATION node that is the call to the LET FUN.
2299 (defun let-combination (fun)
2300 (declare (type clambda fun))
2301 (aver (functional-letlike-p fun))
2302 (lvar-dest (node-lvar (first (leaf-refs fun)))))
2304 ;;; Return the initial value lvar for a LET variable, or NIL if there
2305 ;;; is none.
2306 (defun let-var-initial-value (var)
2307 (declare (type lambda-var var))
2308 (let ((fun (lambda-var-home var)))
2309 (elt (combination-args (let-combination fun))
2310 (position-or-lose var (lambda-vars fun)))))
2312 ;;; Return the LAMBDA that is called by the local CALL.
2313 (defun combination-lambda (call)
2314 (declare (type basic-combination call))
2315 (aver (eq (basic-combination-kind call) :local))
2316 (ref-leaf (lvar-uses (basic-combination-fun call))))
2318 (defvar *inline-expansion-limit* 200
2319 "an upper limit on the number of inline function calls that will be expanded
2320 in any given code object (single function or block compilation)")
2322 ;;; Check whether NODE's component has exceeded its inline expansion
2323 ;;; limit, and warn if so, returning NIL.
2324 (defun inline-expansion-ok (node)
2325 (let ((expanded (incf (component-inline-expansions
2326 (block-component
2327 (node-block node))))))
2328 (cond ((> expanded *inline-expansion-limit*) nil)
2329 ((= expanded *inline-expansion-limit*)
2330 ;; FIXME: If the objective is to stop the recursive
2331 ;; expansion of inline functions, wouldn't it be more
2332 ;; correct to look back through surrounding expansions
2333 ;; (which are, I think, stored in the *CURRENT-PATH*, and
2334 ;; possibly stored elsewhere too) and suppress expansion
2335 ;; and print this warning when the function being proposed
2336 ;; for inline expansion is found there? (I don't like the
2337 ;; arbitrary numerical limit in principle, and I think
2338 ;; it'll be a nuisance in practice if we ever want the
2339 ;; compiler to be able to use WITH-COMPILATION-UNIT on
2340 ;; arbitrarily huge blocks of code. -- WHN)
2341 (let ((*compiler-error-context* node))
2342 (compiler-notify "*INLINE-EXPANSION-LIMIT* (~W) was exceeded, ~
2343 probably trying to~% ~
2344 inline a recursive function."
2345 *inline-expansion-limit*))
2346 nil)
2347 (t t))))
2349 ;;; Make sure that FUNCTIONAL is not let-converted or deleted.
2350 (defun assure-functional-live-p (functional)
2351 (declare (type functional functional))
2352 (when (and (or
2353 ;; looks LET-converted
2354 (functional-somewhat-letlike-p functional)
2355 ;; It's possible for a LET-converted function to end up
2356 ;; deleted later. In that case, for the purposes of this
2357 ;; analysis, it is LET-converted: LET-converted functionals
2358 ;; are too badly trashed to expand them inline, and deleted
2359 ;; LET-converted functionals are even worse.
2360 (memq (functional-kind functional) '(:deleted :zombie))))
2361 (throw 'locall-already-let-converted functional)))
2363 (defun assure-leaf-live-p (leaf)
2364 (typecase leaf
2365 (lambda-var
2366 (when (lambda-var-deleted leaf)
2367 (throw 'locall-already-let-converted leaf)))
2368 (functional
2369 (assure-functional-live-p leaf))))
2372 (defun call-full-like-p (call)
2373 (declare (type basic-combination call))
2374 (let ((kind (basic-combination-kind call)))
2375 (or (eq kind :full)
2376 (and (eq kind :known)
2377 (let ((info (basic-combination-fun-info call)))
2378 (and
2379 (not (fun-info-ir2-convert info))
2380 (dolist (template (fun-info-templates info) t)
2381 (when (eq (template-ltn-policy template) :fast-safe)
2382 (multiple-value-bind (val win)
2383 (valid-fun-use call (template-type template))
2384 (when (or val (not win)) (return nil)))))))))))
2386 ;;;; careful call
2388 ;;; Apply a function to some arguments, returning a list of the values
2389 ;;; resulting of the evaluation. If an error is signalled during the
2390 ;;; application, then we produce a warning message using WARN-FUN and
2391 ;;; return NIL as our second value to indicate this. NODE is used as
2392 ;;; the error context for any error message, and CONTEXT is a string
2393 ;;; that is spliced into the warning.
2394 (declaim (ftype (sfunction ((or symbol function) list node function string)
2395 (values list boolean))
2396 careful-call))
2397 (defun careful-call (function args node warn-fun context)
2398 (values
2399 (multiple-value-list
2400 (handler-case (apply function args)
2401 (error (condition)
2402 (let ((*compiler-error-context* node))
2403 (funcall warn-fun "Lisp error during ~A:~%~A" context condition)
2404 (return-from careful-call (values nil nil))))))
2407 ;;; Variations of SPECIFIER-TYPE for parsing possibly wrong
2408 ;;; specifiers.
2409 (macrolet
2410 ((deffrob (basic careful compiler transform)
2411 `(progn
2412 (defun ,careful (specifier)
2413 (handler-case (,basic specifier)
2414 (error (condition)
2415 (values nil condition))))
2416 (defun ,compiler (specifier)
2417 (handler-case (,basic specifier)
2418 (simple-error (condition)
2419 (apply #'compiler-warn
2420 (simple-condition-format-control condition)
2421 (simple-condition-format-arguments condition)))
2422 (error (condition)
2423 (compiler-warn "~a" condition))))
2424 (defun ,transform (specifier)
2425 (multiple-value-bind (type condition) (,careful specifier)
2426 (or type
2427 (give-up-ir1-transform
2428 (princ-to-string condition))))))))
2429 (deffrob specifier-type careful-specifier-type compiler-specifier-type ir1-transform-specifier-type)
2430 (deffrob values-specifier-type careful-values-specifier-type compiler-values-specifier-type ir1-transform-values-specifier-type))
2433 ;;;; utilities used at run-time for parsing &KEY args in IR1
2435 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
2436 ;;; the lvar for the value of the &KEY argument KEY in the list of
2437 ;;; lvars ARGS. It returns the lvar if the keyword is present, or NIL
2438 ;;; otherwise. The legality and constantness of the keywords should
2439 ;;; already have been checked.
2440 (declaim (ftype (sfunction (list keyword) (or lvar null))
2441 find-keyword-lvar))
2442 (defun find-keyword-lvar (args key)
2443 (do ((arg args (cddr arg)))
2444 ((null arg) nil)
2445 (when (eq (lvar-value (first arg)) key)
2446 (return (second arg)))))
2448 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2449 ;;; verify that alternating lvars in ARGS are constant and that there
2450 ;;; is an even number of args.
2451 (declaim (ftype (sfunction (list) boolean) check-key-args-constant))
2452 (defun check-key-args-constant (args)
2453 (do ((arg args (cddr arg)))
2454 ((null arg) t)
2455 (unless (and (rest arg)
2456 (constant-lvar-p (first arg)))
2457 (return nil))))
2459 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
2460 ;;; verify that the list of lvars ARGS is a well-formed &KEY arglist
2461 ;;; and that only keywords present in the list KEYS are supplied.
2462 (declaim (ftype (sfunction (list list) boolean) check-transform-keys))
2463 (defun check-transform-keys (args keys)
2464 (and (check-key-args-constant args)
2465 (do ((arg args (cddr arg)))
2466 ((null arg) t)
2467 (unless (member (lvar-value (first arg)) keys)
2468 (return nil)))))
2470 ;;;; miscellaneous
2472 ;;; Called by the expansion of the EVENT macro.
2473 (declaim (ftype (sfunction (event-info (or node null)) *) %event))
2474 (defun %event (info node)
2475 (incf (event-info-count info))
2476 (when (and (>= (event-info-level info) *event-note-threshold*)
2477 (policy (or node *lexenv*)
2478 (= inhibit-warnings 0)))
2479 (let ((*compiler-error-context* node))
2480 (compiler-notify (event-info-description info))))
2482 (let ((action (event-info-action info)))
2483 (when action (funcall action node))))
2486 (defun make-cast (value type policy &optional context)
2487 (declare (type lvar value)
2488 (type ctype type)
2489 (type policy policy))
2490 (%make-cast :asserted-type type
2491 :type-to-check (maybe-weaken-check type policy)
2492 :value value
2493 :derived-type (coerce-to-values type)
2494 :context context))
2496 (defun note-single-valuified-lvar (lvar)
2497 (declare (type (or lvar null) lvar))
2498 (when lvar
2499 (let ((use (lvar-uses lvar)))
2500 (cond ((ref-p use)
2501 (let ((leaf (ref-leaf use)))
2502 (when (and (lambda-var-p leaf)
2503 (null (rest (leaf-refs leaf))))
2504 (reoptimize-lambda-var leaf))))
2505 ((or (listp use) (combination-p use))
2506 (do-uses (node lvar)
2507 (setf (node-reoptimize node) t)
2508 (setf (block-reoptimize (node-block node)) t)
2509 (reoptimize-component (node-component node) :maybe)))))))
2511 ;;; Return true if LVAR's only use is a reference to a global function
2512 ;;; designator with one of the specified NAMES, that hasn't been
2513 ;;; declared NOTINLINE.
2514 (defun lvar-fun-is (lvar names)
2515 (declare (type lvar lvar) (list names))
2516 (let ((use (principal-lvar-use lvar)))
2517 (and (ref-p use)
2518 (let* ((*lexenv* (node-lexenv use))
2519 (leaf (ref-leaf use))
2520 (name
2521 (cond ((global-var-p leaf)
2522 ;; Case 1: #'NAME
2523 (and (eq (global-var-kind leaf) :global-function)
2524 (car (member (leaf-source-name leaf) names
2525 :test #'equal))))
2526 ((constant-p leaf)
2527 (let ((value (constant-value leaf)))
2528 (car (if (functionp value)
2529 ;; Case 2: #.#'NAME
2530 (member value names
2531 :key (lambda (name)
2532 (and (fboundp name)
2533 (fdefinition name)))
2534 :test #'eq)
2535 ;; Case 3: 'NAME
2536 (member value names
2537 :test #'equal))))))))
2538 (and name
2539 (not (fun-lexically-notinline-p name)))))))
2541 ;;; Return true if LVAR's only use is a call to one of the named functions
2542 ;;; (or any function if none are specified) with the specified number of
2543 ;;; of arguments (or any number if number is not specified)
2544 (defun lvar-matches (lvar &key fun-names arg-count)
2545 (let ((use (lvar-uses lvar)))
2546 (and (combination-p use)
2547 (or (not fun-names)
2548 (multiple-value-bind (name ok)
2549 (combination-fun-source-name use nil)
2550 (and ok (member name fun-names :test #'eq))))
2551 (or (not arg-count)
2552 (= arg-count (length (combination-args use)))))))
2554 ;;; In (a (b lvar)) (lvar-matches-calls lvar '(b a)) would return T
2555 (defun lvar-matches-calls (lvar dest-fun-names)
2556 (loop for fun in dest-fun-names
2557 for dest = (principal-lvar-dest lvar)
2558 when (or (not (combination-p dest))
2559 (neq fun (combination-fun-source-name dest nil)))
2560 return nil
2561 do (setf lvar (combination-lvar dest))
2562 finally (return t)))
2564 ;;; True if the optional has a rest-argument.
2565 (defun optional-rest-p (opt)
2566 (dolist (var (optional-dispatch-arglist opt) nil)
2567 (let* ((info (when (lambda-var-p var)
2568 (lambda-var-arg-info var)))
2569 (kind (when info
2570 (arg-info-kind info))))
2571 (when (eq :rest kind)
2572 (return t)))))
2574 ;;; Don't substitute single-ref variables on high-debug / low speed, to
2575 ;;; improve the debugging experience. ...but don't bother keeping those
2576 ;;; from system lambdas.
2577 (defun preserve-single-use-debug-var-p (call var)
2578 (and (policy call (eql preserve-single-use-debug-variables 3))
2579 (or (not (lambda-var-p var))
2580 (not (lambda-system-lambda-p (lambda-var-home var))))))
2582 ;;; Call (lambda (arg lambda-var type)), for a mv-combination ARG can
2583 ;;; be NIL when it produces multiple values.
2584 ;;; If REOPTIMIZE is T only the arguments for which LVAR-REOPTIMIZE is
2585 ;;; true will be examined, resetting LVAR-REOPTIMIZE to NIL before
2586 ;;; calling FUNCTION.
2587 (defun map-combination-arg-var (function combination &key reoptimize)
2588 (let ((args (basic-combination-args combination))
2589 (vars (lambda-vars (combination-lambda combination))))
2590 (flet ((reoptimize-p (arg)
2591 (cond ((not arg) nil)
2592 ((not reoptimize))
2593 ((lvar-reoptimize arg)
2594 (setf (lvar-reoptimize arg) nil)
2595 t))))
2596 (cond ((combination-p combination)
2597 (loop for arg in args
2598 for var in vars
2599 when (reoptimize-p arg)
2601 (funcall function arg var (lvar-type arg))))
2602 ((singleton-p args)
2603 (when (reoptimize-p (first args))
2604 (loop with arg = (first args)
2605 for var in vars
2606 for type in (values-type-in (lvar-derived-type arg)
2607 (length vars))
2609 (funcall function
2610 (and (singleton-p vars)
2611 arg)
2613 type))))
2615 (loop for arg in args
2616 do (multiple-value-bind (types length) (values-types (lvar-derived-type arg))
2617 (when (eq length :unknown)
2618 (return))
2619 (if (reoptimize-p arg)
2620 (loop with singleton-arg = (and (= length 1)
2621 arg)
2622 for type in types
2623 while vars
2625 (funcall function singleton-arg
2626 (pop vars) type))
2627 (setf vars (nthcdr length vars))))))))))