31f8748e3afd5ebc6c3855b4ae2f33220e0a8047
[sbcl.git] / src / compiler / ir1util.lisp
1 ;;;; This file contains miscellaneous utilities used for manipulating
2 ;;;; the IR1 representation.
3
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.
12
13 (in-package "SB!C")
14 \f
15 ;;;; cleanup hackery
16
17 ;;; Return the innermost cleanup enclosing NODE, or NIL if there is
18 ;;; none in its function. If NODE has no cleanup, but is in a LET,
19 ;;; then we must still check the environment that the call is in.
20 (defun node-enclosing-cleanup (node)
21   (declare (type node node))
22   (do ((lexenv (node-lexenv node)
23                (lambda-call-lexenv (lexenv-lambda lexenv))))
24       ((null lexenv) nil)
25     (let ((cup (lexenv-cleanup lexenv)))
26       (when cup (return cup)))))
27
28 ;;; Convert the FORM in a block inserted between BLOCK1 and BLOCK2 as
29 ;;; an implicit MV-PROG1. The inserted block is returned. NODE is used
30 ;;; for IR1 context when converting the form. Note that the block is
31 ;;; not assigned a number, and is linked into the DFO at the
32 ;;; beginning. We indicate that we have trashed the DFO by setting
33 ;;; COMPONENT-REANALYZE. If CLEANUP is supplied, then convert with
34 ;;; that cleanup.
35 (defun insert-cleanup-code (block1 block2 node form &optional cleanup)
36   (declare (type cblock block1 block2) (type node node)
37            (type (or cleanup null) cleanup))
38   (setf (component-reanalyze (block-component block1)) t)
39   (with-ir1-environment node
40     (let* ((start (make-continuation))
41            (block (continuation-starts-block start))
42            (cont (make-continuation))
43            (*lexenv* (if cleanup
44                          (make-lexenv :cleanup cleanup)
45                          *lexenv*)))
46       (change-block-successor block1 block2 block)
47       (link-blocks block block2)
48       (ir1-convert start cont form)
49       (setf (block-last block) (continuation-use cont))
50       block)))
51 \f
52 ;;;; continuation use hacking
53
54 ;;; Return a list of all the nodes which use Cont.
55 (declaim (ftype (function (continuation) list) find-uses))
56 (defun find-uses (cont)
57   (ecase (continuation-kind cont)
58     ((:block-start :deleted-block-start)
59      (block-start-uses (continuation-block cont)))
60     (:inside-block (list (continuation-use cont)))
61     (:unused nil)
62     (:deleted nil)))
63
64 ;;; Update continuation use information so that NODE is no longer a
65 ;;; use of its CONT. If the old continuation doesn't start its block,
66 ;;; then we don't update the BLOCK-START-USES, since it will be
67 ;;; deleted when we are done.
68 ;;;
69 ;;; Note: if you call this function, you may have to do a
70 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
71 ;;; has changed.
72 (declaim (ftype (function (node) (values)) delete-continuation-use))
73 (defun delete-continuation-use (node)
74   (let* ((cont (node-cont node))
75          (block (continuation-block cont)))
76     (ecase (continuation-kind cont)
77       (:deleted)
78       ((:block-start :deleted-block-start)
79        (let ((uses (delete node (block-start-uses block))))
80          (setf (block-start-uses block) uses)
81          (setf (continuation-use cont)
82                (if (cdr uses) nil (car uses)))))
83       (:inside-block
84        (setf (continuation-kind cont) :unused)
85        (setf (continuation-block cont) nil)
86        (setf (continuation-use cont) nil)
87        (setf (continuation-next cont) nil)))
88     (setf (node-cont node) nil))
89   (values))
90
91 ;;; Update continuation use information so that NODE uses CONT. If
92 ;;; CONT is :UNUSED, then we set its block to NODE's NODE-BLOCK (which
93 ;;; must be set.)
94 ;;;
95 ;;; Note: if you call this function, you may have to do a
96 ;;; REOPTIMIZE-CONTINUATION to inform IR1 optimization that something
97 ;;; has changed.
98 (declaim (ftype (function (node continuation) (values)) add-continuation-use))
99 (defun add-continuation-use (node cont)
100   (aver (not (node-cont node)))
101   (let ((block (continuation-block cont)))
102     (ecase (continuation-kind cont)
103       (:deleted)
104       (:unused
105        (aver (not block))
106        (let ((block (node-block node)))
107          (aver block)
108          (setf (continuation-block cont) block))
109        (setf (continuation-kind cont) :inside-block)
110        (setf (continuation-use cont) node))
111       ((:block-start :deleted-block-start)
112        (let ((uses (cons node (block-start-uses block))))
113          (setf (block-start-uses block) uses)
114          (setf (continuation-use cont)
115                (if (cdr uses) nil (car uses)))))))
116   (setf (node-cont node) cont)
117   (values))
118
119 ;;; Return true if CONT is the NODE-CONT for NODE and CONT is
120 ;;; transferred to immediately after the evaluation of NODE.
121 (defun immediately-used-p (cont node)
122   (declare (type continuation cont) (type node node))
123   (and (eq (node-cont node) cont)
124        (not (eq (continuation-kind cont) :deleted))
125        (let ((cblock (continuation-block cont))
126              (nblock (node-block node)))
127          (or (eq cblock nblock)
128              (let ((succ (block-succ nblock)))
129                (and (= (length succ) 1)
130                     (eq (first succ) cblock)))))))
131 \f
132 ;;;; continuation substitution
133
134 ;;; In OLD's DEST, replace OLD with NEW. NEW's DEST must initially be
135 ;;; NIL. When we are done, we call FLUSH-DEST on OLD to clear its DEST
136 ;;; and to note potential optimization opportunities.
137 (defun substitute-continuation (new old)
138   (declare (type continuation old new))
139   (aver (not (continuation-dest new)))
140   (let ((dest (continuation-dest old)))
141     (etypecase dest
142       ((or ref bind))
143       (cif (setf (if-test dest) new))
144       (cset (setf (set-value dest) new))
145       (creturn (setf (return-result dest) new))
146       (exit (setf (exit-value dest) new))
147       (basic-combination
148        (if (eq old (basic-combination-fun dest))
149            (setf (basic-combination-fun dest) new)
150            (setf (basic-combination-args dest)
151                  (nsubst new old (basic-combination-args dest))))))
152
153     (flush-dest old)
154     (setf (continuation-dest new) dest))
155   (values))
156
157 ;;; Replace all uses of OLD with uses of NEW, where NEW has an
158 ;;; arbitary number of uses. If NEW will end up with more than one
159 ;;; use, then we must arrange for it to start a block if it doesn't
160 ;;; already.
161 (defun substitute-continuation-uses (new old)
162   (declare (type continuation old new))
163   (unless (and (eq (continuation-kind new) :unused)
164                (eq (continuation-kind old) :inside-block))
165     (ensure-block-start new))
166
167   (do-uses (node old)
168     (delete-continuation-use node)
169     (add-continuation-use node new))
170   (dolist (lexenv-use (continuation-lexenv-uses old))
171     (setf (cadr lexenv-use) new))
172
173   (reoptimize-continuation new)
174   (values))
175 \f
176 ;;;; block starting/creation
177
178 ;;; Return the block that CONT is the start of, making a block if
179 ;;; necessary. This function is called by IR1 translators which may
180 ;;; cause a continuation to be used more than once. Every continuation
181 ;;; which may be used more than once must start a block by the time
182 ;;; that anyone does a USE-CONTINUATION on it.
183 ;;;
184 ;;; We also throw the block into the next/prev list for the
185 ;;; *CURRENT-COMPONENT* so that we keep track of which blocks we have
186 ;;; made.
187 (defun continuation-starts-block (cont)
188   (declare (type continuation cont))
189   (ecase (continuation-kind cont)
190     (:unused
191      (aver (not (continuation-block cont)))
192      (let* ((head (component-head *current-component*))
193             (next (block-next head))
194             (new-block (make-block cont)))
195        (setf (block-next new-block) next)
196        (setf (block-prev new-block) head)
197        (setf (block-prev next) new-block)
198        (setf (block-next head) new-block)
199        (setf (continuation-block cont) new-block)
200        (setf (continuation-use cont) nil)
201        (setf (continuation-kind cont) :block-start)
202        new-block))
203     (:block-start
204      (continuation-block cont))))
205
206 ;;; Ensure that Cont is the start of a block (or deleted) so that the use
207 ;;; set can be freely manipulated.
208 ;;; -- If the continuation is :Unused or is :Inside-Block and the Cont of Last
209 ;;;    in its block, then we make it the start of a new deleted block.
210 ;;; -- If the continuation is :Inside-Block inside a block, then we split the
211 ;;;    block using Node-Ends-Block, which makes the continuation be a
212 ;;;    :Block-Start.
213 (defun ensure-block-start (cont)
214   (declare (type continuation cont))
215   (let ((kind (continuation-kind cont)))
216     (ecase kind
217       ((:deleted :block-start :deleted-block-start))
218       ((:unused :inside-block)
219        (let ((block (continuation-block cont)))
220          (cond ((or (eq kind :unused)
221                     (eq (node-cont (block-last block)) cont))
222                 (setf (continuation-block cont)
223                       (make-block-key :start cont
224                                       :component nil
225                                       :start-uses (find-uses cont)))
226                 (setf (continuation-kind cont) :deleted-block-start))
227                (t
228                 (node-ends-block (continuation-use cont))))))))
229   (values))
230 \f
231 ;;;; miscellaneous shorthand functions
232
233 ;;; Return the home (i.e. enclosing non-let) lambda for Node. Since the
234 ;;; LEXENV-LAMBDA may be deleted, we must chain up the LAMBDA-CALL-LEXENV
235 ;;; thread until we find a lambda that isn't deleted, and then return its home.
236 (declaim (maybe-inline node-home-lambda))
237 (defun node-home-lambda (node)
238   (declare (type node node))
239   (do ((fun (lexenv-lambda (node-lexenv node))
240             (lexenv-lambda (lambda-call-lexenv fun))))
241       ((not (eq (functional-kind fun) :deleted))
242        (lambda-home fun))
243     (when (eq (lambda-home fun) fun)
244       (return fun))))
245
246 #!-sb-fluid (declaim (inline node-block node-tlf-number))
247 (declaim (maybe-inline node-environment))
248 (defun node-block (node)
249   (declare (type node node))
250   (the cblock (continuation-block (node-prev node))))
251 (defun node-environment (node)
252   (declare (type node node))
253   #!-sb-fluid (declare (inline node-home-lambda))
254   (the environment (lambda-environment (node-home-lambda node))))
255
256 ;;; Return the enclosing cleanup for environment of the first or last node
257 ;;; in BLOCK.
258 (defun block-start-cleanup (block)
259   (declare (type cblock block))
260   (node-enclosing-cleanup (continuation-next (block-start block))))
261 (defun block-end-cleanup (block)
262   (declare (type cblock block))
263   (node-enclosing-cleanup (block-last block)))
264
265 ;;; Return the non-LET LAMBDA that holds BLOCK's code.
266 (defun block-home-lambda (block)
267   (declare (type cblock block))
268   #!-sb-fluid (declare (inline node-home-lambda))
269   (node-home-lambda (block-last block)))
270
271 ;;; Return the IR1 environment for BLOCK.
272 (defun block-environment (block)
273   (declare (type cblock block))
274   #!-sb-fluid (declare (inline node-home-lambda))
275   (lambda-environment (node-home-lambda (block-last block))))
276
277 ;;; Return the Top Level Form number of PATH, i.e. the ordinal number
278 ;;; of its original source's top-level form in its compilation unit.
279 (defun source-path-tlf-number (path)
280   (declare (list path))
281   (car (last path)))
282
283 ;;; Return the (reversed) list for the PATH in the original source
284 ;;; (with the Top Level Form number last).
285 (defun source-path-original-source (path)
286   (declare (list path) (inline member))
287   (cddr (member 'original-source-start path :test #'eq)))
288
289 ;;; Return the Form Number of PATH's original source inside the Top
290 ;;; Level Form that contains it. This is determined by the order that
291 ;;; we walk the subforms of the top level source form.
292 (defun source-path-form-number (path)
293   (declare (list path) (inline member))
294   (cadr (member 'original-source-start path :test #'eq)))
295
296 ;;; Return a list of all the enclosing forms not in the original
297 ;;; source that converted to get to this form, with the immediate
298 ;;; source for node at the start of the list.
299 (defun source-path-forms (path)
300   (subseq path 0 (position 'original-source-start path)))
301
302 ;;; Return the innermost source form for NODE.
303 (defun node-source-form (node)
304   (declare (type node node))
305   (let* ((path (node-source-path node))
306          (forms (source-path-forms path)))
307     (if forms
308         (first forms)
309         (values (find-original-source path)))))
310
311 ;;; Return NODE-SOURCE-FORM, T if continuation has a single use,
312 ;;; otherwise NIL, NIL.
313 (defun continuation-source (cont)
314   (let ((use (continuation-use cont)))
315     (if use
316         (values (node-source-form use) t)
317         (values nil nil))))
318 \f
319 ;;; Return a new LEXENV just like DEFAULT except for the specified
320 ;;; slot values. Values for the alist slots are NCONCed to the
321 ;;; beginning of the current value, rather than replacing it entirely.
322 (defun make-lexenv (&key (default *lexenv*)
323                          functions variables blocks tags type-restrictions
324                          options
325                          (lambda (lexenv-lambda default))
326                          (cleanup (lexenv-cleanup default))
327                          (policy (lexenv-policy default)))
328   (macrolet ((frob (var slot)
329                `(let ((old (,slot default)))
330                   (if ,var
331                       (nconc ,var old)
332                       old))))
333     (internal-make-lexenv
334      (frob functions lexenv-functions)
335      (frob variables lexenv-variables)
336      (frob blocks lexenv-blocks)
337      (frob tags lexenv-tags)
338      (frob type-restrictions lexenv-type-restrictions)
339      lambda cleanup policy 
340      (frob options lexenv-options))))
341 \f
342 ;;;; flow/DFO/component hackery
343
344 ;;; Join BLOCK1 and BLOCK2.
345 #!-sb-fluid (declaim (inline link-blocks))
346 (defun link-blocks (block1 block2)
347   (declare (type cblock block1 block2))
348   (setf (block-succ block1)
349         (if (block-succ block1)
350             (%link-blocks block1 block2)
351             (list block2)))
352   (push block1 (block-pred block2))
353   (values))
354 (defun %link-blocks (block1 block2)
355   (declare (type cblock block1 block2) (inline member))
356   (let ((succ1 (block-succ block1)))
357     (aver (not (member block2 succ1 :test #'eq)))
358     (cons block2 succ1)))
359
360 ;;; This is like LINK-BLOCKS, but we separate BLOCK1 and BLOCK2. If
361 ;;; this leaves a successor with a single predecessor that ends in an
362 ;;; IF, then set BLOCK-TEST-MODIFIED so that any test constraint will
363 ;;; now be able to be propagated to the successor.
364 (defun unlink-blocks (block1 block2)
365   (declare (type cblock block1 block2))
366   (let ((succ1 (block-succ block1)))
367     (if (eq block2 (car succ1))
368         (setf (block-succ block1) (cdr succ1))
369         (do ((succ (cdr succ1) (cdr succ))
370              (prev succ1 succ))
371             ((eq (car succ) block2)
372              (setf (cdr prev) (cdr succ)))
373           (aver succ))))
374
375   (let ((new-pred (delq block1 (block-pred block2))))
376     (setf (block-pred block2) new-pred)
377     (when (and new-pred (null (rest new-pred)))
378       (let ((pred-block (first new-pred)))
379         (when (if-p (block-last pred-block))
380           (setf (block-test-modified pred-block) t)))))
381   (values))
382
383 ;;; Swing the succ/pred link between BLOCK and OLD to be between BLOCK
384 ;;; and NEW. If BLOCK ends in an IF, then we have to fix up the
385 ;;; consequent/alternative blocks to point to NEW. We also set
386 ;;; BLOCK-TEST-MODIFIED so that any test constraint will be applied to
387 ;;; the new successor.
388 (defun change-block-successor (block old new)
389   (declare (type cblock new old block) (inline member))
390   (unlink-blocks block old)
391   (let ((last (block-last block))
392         (comp (block-component block)))
393     (setf (component-reanalyze comp) t)
394     (typecase last
395       (cif
396        (setf (block-test-modified block) t)
397        (let* ((succ-left (block-succ block))
398               (new (if (and (eq new (component-tail comp))
399                             succ-left)
400                        (first succ-left)
401                        new)))
402          (unless (member new succ-left :test #'eq)
403            (link-blocks block new))
404          (macrolet ((frob (slot)
405                       `(when (eq (,slot last) old)
406                          (setf (,slot last) new))))
407            (frob if-consequent)
408            (frob if-alternative))))
409       (t
410        (unless (member new (block-succ block) :test #'eq)
411          (link-blocks block new)))))
412
413   (values))
414
415 ;;; Unlink a block from the next/prev chain. We also null out the
416 ;;; COMPONENT.
417 (declaim (ftype (function (cblock) (values)) remove-from-dfo))
418 (defun remove-from-dfo (block)
419   (let ((next (block-next block))
420         (prev (block-prev block)))
421     (setf (block-component block) nil)
422     (setf (block-next prev) next)
423     (setf (block-prev next) prev))
424   (values))
425
426 ;;; Add BLOCK to the next/prev chain following AFTER. We also set the
427 ;;; Component to be the same as for AFTER.
428 (defun add-to-dfo (block after)
429   (declare (type cblock block after))
430   (let ((next (block-next after))
431         (comp (block-component after)))
432     (aver (not (eq (component-kind comp) :deleted)))
433     (setf (block-component block) comp)
434     (setf (block-next after) block)
435     (setf (block-prev block) after)
436     (setf (block-next block) next)
437     (setf (block-prev next) block))
438   (values))
439
440 ;;; Set the FLAG for all the blocks in COMPONENT to NIL, except for
441 ;;; the head and tail which are set to T.
442 (declaim (ftype (function (component) (values)) clear-flags))
443 (defun clear-flags (component)
444   (let ((head (component-head component))
445         (tail (component-tail component)))
446     (setf (block-flag head) t)
447     (setf (block-flag tail) t)
448     (do-blocks (block component)
449       (setf (block-flag block) nil)))
450   (values))
451
452 ;;; Make a component with no blocks in it. The BLOCK-FLAG is initially
453 ;;; true in the head and tail blocks.
454 (declaim (ftype (function nil component) make-empty-component))
455 (defun make-empty-component ()
456   (let* ((head (make-block-key :start nil :component nil))
457          (tail (make-block-key :start nil :component nil))
458          (res (make-component :head head :tail tail)))
459     (setf (block-flag head) t)
460     (setf (block-flag tail) t)
461     (setf (block-component head) res)
462     (setf (block-component tail) res)
463     (setf (block-next head) tail)
464     (setf (block-prev tail) head)
465     res))
466
467 ;;; Make NODE the LAST node in its block, splitting the block if necessary.
468 ;;; The new block is added to the DFO immediately following NODE's block.
469 (defun node-ends-block (node)
470   (declare (type node node))
471   (let* ((block (node-block node))
472          (start (node-cont node))
473          (last (block-last block))
474          (last-cont (node-cont last)))
475     (unless (eq last node)
476       (aver (and (eq (continuation-kind start) :inside-block)
477                    (not (block-delete-p block))))
478       (let* ((succ (block-succ block))
479              (new-block
480               (make-block-key :start start
481                               :component (block-component block)
482                               :start-uses (list (continuation-use start))
483                               :succ succ :last last)))
484         (setf (continuation-kind start) :block-start)
485         (dolist (b succ)
486           (setf (block-pred b)
487                 (cons new-block (remove block (block-pred b)))))
488         (setf (block-succ block) ())
489         (setf (block-last block) node)
490         (link-blocks block new-block)
491         (add-to-dfo new-block block)
492         (setf (component-reanalyze (block-component block)) t)
493         
494         (do ((cont start (node-cont (continuation-next cont))))
495             ((eq cont last-cont)
496              (when (eq (continuation-kind last-cont) :inside-block)
497                (setf (continuation-block last-cont) new-block)))
498           (setf (continuation-block cont) new-block))
499
500         (setf (block-type-asserted block) t)
501         (setf (block-test-modified block) t))))
502
503   (values))
504 \f
505 ;;;; deleting stuff
506
507 ;;; Deal with deleting the last (read) reference to a LAMBDA-VAR. We
508 ;;; iterate over all local calls flushing the corresponding argument,
509 ;;; allowing the computation of the argument to be deleted. We also
510 ;;; mark the let for reoptimization, since it may be that we have
511 ;;; deleted the last variable.
512 ;;;
513 ;;; The LAMBDA-VAR may still have some SETs, but this doesn't cause
514 ;;; too much difficulty, since we can efficiently implement write-only
515 ;;; variables. We iterate over the sets, marking their blocks for dead
516 ;;; code flushing, since we can delete sets whose value is unused.
517 (defun delete-lambda-var (leaf)
518   (declare (type lambda-var leaf))
519   (let* ((fun (lambda-var-home leaf))
520          (n (position leaf (lambda-vars fun))))
521     (dolist (ref (leaf-refs fun))
522       (let* ((cont (node-cont ref))
523              (dest (continuation-dest cont)))
524         (when (and (combination-p dest)
525                    (eq (basic-combination-fun dest) cont)
526                    (eq (basic-combination-kind dest) :local))
527           (let* ((args (basic-combination-args dest))
528                  (arg (elt args n)))
529             (reoptimize-continuation arg)
530             (flush-dest arg)
531             (setf (elt args n) nil))))))
532
533   (dolist (set (lambda-var-sets leaf))
534     (setf (block-flush-p (node-block set)) t))
535
536   (values))
537
538 ;;; Note that something interesting has happened to VAR. We only deal
539 ;;; with LET variables, marking the corresponding initial value arg as
540 ;;; needing to be reoptimized.
541 (defun reoptimize-lambda-var (var)
542   (declare (type lambda-var var))
543   (let ((fun (lambda-var-home var)))
544     (when (and (eq (functional-kind fun) :let)
545                (leaf-refs var))
546       (do ((args (basic-combination-args
547                   (continuation-dest
548                    (node-cont
549                     (first (leaf-refs fun)))))
550                  (cdr args))
551            (vars (lambda-vars fun) (cdr vars)))
552           ((eq (car vars) var)
553            (reoptimize-continuation (car args))))))
554   (values))
555
556 ;;; Delete a function that has no references. This need only be called
557 ;;; on functions that never had any references, since otherwise
558 ;;; DELETE-REF will handle the deletion.
559 (defun delete-functional (fun)
560   (aver (and (null (leaf-refs fun))
561              (not (functional-entry-function fun))))
562   (etypecase fun
563     (optional-dispatch (delete-optional-dispatch fun))
564     (clambda (delete-lambda fun)))
565   (values))
566
567 ;;; Deal with deleting the last reference to a LAMBDA. Since there is
568 ;;; only one way into a LAMBDA, deleting the last reference to a
569 ;;; LAMBDA ensures that there is no way to reach any of the code in
570 ;;; it. So we just set the FUNCTIONAL-KIND for FUN and its LETs to
571 ;;; :DELETED, causing IR1 optimization to delete blocks in that
572 ;;; lambda.
573 ;;;
574 ;;; If the function isn't a LET, we unlink the function head and tail
575 ;;; from the component head and tail to indicate that the code is
576 ;;; unreachable. We also delete the function from COMPONENT-LAMBDAS
577 ;;; (it won't be there before local call analysis, but no matter.) If
578 ;;; the lambda was never referenced, we give a note.
579 ;;;
580 ;;; If the lambda is an XEP, then we null out the ENTRY-FUNCTION in its
581 ;;; ENTRY-FUNCTION so that people will know that it is not an entry point
582 ;;; anymore.
583 (defun delete-lambda (leaf)
584   (declare (type clambda leaf))
585   (let ((kind (functional-kind leaf))
586         (bind (lambda-bind leaf)))
587     (aver (not (member kind '(:deleted :optional :top-level))))
588     (aver (not (functional-has-external-references-p leaf)))
589     (setf (functional-kind leaf) :deleted)
590     (setf (lambda-bind leaf) nil)
591     (dolist (let (lambda-lets leaf))
592       (setf (lambda-bind let) nil)
593       (setf (functional-kind let) :deleted))
594
595     (if (member kind '(:let :mv-let :assignment))
596         (let ((home (lambda-home leaf)))
597           (setf (lambda-lets home) (delete leaf (lambda-lets home))))
598         (let* ((bind-block (node-block bind))
599                (component (block-component bind-block))
600                (return (lambda-return leaf)))
601           (aver (null (leaf-refs leaf)))
602           (unless (leaf-ever-used leaf)
603             (let ((*compiler-error-context* bind))
604               (compiler-note "deleting unused function~:[.~;~:*~%  ~S~]"
605                              (leaf-name leaf))))
606           (unlink-blocks (component-head component) bind-block)
607           (when return
608             (unlink-blocks (node-block return) (component-tail component)))
609           (setf (component-reanalyze component) t)
610           (let ((tails (lambda-tail-set leaf)))
611             (setf (tail-set-functions tails)
612                   (delete leaf (tail-set-functions tails)))
613             (setf (lambda-tail-set leaf) nil))
614           (setf (component-lambdas component)
615                 (delete leaf (component-lambdas component)))))
616
617     (when (eq kind :external)
618       (let ((fun (functional-entry-function leaf)))
619         (setf (functional-entry-function fun) nil)
620         (when (optional-dispatch-p fun)
621           (delete-optional-dispatch fun)))))
622
623   (values))
624
625 ;;; Deal with deleting the last reference to an OPTIONAL-DISPATCH. We
626 ;;; have to be a bit more careful than with lambdas, since DELETE-REF
627 ;;; is used both before and after local call analysis. Afterward, all
628 ;;; references to still-existing OPTIONAL-DISPATCHes have been moved
629 ;;; to the XEP, leaving it with no references at all. So we look at
630 ;;; the XEP to see whether an optional-dispatch is still really being
631 ;;; used. But before local call analysis, there are no XEPs, and all
632 ;;; references are direct.
633 ;;;
634 ;;; When we do delete the OPTIONAL-DISPATCH, we grovel all of its
635 ;;; entry-points, making them be normal lambdas, and then deleting the
636 ;;; ones with no references. This deletes any e-p lambdas that were
637 ;;; either never referenced, or couldn't be deleted when the last
638 ;;; deference was deleted (due to their :OPTIONAL kind.)
639 ;;;
640 ;;; Note that the last optional ep may alias the main entry, so when
641 ;;; we process the main entry, its kind may have been changed to NIL
642 ;;; or even converted to a let.
643 (defun delete-optional-dispatch (leaf)
644   (declare (type optional-dispatch leaf))
645   (let ((entry (functional-entry-function leaf)))
646     (unless (and entry (leaf-refs entry))
647       (aver (or (not entry) (eq (functional-kind entry) :deleted)))
648       (setf (functional-kind leaf) :deleted)
649
650       (flet ((frob (fun)
651                (unless (eq (functional-kind fun) :deleted)
652                  (aver (eq (functional-kind fun) :optional))
653                  (setf (functional-kind fun) nil)
654                  (let ((refs (leaf-refs fun)))
655                    (cond ((null refs)
656                           (delete-lambda fun))
657                          ((null (rest refs))
658                           (or (maybe-let-convert fun)
659                               (maybe-convert-to-assignment fun)))
660                          (t
661                           (maybe-convert-to-assignment fun)))))))
662         
663         (dolist (ep (optional-dispatch-entry-points leaf))
664           (frob ep))
665         (when (optional-dispatch-more-entry leaf)
666           (frob (optional-dispatch-more-entry leaf)))
667         (let ((main (optional-dispatch-main-entry leaf)))
668           (when (eq (functional-kind main) :optional)
669             (frob main))))))
670
671   (values))
672
673 ;;; Do stuff to delete the semantic attachments of a REF node. When
674 ;;; this leaves zero or one reference, we do a type dispatch off of
675 ;;; the leaf to determine if a special action is appropriate.
676 (defun delete-ref (ref)
677   (declare (type ref ref))
678   (let* ((leaf (ref-leaf ref))
679          (refs (delete ref (leaf-refs leaf))))
680     (setf (leaf-refs leaf) refs)
681
682     (cond ((null refs)
683            (typecase leaf
684              (lambda-var (delete-lambda-var leaf))
685              (clambda
686               (ecase (functional-kind leaf)
687                 ((nil :let :mv-let :assignment :escape :cleanup)
688                  (aver (not (functional-entry-function leaf)))
689                  (delete-lambda leaf))
690                 (:external
691                  (delete-lambda leaf))
692                 ((:deleted :optional))))
693              (optional-dispatch
694               (unless (eq (functional-kind leaf) :deleted)
695                 (delete-optional-dispatch leaf)))))
696           ((null (rest refs))
697            (typecase leaf
698              (clambda (or (maybe-let-convert leaf)
699                           (maybe-convert-to-assignment leaf)))
700              (lambda-var (reoptimize-lambda-var leaf))))
701           (t
702            (typecase leaf
703              (clambda (maybe-convert-to-assignment leaf))))))
704
705   (values))
706
707 ;;; This function is called by people who delete nodes; it provides a
708 ;;; way to indicate that the value of a continuation is no longer
709 ;;; used. We null out the CONTINUATION-DEST, set FLUSH-P in the blocks
710 ;;; containing uses of CONT and set COMPONENT-REOPTIMIZE. If the PREV
711 ;;; of the use is deleted, then we blow off reoptimization.
712 ;;;
713 ;;; If the continuation is :Deleted, then we don't do anything, since
714 ;;; all semantics have already been flushed. :DELETED-BLOCK-START
715 ;;; start continuations are treated just like :BLOCK-START; it is
716 ;;; possible that the continuation may be given a new dest (e.g. by
717 ;;; SUBSTITUTE-CONTINUATION), so we don't want to delete it.
718 (defun flush-dest (cont)
719   (declare (type continuation cont))
720
721   (unless (eq (continuation-kind cont) :deleted)
722     (aver (continuation-dest cont))
723     (setf (continuation-dest cont) nil)
724     (do-uses (use cont)
725       (let ((prev (node-prev use)))
726         (unless (eq (continuation-kind prev) :deleted)
727           (let ((block (continuation-block prev)))
728             (setf (component-reoptimize (block-component block)) t)
729             (setf (block-attributep (block-flags block) flush-p type-asserted)
730                   t))))))
731
732   (setf (continuation-%type-check cont) nil)
733
734   (values))
735
736 ;;; Do a graph walk backward from BLOCK, marking all predecessor
737 ;;; blocks with the DELETE-P flag.
738 (defun mark-for-deletion (block)
739   (declare (type cblock block))
740   (unless (block-delete-p block)
741     (setf (block-delete-p block) t)
742     (setf (component-reanalyze (block-component block)) t)
743     (dolist (pred (block-pred block))
744       (mark-for-deletion pred)))
745   (values))
746
747 ;;; Delete CONT, eliminating both control and value semantics. We set
748 ;;; FLUSH-P and COMPONENT-REOPTIMIZE similarly to in FLUSH-DEST. Here
749 ;;; we must get the component from the use block, since the
750 ;;; continuation may be a :DELETED-BLOCK-START.
751 ;;;
752 ;;; If CONT has DEST, then it must be the case that the DEST is
753 ;;; unreachable, since we can't compute the value desired. In this
754 ;;; case, we call MARK-FOR-DELETION to cause the DEST block and its
755 ;;; predecessors to tell people to ignore them, and to cause them to
756 ;;; be deleted eventually.
757 (defun delete-continuation (cont)
758   (declare (type continuation cont))
759   (aver (not (eq (continuation-kind cont) :deleted)))
760
761   (do-uses (use cont)
762     (let ((prev (node-prev use)))
763       (unless (eq (continuation-kind prev) :deleted)
764         (let ((block (continuation-block prev)))
765           (setf (block-attributep (block-flags block) flush-p type-asserted) t)
766           (setf (component-reoptimize (block-component block)) t)))))
767
768   (let ((dest (continuation-dest cont)))
769     (when dest
770       (let ((prev (node-prev dest)))
771         (when (and prev
772                    (not (eq (continuation-kind prev) :deleted)))
773           (let ((block (continuation-block prev)))
774             (unless (block-delete-p block)
775               (mark-for-deletion block)))))))
776
777   (setf (continuation-kind cont) :deleted)
778   (setf (continuation-dest cont) nil)
779   (setf (continuation-next cont) nil)
780   (setf (continuation-asserted-type cont) *empty-type*)
781   (setf (continuation-%derived-type cont) *empty-type*)
782   (setf (continuation-use cont) nil)
783   (setf (continuation-block cont) nil)
784   (setf (continuation-reoptimize cont) nil)
785   (setf (continuation-%type-check cont) nil)
786   (setf (continuation-info cont) nil)
787
788   (values))
789
790 ;;; This function does what is necessary to eliminate the code in it
791 ;;; from the IR1 representation. This involves unlinking it from its
792 ;;; predecessors and successors and deleting various node-specific
793 ;;; semantic information.
794 ;;;
795 ;;; We mark the START as has having no next and remove the last node
796 ;;; from its CONT's uses. We also flush the DEST for all continuations
797 ;;; whose values are received by nodes in the block.
798 (defun delete-block (block)
799   (declare (type cblock block))
800   (aver (block-component block)) ; else block is already deleted!
801   (note-block-deletion block)
802   (setf (block-delete-p block) t)
803
804   (let* ((last (block-last block))
805          (cont (node-cont last)))
806     (delete-continuation-use last)
807     (if (eq (continuation-kind cont) :unused)
808         (delete-continuation cont)
809         (reoptimize-continuation cont)))
810
811   (dolist (b (block-pred block))
812     (unlink-blocks b block))
813   (dolist (b (block-succ block))
814     (unlink-blocks block b))
815
816   (do-nodes (node cont block)
817     (typecase node
818       (ref (delete-ref node))
819       (cif
820        (flush-dest (if-test node)))
821       ;; The next two cases serve to maintain the invariant that a LET
822       ;; always has a well-formed COMBINATION, REF and BIND. We delete
823       ;; the lambda whenever we delete any of these, but we must be
824       ;; careful that this LET has not already been partially deleted.
825       (basic-combination
826        (when (and (eq (basic-combination-kind node) :local)
827                   ;; Guards COMBINATION-LAMBDA agains the REF being deleted.
828                   (continuation-use (basic-combination-fun node)))
829          (let ((fun (combination-lambda node)))
830            ;; If our REF was the 2'nd to last ref, and has been deleted, then
831            ;; Fun may be a LET for some other combination.
832            (when (and (member (functional-kind fun) '(:let :mv-let))
833                       (eq (let-combination fun) node))
834              (delete-lambda fun))))
835        (flush-dest (basic-combination-fun node))
836        (dolist (arg (basic-combination-args node))
837          (when arg (flush-dest arg))))
838       (bind
839        (let ((lambda (bind-lambda node)))
840          (unless (eq (functional-kind lambda) :deleted)
841            (aver (member (functional-kind lambda) '(:let :mv-let :assignment)))
842            (delete-lambda lambda))))
843       (exit
844        (let ((value (exit-value node))
845              (entry (exit-entry node)))
846          (when value
847            (flush-dest value))
848          (when entry
849            (setf (entry-exits entry)
850                  (delete node (entry-exits entry))))))
851       (creturn
852        (flush-dest (return-result node))
853        (delete-return node))
854       (cset
855        (flush-dest (set-value node))
856        (let ((var (set-var node)))
857          (setf (basic-var-sets var)
858                (delete node (basic-var-sets var))))))
859
860     (delete-continuation (node-prev node)))
861
862   (remove-from-dfo block)
863   (values))
864
865 ;;; Do stuff to indicate that the return node Node is being deleted.
866 ;;; We set the RETURN to NIL.
867 (defun delete-return (node)
868   (declare (type creturn node))
869   (let ((fun (return-lambda node)))
870     (aver (lambda-return fun))
871     (setf (lambda-return fun) nil))
872   (values))
873
874 ;;; If any of the VARS in FUN was never referenced and was not
875 ;;; declared IGNORE, then complain.
876 (defun note-unreferenced-vars (fun)
877   (declare (type clambda fun))
878   (dolist (var (lambda-vars fun))
879     (unless (or (leaf-ever-used var)
880                 (lambda-var-ignorep var))
881       (let ((*compiler-error-context* (lambda-bind fun)))
882         (unless (policy *compiler-error-context* (= inhibit-warnings 3))
883           ;; ANSI section "3.2.5 Exceptional Situations in the Compiler"
884           ;; requires this to be a STYLE-WARNING.
885           (compiler-style-warning "The variable ~S is defined but never used."
886                                   (leaf-name var)))
887         (setf (leaf-ever-used var) t))))
888   (values))
889
890 (defvar *deletion-ignored-objects* '(t nil))
891
892 ;;; Return true if we can find OBJ in FORM, NIL otherwise. We bound
893 ;;; our recursion so that we don't get lost in circular structures. We
894 ;;; ignore the car of forms if they are a symbol (to prevent confusing
895 ;;; function referencess with variables), and we also ignore anything
896 ;;; inside ' or #'.
897 (defun present-in-form (obj form depth)
898   (declare (type (integer 0 20) depth))
899   (cond ((= depth 20) nil)
900         ((eq obj form) t)
901         ((atom form) nil)
902         (t
903          (let ((first (car form))
904                (depth (1+ depth)))
905            (if (member first '(quote function))
906                nil
907                (or (and (not (symbolp first))
908                         (present-in-form obj first depth))
909                    (do ((l (cdr form) (cdr l))
910                         (n 0 (1+ n)))
911                        ((or (atom l) (> n 100))
912                         nil)
913                      (declare (fixnum n))
914                      (when (present-in-form obj (car l) depth)
915                        (return t)))))))))
916
917 ;;; This function is called on a block immediately before we delete
918 ;;; it. We check to see whether any of the code about to die appeared
919 ;;; in the original source, and emit a note if so.
920 ;;;
921 ;;; If the block was in a lambda is now deleted, then we ignore the
922 ;;; whole block, since this case is picked off in DELETE-LAMBDA. We
923 ;;; also ignore the deletion of CRETURN nodes, since it is somewhat
924 ;;; reasonable for a function to not return, and there is a different
925 ;;; note for that case anyway.
926 ;;;
927 ;;; If the actual source is an atom, then we use a bunch of heuristics
928 ;;; to guess whether this reference really appeared in the original
929 ;;; source:
930 ;;; -- If a symbol, it must be interned and not a keyword.
931 ;;; -- It must not be an easily introduced constant (T or NIL, a fixnum
932 ;;;    or a character.)
933 ;;; -- The atom must be "present" in the original source form, and
934 ;;;    present in all intervening actual source forms.
935 (defun note-block-deletion (block)
936   (let ((home (block-home-lambda block)))
937     (unless (eq (functional-kind home) :deleted)
938       (do-nodes (node cont block)
939         (let* ((path (node-source-path node))
940                (first (first path)))
941           (when (or (eq first 'original-source-start)
942                     (and (atom first)
943                          (or (not (symbolp first))
944                              (let ((pkg (symbol-package first)))
945                                (and pkg
946                                     (not (eq pkg (symbol-package :end))))))
947                          (not (member first *deletion-ignored-objects*))
948                          (not (typep first '(or fixnum character)))
949                          (every #'(lambda (x)
950                                     (present-in-form first x 0))
951                                 (source-path-forms path))
952                          (present-in-form first (find-original-source path)
953                                           0)))
954             (unless (return-p node)
955               (let ((*compiler-error-context* node))
956                 (compiler-note "deleting unreachable code")))
957             (return))))))
958   (values))
959
960 ;;; Delete a node from a block, deleting the block if there are no
961 ;;; nodes left. We remove the node from the uses of its CONT, but we
962 ;;; don't deal with cleaning up any type-specific semantic
963 ;;; attachments. If the CONT is :UNUSED after deleting this use, then
964 ;;; we delete CONT. (Note :UNUSED is not the same as no uses. A
965 ;;; continuation will only become :UNUSED if it was :INSIDE-BLOCK
966 ;;; before.)
967 ;;;
968 ;;; If the node is the last node, there must be exactly one successor.
969 ;;; We link all of our precedessors to the successor and unlink the
970 ;;; block. In this case, we return T, otherwise NIL. If no nodes are
971 ;;; left, and the block is a successor of itself, then we replace the
972 ;;; only node with a degenerate exit node. This provides a way to
973 ;;; represent the bodyless infinite loop, given the prohibition on
974 ;;; empty blocks in IR1.
975 (defun unlink-node (node)
976   (declare (type node node))
977   (let* ((cont (node-cont node))
978          (next (continuation-next cont))
979          (prev (node-prev node))
980          (block (continuation-block prev))
981          (prev-kind (continuation-kind prev))
982          (last (block-last block)))
983
984     (unless (eq (continuation-kind cont) :deleted)
985       (delete-continuation-use node)
986       (when (eq (continuation-kind cont) :unused)
987         (aver (not (continuation-dest cont)))
988         (delete-continuation cont)))
989
990     (setf (block-type-asserted block) t)
991     (setf (block-test-modified block) t)
992
993     (cond ((or (eq prev-kind :inside-block)
994                (and (eq prev-kind :block-start)
995                     (not (eq node last))))
996            (cond ((eq node last)
997                   (setf (block-last block) (continuation-use prev))
998                   (setf (continuation-next prev) nil))
999                  (t
1000                   (setf (continuation-next prev) next)
1001                   (setf (node-prev next) prev)))
1002            (setf (node-prev node) nil)
1003            nil)
1004           (t
1005            (aver (eq prev-kind :block-start))
1006            (aver (eq node last))
1007            (let* ((succ (block-succ block))
1008                   (next (first succ)))
1009              (aver (and succ (null (cdr succ))))
1010              (cond
1011               ((member block succ)
1012                (with-ir1-environment node
1013                  (let ((exit (make-exit))
1014                        (dummy (make-continuation)))
1015                    (setf (continuation-next prev) nil)
1016                    (prev-link exit prev)
1017                    (add-continuation-use exit dummy)
1018                    (setf (block-last block) exit)))
1019                (setf (node-prev node) nil)
1020                nil)
1021               (t
1022                (aver (eq (block-start-cleanup block)
1023                          (block-end-cleanup block)))
1024                (unlink-blocks block next)
1025                (dolist (pred (block-pred block))
1026                  (change-block-successor pred block next))
1027                (remove-from-dfo block)
1028                (cond ((continuation-dest prev)
1029                       (setf (continuation-next prev) nil)
1030                       (setf (continuation-kind prev) :deleted-block-start))
1031                      (t
1032                       (delete-continuation prev)))
1033                (setf (node-prev node) nil)
1034                t)))))))
1035
1036 ;;; Return true if NODE has been deleted, false if it is still a valid
1037 ;;; part of IR1.
1038 (defun node-deleted (node)
1039   (declare (type node node))
1040   (let ((prev (node-prev node)))
1041     (not (and prev
1042               (not (eq (continuation-kind prev) :deleted))
1043               (let ((block (continuation-block prev)))
1044                 (and (block-component block)
1045                      (not (block-delete-p block))))))))
1046
1047 ;;; Delete all the blocks and functions in COMPONENT. We scan first
1048 ;;; marking the blocks as delete-p to prevent weird stuff from being
1049 ;;; triggered by deletion.
1050 (defun delete-component (component)
1051   (declare (type component component))
1052   (aver (null (component-new-functions component)))
1053   (setf (component-kind component) :deleted)
1054   (do-blocks (block component)
1055     (setf (block-delete-p block) t))
1056   (dolist (fun (component-lambdas component))
1057     (setf (functional-kind fun) nil)
1058     (setf (functional-entry-function fun) nil)
1059     (setf (leaf-refs fun) nil)
1060     (delete-functional fun))
1061   (do-blocks (block component)
1062     (delete-block block))
1063   (values))
1064
1065 ;;; Convert code of the form
1066 ;;;   (FOO ... (FUN ...) ...)
1067 ;;; to
1068 ;;;   (FOO ...    ...    ...).
1069 ;;; In other words, replace the function combination FUN by its
1070 ;;; arguments. If there are any problems with doing this, use GIVE-UP
1071 ;;; to blow out of whatever transform called this. Note, as the number
1072 ;;; of arguments changes, the transform must be prepared to return a
1073 ;;; lambda with a new lambda-list with the correct number of
1074 ;;; arguments.
1075 (defun extract-function-args (cont fun num-args)
1076   #!+sb-doc
1077   "If CONT is a call to FUN with NUM-ARGS args, change those arguments
1078    to feed directly to the continuation-dest of CONT, which must be
1079    a combination."
1080   (declare (type continuation cont)
1081            (type symbol fun)
1082            (type index num-args))
1083   (let ((outside (continuation-dest cont))
1084         (inside (continuation-use cont)))
1085     (aver (combination-p outside))
1086     (unless (combination-p inside)
1087       (give-up-ir1-transform))
1088     (let ((inside-fun (combination-fun inside)))
1089       (unless (eq (continuation-function-name inside-fun) fun)
1090         (give-up-ir1-transform))
1091       (let ((inside-args (combination-args inside)))
1092         (unless (= (length inside-args) num-args)
1093           (give-up-ir1-transform))
1094         (let* ((outside-args (combination-args outside))
1095                (arg-position (position cont outside-args))
1096                (before-args (subseq outside-args 0 arg-position))
1097                (after-args (subseq outside-args (1+ arg-position))))
1098           (dolist (arg inside-args)
1099             (setf (continuation-dest arg) outside))
1100           (setf (combination-args inside) nil)
1101           (setf (combination-args outside)
1102                 (append before-args inside-args after-args))
1103           (change-ref-leaf (continuation-use inside-fun)
1104                            (find-free-function 'list "???"))
1105           (setf (combination-kind inside) :full)
1106           (setf (node-derived-type inside) *wild-type*)
1107           (flush-dest cont)
1108           (setf (continuation-asserted-type cont) *wild-type*)
1109           (values))))))
1110 \f
1111 ;;;; leaf hackery
1112
1113 ;;; Change the Leaf that a Ref refers to.
1114 (defun change-ref-leaf (ref leaf)
1115   (declare (type ref ref) (type leaf leaf))
1116   (unless (eq (ref-leaf ref) leaf)
1117     (push ref (leaf-refs leaf))
1118     (delete-ref ref)
1119     (setf (ref-leaf ref) leaf)
1120     (let ((ltype (leaf-type leaf)))
1121       (if (function-type-p ltype)
1122           (setf (node-derived-type ref) ltype)
1123           (derive-node-type ref ltype)))
1124     (reoptimize-continuation (node-cont ref)))
1125   (values))
1126
1127 ;;; Change all REFS for OLD-LEAF to NEW-LEAF.
1128 (defun substitute-leaf (new-leaf old-leaf)
1129   (declare (type leaf new-leaf old-leaf))
1130   (dolist (ref (leaf-refs old-leaf))
1131     (change-ref-leaf ref new-leaf))
1132   (values))
1133
1134 ;;; Like SUBSITUTE-LEAF, only there is a predicate on the Ref to tell
1135 ;;; whether to substitute.
1136 (defun substitute-leaf-if (test new-leaf old-leaf)
1137   (declare (type leaf new-leaf old-leaf) (type function test))
1138   (dolist (ref (leaf-refs old-leaf))
1139     (when (funcall test ref)
1140       (change-ref-leaf ref new-leaf)))
1141   (values))
1142
1143 ;;; Return a LEAF which represents the specified constant object. If
1144 ;;; the object is not in *CONSTANTS*, then we create a new constant
1145 ;;; LEAF and enter it.
1146 #!-sb-fluid (declaim (maybe-inline find-constant))
1147 (defun find-constant (object)
1148   (if (typep object '(or symbol number character instance))
1149     (or (gethash object *constants*)
1150         (setf (gethash object *constants*)
1151               (make-constant :value object
1152                              :name nil
1153                              :type (ctype-of object)
1154                              :where-from :defined)))
1155     (make-constant :value object
1156                    :name nil
1157                    :type (ctype-of object)
1158                    :where-from :defined)))
1159 \f
1160 ;;; If there is a non-local exit noted in ENTRY's environment that
1161 ;;; exits to CONT in that entry, then return it, otherwise return NIL.
1162 (defun find-nlx-info (entry cont)
1163   (declare (type entry entry) (type continuation cont))
1164   (let ((entry-cleanup (entry-cleanup entry)))
1165     (dolist (nlx (environment-nlx-info (node-environment entry)) nil)
1166       (when (and (eq (nlx-info-continuation nlx) cont)
1167                  (eq (nlx-info-cleanup nlx) entry-cleanup))
1168         (return nlx)))))
1169 \f
1170 ;;;; functional hackery
1171
1172 (declaim (ftype (function (functional) clambda) main-entry))
1173 (defun main-entry (functional)
1174   (etypecase functional
1175     (clambda functional)
1176     (optional-dispatch
1177      (optional-dispatch-main-entry functional))))
1178
1179 ;;; RETURN true if FUNCTIONAL is a thing that can be treated like
1180 ;;; MV-BIND when it appears in an MV-CALL. All fixed arguments must be
1181 ;;; optional with null default and no SUPPLIED-P. There must be a
1182 ;;; &REST arg with no references.
1183 (declaim (ftype (function (functional) boolean) looks-like-an-mv-bind))
1184 (defun looks-like-an-mv-bind (functional)
1185   (and (optional-dispatch-p functional)
1186        (do ((arg (optional-dispatch-arglist functional) (cdr arg)))
1187            ((null arg) nil)
1188          (let ((info (lambda-var-arg-info (car arg))))
1189            (unless info (return nil))
1190            (case (arg-info-kind info)
1191              (:optional
1192               (when (or (arg-info-supplied-p info) (arg-info-default info))
1193                 (return nil)))
1194              (:rest
1195               (return (and (null (cdr arg)) (null (leaf-refs (car arg))))))
1196              (t
1197               (return nil)))))))
1198
1199 ;;; Return true if function is an XEP. This is true of normal XEPs
1200 ;;; (:EXTERNAL kind) and top-level lambdas (:TOP-LEVEL kind.)
1201 (defun external-entry-point-p (fun)
1202   (declare (type functional fun))
1203   (not (null (member (functional-kind fun) '(:external :top-level)))))
1204
1205 ;;; If CONT's only use is a non-notinline global function reference,
1206 ;;; then return the referenced symbol, otherwise NIL. If NOTINLINE-OK
1207 ;;; is true, then we don't care if the leaf is NOTINLINE.
1208 (defun continuation-function-name (cont &optional notinline-ok)
1209   (declare (type continuation cont))
1210   (let ((use (continuation-use cont)))
1211     (if (ref-p use)
1212         (let ((leaf (ref-leaf use)))
1213           (if (and (global-var-p leaf)
1214                    (eq (global-var-kind leaf) :global-function)
1215                    (or (not (defined-function-p leaf))
1216                        (not (eq (defined-function-inlinep leaf) :notinline))
1217                        notinline-ok))
1218               (leaf-name leaf)
1219               nil))
1220         nil)))
1221
1222 ;;; Return the COMBINATION node that is the call to the LET FUN.
1223 (defun let-combination (fun)
1224   (declare (type clambda fun))
1225   (aver (member (functional-kind fun) '(:let :mv-let)))
1226   (continuation-dest (node-cont (first (leaf-refs fun)))))
1227
1228 ;;; Return the initial value continuation for a LET variable, or NIL
1229 ;;; if there is none.
1230 (defun let-var-initial-value (var)
1231   (declare (type lambda-var var))
1232   (let ((fun (lambda-var-home var)))
1233     (elt (combination-args (let-combination fun))
1234          (position-or-lose var (lambda-vars fun)))))
1235
1236 ;;; Return the LAMBDA that is called by the local Call.
1237 #!-sb-fluid (declaim (inline combination-lambda))
1238 (defun combination-lambda (call)
1239   (declare (type basic-combination call))
1240   (aver (eq (basic-combination-kind call) :local))
1241   (ref-leaf (continuation-use (basic-combination-fun call))))
1242
1243 (defvar *inline-expansion-limit* 200
1244   #!+sb-doc
1245   "an upper limit on the number of inline function calls that will be expanded
1246    in any given code object (single function or block compilation)")
1247
1248 ;;; Check whether NODE's component has exceeded its inline expansion
1249 ;;; limit, and warn if so, returning NIL.
1250 (defun inline-expansion-ok (node)
1251   (let ((expanded (incf (component-inline-expansions
1252                          (block-component
1253                           (node-block node))))))
1254     (cond ((> expanded *inline-expansion-limit*) nil)
1255           ((= expanded *inline-expansion-limit*)
1256            ;; FIXME: If the objective is to stop the recursive
1257            ;; expansion of inline functions, wouldn't it be more
1258            ;; correct to look back through surrounding expansions
1259            ;; (which are, I think, stored in the *CURRENT-PATH*, and
1260            ;; possibly stored elsewhere too) and suppress expansion
1261            ;; and print this warning when the function being proposed
1262            ;; for inline expansion is found there? (I don't like the
1263            ;; arbitrary numerical limit in principle, and I think
1264            ;; it'll be a nuisance in practice if we ever want the
1265            ;; compiler to be able to use WITH-COMPILATION-UNIT on
1266            ;; arbitrarily huge blocks of code. -- WHN)
1267            (let ((*compiler-error-context* node))
1268              (compiler-note "*INLINE-EXPANSION-LIMIT* (~D) was exceeded, ~
1269                              probably trying to~%  ~
1270                              inline a recursive function."
1271                             *inline-expansion-limit*))
1272            nil)
1273           (t t))))
1274 \f
1275 ;;;; careful call
1276
1277 ;;; Apply a function to some arguments, returning a list of the values
1278 ;;; resulting of the evaluation. If an error is signalled during the
1279 ;;; application, then we print a warning message and return NIL as our
1280 ;;; second value to indicate this. Node is used as the error context
1281 ;;; for any error message, and Context is a string that is spliced
1282 ;;; into the warning.
1283 (declaim (ftype (function ((or symbol function) list node string)
1284                           (values list boolean))
1285                 careful-call))
1286 (defun careful-call (function args node context)
1287   (values
1288    (multiple-value-list
1289     (handler-case (apply function args)
1290       (error (condition)
1291         (let ((*compiler-error-context* node))
1292           (compiler-warning "Lisp error during ~A:~%~A" context condition)
1293           (return-from careful-call (values nil nil))))))
1294    t))
1295 \f
1296 ;;;; utilities used at run-time for parsing &KEY args in IR1
1297
1298 ;;; This function is used by the result of PARSE-DEFTRANSFORM to find
1299 ;;; the continuation for the value of the &KEY argument KEY in the
1300 ;;; list of continuations ARGS. It returns the continuation if the
1301 ;;; keyword is present, or NIL otherwise. The legality and
1302 ;;; constantness of the keywords should already have been checked.
1303 (declaim (ftype (function (list keyword) (or continuation null))
1304                 find-keyword-continuation))
1305 (defun find-keyword-continuation (args key)
1306   (do ((arg args (cddr arg)))
1307       ((null arg) nil)
1308     (when (eq (continuation-value (first arg)) key)
1309       (return (second arg)))))
1310
1311 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1312 ;;; verify that alternating continuations in ARGS are constant and
1313 ;;; that there is an even number of args.
1314 (declaim (ftype (function (list) boolean) check-key-args-constant))
1315 (defun check-key-args-constant (args)
1316   (do ((arg args (cddr arg)))
1317       ((null arg) t)
1318     (unless (and (rest arg)
1319                  (constant-continuation-p (first arg)))
1320       (return nil))))
1321
1322 ;;; This function is used by the result of PARSE-DEFTRANSFORM to
1323 ;;; verify that the list of continuations ARGS is a well-formed &KEY
1324 ;;; arglist and that only keywords present in the list KEYS are
1325 ;;; supplied.
1326 (declaim (ftype (function (list list) boolean) check-transform-keys))
1327 (defun check-transform-keys (args keys)
1328   (and (check-key-args-constant args)
1329        (do ((arg args (cddr arg)))
1330            ((null arg) t)
1331          (unless (member (continuation-value (first arg)) keys)
1332            (return nil)))))
1333 \f
1334 ;;;; miscellaneous
1335
1336 ;;; Called by the expansion of the EVENT macro.
1337 (declaim (ftype (function (event-info (or node null)) *) %event))
1338 (defun %event (info node)
1339   (incf (event-info-count info))
1340   (when (and (>= (event-info-level info) *event-note-threshold*)
1341              (policy (or node *lexenv*)
1342                      (= inhibit-warnings 0)))
1343     (let ((*compiler-error-context* node))
1344       (compiler-note (event-info-description info))))
1345
1346   (let ((action (event-info-action info)))
1347     (when action (funcall action node))))