0d16dbfb6907213441feebc25b85bc443e6a7abe
[sbcl.git] / src / compiler / physenvanal.lisp
1 ;;;; This file implements the environment analysis phase for the
2 ;;;; compiler. This phase annotates IR1 with a hierarchy environment
3 ;;;; structures, determining the physical environment that each LAMBDA
4 ;;;; allocates its variables and finding what values are closed over
5 ;;;; by each physical environment.
6
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
15
16 (in-package "SB!C")
17
18 ;;; Do environment analysis on the code in COMPONENT. This involves
19 ;;; various things:
20 ;;;  1. Make a PHYSENV structure for each non-LET LAMBDA, assigning 
21 ;;;     the LAMBDA-PHYSENV for all LAMBDAs.
22 ;;;  2. Find all values that need to be closed over by each
23 ;;;     physical environment.
24 ;;;  3. Scan the blocks in the component closing over non-local-exit
25 ;;;     continuations.
26 ;;;  4. Delete all non-top-level functions with no references. This
27 ;;;     should only get functions with non-NULL kinds, since normal
28 ;;;     functions are deleted when their references go to zero. 
29 (defun physenv-analyze (component)
30   (declare (type component component))
31   (aver (every (lambda (x)
32                  (eq (functional-kind x) :deleted))
33                (component-new-functions component)))
34   (setf (component-new-functions component) ())
35   (dolist (fun (component-lambdas component))
36     (reinit-lambda-physenv fun))
37   (dolist (fun (component-lambdas component))
38     (compute-closure fun)
39     (dolist (let (lambda-lets fun))
40       (compute-closure let)))
41
42   (find-non-local-exits component)
43   (find-cleanup-points component)
44   (tail-annotate component)
45
46   (dolist (fun (component-lambdas component))
47     (when (null (leaf-refs fun))
48       (let ((kind (functional-kind fun)))
49         (unless (or (eq kind :toplevel)
50                     (functional-has-external-references-p fun))
51           (aver (member kind '(:optional :cleanup :escape)))
52           (setf (functional-kind fun) nil)
53           (delete-functional fun)))))
54
55   (values))
56
57 ;;; This is to be called on a COMPONENT with top level LAMBDAs before
58 ;;; the compilation of the associated non-top-level code to detect
59 ;;; closed over top level variables. We just do COMPUTE-CLOSURE on all
60 ;;; the lambdas. This will pre-allocate environments for all the
61 ;;; functions with closed-over top level variables. The post-pass will
62 ;;; use the existing structure, rather than allocating a new one. We
63 ;;; return true if we discover any possible closure vars.
64 (defun pre-physenv-analyze-toplevel (component)
65   (declare (type component component))
66   (let ((found-it nil))
67     (dolist (lambda (component-lambdas component))
68       (when (compute-closure lambda)
69         (setq found-it t))
70       (dolist (let (lambda-lets lambda))
71         (when (compute-closure let)
72           (setq found-it t))))
73     found-it))
74
75 ;;; This is like old CMU CL PRE-ENVIRONMENT-ANALYZE-TOPLEVEL, except
76 ;;;   (1) It's been brought into the post-0.7.0 world where the property
77 ;;;       HAS-EXTERNAL-REFERENCES-P is orthogonal to the property of
78 ;;;       being specialized/optimized for locall at top level.
79 ;;;   (2) There's no return value, since we don't care whether we
80 ;;;       find any possible closure variables.
81 ;;;
82 ;;; I wish I could find an explanation of why
83 ;;; PRE-ENVIRONMENT-ANALYZE-TOPLEVEL is important. The old CMU CL
84 ;;; comments said
85 ;;;     Called on component with top level lambdas before the
86 ;;;     compilation of the associated non-top-level code to detect
87 ;;;     closed over top level variables. We just do COMPUTE-CLOSURE on
88 ;;;     all the lambdas. This will pre-allocate environments for all
89 ;;;     the functions with closed-over top level variables. The
90 ;;;     post-pass will use the existing structure, rather than
91 ;;;     allocating a new one. We return true if we discover any
92 ;;;     possible closure vars.
93 ;;; But that doesn't seem to explain why it's important. I do observe
94 ;;; that when it's not done, compiler assertions occasionally fail. My
95 ;;; tentative hypothesis is that other environment analysis expects to
96 ;;; bottom out on the outermost enclosing thing, and (insert
97 ;;; mysterious reason here) it's important to set up bottomed-out-here
98 ;;; environments before anything else. -- WHN 2001-09-30
99 (defun preallocate-physenvs-for-toplevelish-lambdas (component)
100   (/show "entering PREALLOCATE-PHYSENVS-FOR-TOPLEVELISH-LAMDBAS" component)
101   (dolist (clambda (component-lambdas component))
102     (/show clambda (lambda-vars clambda) (lambda-toplevelish-p clambda))
103     (when (lambda-toplevelish-p clambda)
104       (compute-closure clambda)))
105   (/show "leaving PREALLOCATE-PHYSENVS-FOR-TOPLEVELISH-LAMDBAS" component)
106   (values))
107
108 ;;; If CLAMBDA has a PHYSENV , return it, otherwise assign an empty one.
109 (defun get-lambda-physenv (clambda)
110   (declare (type clambda clambda))
111   (let ((homefun (lambda-home clambda)))
112     (or (lambda-physenv homefun)
113         (let ((res (make-physenv :function homefun)))
114           (setf (lambda-physenv homefun) res)
115           (dolist (letlambda (lambda-lets homefun))
116             ;; This assertion is to make explicit an
117             ;; apparently-otherwise-undocumented property of existing
118             ;; code: We never overwrite an old LAMBDA-PHYSENV.
119             ;; -- WHN 2001-09-30
120             (aver (null (lambda-physenv letlambda)))
121             ;; I *think* this is true regardless of LAMBDA-KIND.
122             ;; -- WHN 2001-09-30
123             (aver (eql (lambda-home letlambda) homefun))
124             (setf (lambda-physenv letlambda) res))
125           res))))
126
127 ;;; If FUN has no physical environment, assign one, otherwise clean up
128 ;;; the old physical environment, removing/flagging variables that
129 ;;; have no sets or refs. If a var has no references, we remove it
130 ;;; from the closure. If it has no sets, we clear the INDIRECT flag.
131 ;;; This is necessary because pre-analysis is done before
132 ;;; optimization.
133 (defun reinit-lambda-physenv (fun)
134   (let ((old (lambda-physenv (lambda-home fun))))
135     (cond (old
136            (setf (physenv-closure old)
137                  (delete-if (lambda (x)
138                               (and (lambda-var-p x)
139                                    (null (leaf-refs x))))
140                             (physenv-closure old)))
141            (flet ((clear (fun)
142                     (dolist (var (lambda-vars fun))
143                       (unless (lambda-var-sets var)
144                         (setf (lambda-var-indirect var) nil)))))
145              (clear fun)
146              (map nil #'clear (lambda-lets fun))))
147           (t
148            (get-lambda-physenv fun))))
149   (values))
150
151 ;;; Get NODE's environment, assigning one if necessary.
152 (defun get-node-physenv (node)
153   (declare (type node node))
154   (get-lambda-physenv (node-home-lambda node)))
155
156 ;;; Find any variables in FUN with references outside of the home
157 ;;; environment and close over them. If a closed over variable is set,
158 ;;; then we set the INDIRECT flag so that we will know the closed over
159 ;;; value is really a pointer to the value cell. We also warn about
160 ;;; unreferenced variables here, just because it's a convenient place
161 ;;; to do it. We return true if we close over anything.
162 (defun compute-closure (fun)
163   (declare (type clambda fun))
164   (let ((env (get-lambda-physenv fun))
165         (did-something nil))
166     (note-unreferenced-vars fun)
167     (dolist (var (lambda-vars fun))
168       (dolist (ref (leaf-refs var))
169         (let ((ref-env (get-node-physenv ref)))
170           (unless (eq ref-env env)
171             (when (lambda-var-sets var)
172               (setf (lambda-var-indirect var) t))
173             (setq did-something t)
174             (close-over var ref-env env))))
175       (dolist (set (basic-var-sets var))
176         (let ((set-env (get-node-physenv set)))
177           (unless (eq set-env env)
178             (setq did-something t)
179             (setf (lambda-var-indirect var) t)
180             (close-over var set-env env)))))
181     did-something))
182
183 ;;; Make sure that THING is closed over in REF-ENV and in all
184 ;;; environments for the functions that reference REF-ENV's function
185 ;;; (not just calls.) HOME-ENV is THING's home environment. When we
186 ;;; reach the home environment, we stop propagating the closure.
187 (defun close-over (thing ref-env home-env)
188   (declare (type physenv ref-env home-env))
189   (cond ((eq ref-env home-env))
190         ((member thing (physenv-closure ref-env)))
191         (t
192          (push thing (physenv-closure ref-env))
193          (dolist (call (leaf-refs (physenv-function ref-env)))
194            (close-over thing (get-node-physenv call) home-env))))
195   (values))
196 \f
197 ;;;; non-local exit
198
199 ;;; Insert the entry stub before the original exit target, and add a
200 ;;; new entry to the PHYSENV-NLX-INFO. The %NLX-ENTRY call in the
201 ;;; stub is passed the NLX-INFO as an argument so that the back end
202 ;;; knows what entry is being done.
203 ;;;
204 ;;; The link from the EXIT block to the entry stub is changed to be a
205 ;;; link to the component head. Similarly, the EXIT block is linked to
206 ;;; the component tail. This leaves the entry stub reachable, but
207 ;;; makes the flow graph less confusing to flow analysis.
208 ;;;
209 ;;; If a CATCH or an UNWIND-protect, then we set the LEXENV for the
210 ;;; last node in the cleanup code to be the enclosing environment, to
211 ;;; represent the fact that the binding was undone as a side-effect of
212 ;;; the exit. This will cause a lexical exit to be broken up if we are
213 ;;; actually exiting the scope (i.e. a BLOCK), and will also do any
214 ;;; other cleanups that may have to be done on the way.
215 (defun insert-nlx-entry-stub (exit env)
216   (declare (type physenv env) (type exit exit))
217   (let* ((exit-block (node-block exit))
218          (next-block (first (block-succ exit-block)))
219          (cleanup (entry-cleanup (exit-entry exit)))
220          (info (make-nlx-info :cleanup cleanup
221                               :continuation (node-cont exit)))
222          (entry (exit-entry exit))
223          (new-block (insert-cleanup-code exit-block next-block
224                                          entry
225                                          `(%nlx-entry ',info)
226                                          (entry-cleanup entry)))
227          (component (block-component new-block)))
228     (unlink-blocks exit-block new-block)
229     (link-blocks exit-block (component-tail component))
230     (link-blocks (component-head component) new-block)
231
232     (setf (nlx-info-target info) new-block)
233     (push info (physenv-nlx-info env))
234     (push info (cleanup-nlx-info cleanup))
235     (when (member (cleanup-kind cleanup) '(:catch :unwind-protect))
236       (setf (node-lexenv (block-last new-block))
237             (node-lexenv entry))))
238
239   (values))
240
241 ;;; Do stuff necessary to represent a non-local exit from the node
242 ;;; EXIT into ENV. This is called for each non-local exit node, of
243 ;;; which there may be several per exit continuation. This is what we
244 ;;; do:
245 ;;; -- If there isn't any NLX-Info entry in the environment, make
246 ;;;    an entry stub, otherwise just move the exit block link to
247 ;;;    the component tail.
248 ;;; -- Close over the NLX-Info in the exit environment.
249 ;;; -- If the exit is from an :Escape function, then substitute a
250 ;;;    constant reference to NLX-Info structure for the escape
251 ;;;    function reference. This will cause the escape function to
252 ;;;    be deleted (although not removed from the DFO.)  The escape
253 ;;;    function is no longer needed, and we don't want to emit code
254 ;;;    for it. We then also change the %NLX-ENTRY call to use the
255 ;;;    NLX continuation so that there will be a use to represent
256 ;;;    the NLX use.
257 (defun note-non-local-exit (env exit)
258   (declare (type physenv env) (type exit exit))
259   (let ((entry (exit-entry exit))
260         (cont (node-cont exit))
261         (exit-fun (node-home-lambda exit)))
262
263     (if (find-nlx-info entry cont)
264         (let ((block (node-block exit)))
265           (aver (= (length (block-succ block)) 1))
266           (unlink-blocks block (first (block-succ block)))
267           (link-blocks block (component-tail (block-component block))))
268         (insert-nlx-entry-stub exit env))
269
270     (let ((info (find-nlx-info entry cont)))
271       (aver info)
272       (close-over info (node-physenv exit) env)
273       (when (eq (functional-kind exit-fun) :escape)
274         (mapc #'(lambda (x)
275                   (setf (node-derived-type x) *wild-type*))
276               (leaf-refs exit-fun))
277         (substitute-leaf (find-constant info) exit-fun)
278         (let ((node (block-last (nlx-info-target info))))
279           (delete-continuation-use node)
280           (add-continuation-use node (nlx-info-continuation info))))))
281
282   (values))
283
284 ;;; Iterate over the EXITs in COMPONENT, calling NOTE-NON-LOCAL-EXIT
285 ;;; when we find a block that ends in a non-local EXIT node. We also
286 ;;; ensure that all EXIT nodes are either non-local or degenerate by
287 ;;; calling IR1-OPTIMIZE-EXIT on local exits. This makes life simpler
288 ;;; for later phases.
289 (defun find-non-local-exits (component)
290   (declare (type component component))
291   (dolist (lambda (component-lambdas component))
292     (dolist (entry (lambda-entries lambda))
293       (dolist (exit (entry-exits entry))
294         (let ((target-env (node-physenv entry)))
295           (if (eq (node-physenv exit) target-env)
296               (maybe-delete-exit exit)
297               (note-non-local-exit target-env exit))))))
298
299   (values))
300 \f
301 ;;;; cleanup emission
302
303 ;;; Zoom up the cleanup nesting until we hit CLEANUP1, accumulating
304 ;;; cleanup code as we go. When we are done, convert the cleanup code
305 ;;; in an implicit MV-PROG1. We have to force local call analysis of
306 ;;; new references to UNWIND-PROTECT cleanup functions. If we don't
307 ;;; actually have to do anything, then we don't insert any cleanup
308 ;;; code.
309 ;;;
310 ;;; If we do insert cleanup code, we check that BLOCK1 doesn't end in
311 ;;; a "tail" local call.
312 ;;;
313 ;;; We don't need to adjust the ending cleanup of the cleanup block,
314 ;;; since the cleanup blocks are inserted at the start of the DFO, and
315 ;;; are thus never scanned.
316 (defun emit-cleanups (block1 block2)
317   (declare (type cblock block1 block2))
318   (collect ((code)
319             (reanalyze-funs))
320     (let ((cleanup2 (block-start-cleanup block2)))
321       (do ((cleanup (block-end-cleanup block1)
322                     (node-enclosing-cleanup (cleanup-mess-up cleanup))))
323           ((eq cleanup cleanup2))
324         (let* ((node (cleanup-mess-up cleanup))
325                (args (when (basic-combination-p node)
326                        (basic-combination-args node))))
327           (ecase (cleanup-kind cleanup)
328             (:special-bind
329              (code `(%special-unbind ',(continuation-value (first args)))))
330             (:catch
331              (code `(%catch-breakup)))
332             (:unwind-protect
333              (code `(%unwind-protect-breakup))
334              (let ((fun (ref-leaf (continuation-use (second args)))))
335                (reanalyze-funs fun)
336                (code `(%funcall ,fun))))
337             ((:block :tagbody)
338              (dolist (nlx (cleanup-nlx-info cleanup))
339                (code `(%lexical-exit-breakup ',nlx)))))))
340
341       (when (code)
342         (aver (not (node-tail-p (block-last block1))))
343         (insert-cleanup-code block1 block2
344                              (block-last block1)
345                              `(progn ,@(code)))
346         (dolist (fun (reanalyze-funs))
347           (local-call-analyze-1 fun)))))
348
349   (values))
350
351 ;;; Loop over the blocks in COMPONENT, calling EMIT-CLEANUPS when we
352 ;;; see a successor in the same environment with a different cleanup.
353 ;;; We ignore the cleanup transition if it is to a cleanup enclosed by
354 ;;; the current cleanup, since in that case we are just messing up the
355 ;;; environment, hence this is not the place to clean it.
356 (defun find-cleanup-points (component)
357   (declare (type component component))
358   (do-blocks (block1 component)
359     (let ((env1 (block-physenv block1))
360           (cleanup1 (block-end-cleanup block1)))
361       (dolist (block2 (block-succ block1))
362         (when (block-start block2)
363           (let ((env2 (block-physenv block2))
364                 (cleanup2 (block-start-cleanup block2)))
365             (unless (or (not (eq env2 env1))
366                         (eq cleanup1 cleanup2)
367                         (and cleanup2
368                              (eq (node-enclosing-cleanup
369                                   (cleanup-mess-up cleanup2))
370                                  cleanup1)))
371               (emit-cleanups block1 block2)))))))
372   (values))
373
374 ;;; Mark all tail-recursive uses of function result continuations with
375 ;;; the corresponding TAIL-SET. Nodes whose type is NIL (i.e. don't
376 ;;; return) such as calls to ERROR are never annotated as tail in
377 ;;; order to preserve debugging information.
378 (defun tail-annotate (component)
379   (declare (type component component))
380   (dolist (fun (component-lambdas component))
381     (let ((ret (lambda-return fun)))
382       (when ret
383         (let ((result (return-result ret)))
384           (do-uses (use result)
385             (when (and (immediately-used-p result use)
386                      (or (not (eq (node-derived-type use) *empty-type*))
387                          (not (basic-combination-p use))
388                          (eq (basic-combination-kind use) :local)))
389                 (setf (node-tail-p use) t)))))))
390   (values))