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