Commit | Line | Data |
---|---|---|
6e8ad823 AW |
1 | ;;; Continuation-passing style (CPS) intermediate language (IL) |
2 | ||
6eb02960 | 3 | ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc. |
6e8ad823 AW |
4 | |
5 | ;;;; This library is free software; you can redistribute it and/or | |
6 | ;;;; modify it under the terms of the GNU Lesser General Public | |
7 | ;;;; License as published by the Free Software Foundation; either | |
8 | ;;;; version 3 of the License, or (at your option) any later version. | |
9 | ;;;; | |
10 | ;;;; This library is distributed in the hope that it will be useful, | |
11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | ;;;; Lesser General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU Lesser General Public | |
16 | ;;;; License along with this library; if not, write to the Free Software | |
17 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | ||
19 | ;;; Commentary: | |
20 | ;;; | |
21 | ;;; Many passes rely on a local or global static analysis of a function. | |
22 | ;;; This module implements a simple data-flow graph (DFG) analysis, | |
23 | ;;; tracking the definitions and uses of variables and continuations. | |
f22979db | 24 | ;;; It also builds a table of continuations and scope links, to be able |
6e8ad823 AW |
25 | ;;; to easily determine if one continuation is in the scope of another, |
26 | ;;; and to get to the expression inside a continuation. | |
27 | ;;; | |
28 | ;;; Note that the data-flow graph of continuation labels is a | |
29 | ;;; control-flow graph. | |
30 | ;;; | |
31 | ;;; We currently don't expose details of the DFG type outside this | |
32 | ;;; module, preferring to only expose accessors. That may change in the | |
33 | ;;; future but it seems to work for now. | |
34 | ;;; | |
35 | ;;; Code: | |
36 | ||
37 | (define-module (language cps dfg) | |
38 | #:use-module (ice-9 match) | |
39 | #:use-module (srfi srfi-1) | |
40 | #:use-module (srfi srfi-9) | |
41 | #:use-module (srfi srfi-26) | |
42 | #:use-module (language cps) | |
43 | #:export (build-cont-table | |
6e8ad823 AW |
44 | lookup-cont |
45 | ||
46 | compute-dfg | |
47 | dfg-cont-table | |
a8430ab1 AW |
48 | dfg-min-label |
49 | dfg-label-count | |
50 | dfg-min-var | |
51 | dfg-var-count | |
6e8ad823 AW |
52 | lookup-def |
53 | lookup-uses | |
f22979db AW |
54 | lookup-predecessors |
55 | lookup-successors | |
c8ad7426 | 56 | lookup-block-scope |
6e8ad823 AW |
57 | find-call |
58 | call-expression | |
59 | find-expression | |
60 | find-defining-expression | |
61 | find-constant-value | |
f22979db | 62 | continuation-bound-in? |
d51fb1e6 | 63 | variable-free-in? |
6e8ad823 | 64 | constant-needs-allocation? |
e636f424 | 65 | control-point? |
db11440d AW |
66 | lookup-bound-syms |
67 | ||
68 | ;; Data flow analysis. | |
69 | compute-live-variables | |
70 | dfa-k-idx dfa-k-sym dfa-k-count dfa-k-in dfa-k-out | |
29619661 | 71 | dfa-var-idx dfa-var-sym dfa-var-count |
db11440d | 72 | print-dfa)) |
6e8ad823 | 73 | |
48c2a539 AW |
74 | ;; These definitions are here because currently we don't do cross-module |
75 | ;; inlining. They can be removed once that restriction is gone. | |
76 | (define-inlinable (for-each f l) | |
77 | (unless (list? l) | |
78 | (scm-error 'wrong-type-arg "for-each" "Not a list: ~S" (list l) #f)) | |
79 | (let for-each1 ((l l)) | |
80 | (unless (null? l) | |
81 | (f (car l)) | |
82 | (for-each1 (cdr l))))) | |
83 | ||
84 | (define-inlinable (for-each/2 f l1 l2) | |
85 | (unless (= (length l1) (length l2)) | |
86 | (scm-error 'wrong-type-arg "for-each" "List of wrong length: ~S" | |
87 | (list l2) #f)) | |
88 | (let for-each2 ((l1 l1) (l2 l2)) | |
89 | (unless (null? l1) | |
90 | (f (car l1) (car l2)) | |
91 | (for-each2 (cdr l1) (cdr l2))))) | |
92 | ||
6e8ad823 | 93 | (define (build-cont-table fun) |
fbdb69b2 AW |
94 | (let ((max-k (fold-conts (lambda (k cont max-k) (max k max-k)) |
95 | -1 fun))) | |
96 | (fold-conts (lambda (k cont table) | |
97 | (vector-set! table k cont) | |
98 | table) | |
99 | (make-vector (1+ max-k) #f) | |
100 | fun))) | |
101 | ||
6e8ad823 AW |
102 | ;; Data-flow graph for CPS: both for values and continuations. |
103 | (define-record-type $dfg | |
4bf757b8 | 104 | (make-dfg conts preds defs uses scopes scope-levels |
a8430ab1 | 105 | min-label label-count min-var var-count) |
6e8ad823 | 106 | dfg? |
5e897908 | 107 | ;; vector of label -> $kif, $kargs, etc |
6e8ad823 | 108 | (conts dfg-cont-table) |
5fc40391 | 109 | ;; vector of label -> (pred-label ...) |
21d6d183 | 110 | (preds dfg-preds) |
5fc40391 | 111 | ;; vector of var -> def-label |
98c5b69f | 112 | (defs dfg-defs) |
5fc40391 | 113 | ;; vector of var -> (use-label ...) |
98c5b69f | 114 | (uses dfg-uses) |
5fc40391 AW |
115 | ;; vector of label -> label |
116 | (scopes dfg-scopes) | |
117 | ;; vector of label -> int | |
118 | (scope-levels dfg-scope-levels) | |
5e897908 AW |
119 | |
120 | (min-label dfg-min-label) | |
a8430ab1 | 121 | (label-count dfg-label-count) |
5e897908 | 122 | (min-var dfg-min-var) |
a8430ab1 | 123 | (var-count dfg-var-count)) |
6e8ad823 | 124 | |
334bd8e3 AW |
125 | ;; Control-flow analysis. |
126 | (define-record-type $cfa | |
16af91e8 | 127 | (make-cfa min-label k-map order preds) |
334bd8e3 | 128 | cfa? |
16af91e8 AW |
129 | ;; Minumum label. |
130 | (min-label cfa-min-label) | |
131 | ;; Vector of (k - min-label) -> k-idx | |
334bd8e3 AW |
132 | (k-map cfa-k-map) |
133 | ;; Vector of k-idx -> k-sym, in reverse post order | |
134 | (order cfa-order) | |
135 | ;; Vector of k-idx -> list of k-idx | |
dda5fd94 AW |
136 | (preds cfa-preds)) |
137 | ||
16af91e8 AW |
138 | (define (cfa-k-idx cfa k) |
139 | (vector-ref (cfa-k-map cfa) (- k (cfa-min-label cfa)))) | |
dda5fd94 AW |
140 | |
141 | (define (cfa-k-count cfa) | |
142 | (vector-length (cfa-order cfa))) | |
143 | ||
144 | (define (cfa-k-sym cfa n) | |
145 | (vector-ref (cfa-order cfa) n)) | |
146 | ||
147 | (define (cfa-predecessors cfa n) | |
148 | (vector-ref (cfa-preds cfa) n)) | |
149 | ||
9002277d AW |
150 | (define-inlinable (vector-push! vec idx val) |
151 | (let ((v vec) (i idx)) | |
152 | (vector-set! v i (cons val (vector-ref v i))))) | |
153 | ||
16af91e8 | 154 | (define (compute-reachable dfg min-label label-count) |
9002277d AW |
155 | "Given the forward control-flow analysis in CFA, compute and return |
156 | the continuations that may be reached if flow reaches a continuation N. | |
157 | Returns a vector of bitvectors. The given CFA should be a forward CFA, | |
158 | for quickest convergence." | |
16af91e8 AW |
159 | (let (;; Vector of bitvectors, indicating that continuation N can |
160 | ;; reach a set M... | |
161 | (reachable (make-vector label-count #f))) | |
162 | ||
163 | (define (label->idx label) (- label min-label)) | |
9002277d AW |
164 | |
165 | ;; All continuations are reachable from themselves. | |
166 | (let lp ((n 0)) | |
16af91e8 AW |
167 | (when (< n label-count) |
168 | (let ((bv (make-bitvector label-count #f))) | |
9002277d AW |
169 | (bitvector-set! bv n #t) |
170 | (vector-set! reachable n bv) | |
171 | (lp (1+ n))))) | |
172 | ||
9002277d | 173 | ;; Iterate cfa backwards, to converge quickly. |
16af91e8 AW |
174 | (let ((tmp (make-bitvector label-count #f))) |
175 | (define (add-reachable! succ) | |
176 | (bit-set*! tmp (vector-ref reachable (label->idx succ)) #t)) | |
177 | (let lp ((label (+ min-label label-count)) (changed? #f)) | |
9002277d | 178 | (cond |
16af91e8 | 179 | ((= label min-label) |
9002277d | 180 | (if changed? |
16af91e8 | 181 | (lp (+ min-label label-count) #f) |
9002277d AW |
182 | reachable)) |
183 | (else | |
16af91e8 AW |
184 | (let* ((label (1- label)) |
185 | (idx (label->idx label))) | |
9002277d | 186 | (bitvector-fill! tmp #f) |
16af91e8 AW |
187 | (visit-cont-successors |
188 | (case-lambda | |
189 | (() #t) | |
190 | ((succ0) (add-reachable! succ0)) | |
191 | ((succ0 succ1) (add-reachable! succ0) (add-reachable! succ1))) | |
192 | (lookup-cont label dfg)) | |
193 | (bitvector-set! tmp idx #t) | |
194 | (bit-set*! tmp (vector-ref reachable idx) #f) | |
9002277d AW |
195 | (cond |
196 | ((bit-position #t tmp 0) | |
16af91e8 AW |
197 | (bit-set*! (vector-ref reachable idx) tmp #t) |
198 | (lp label #t)) | |
9002277d | 199 | (else |
16af91e8 | 200 | (lp label changed?)))))))))) |
9002277d | 201 | |
16af91e8 AW |
202 | (define (find-prompts dfg min-label label-count) |
203 | "Find the prompts in DFG between MIN-LABEL and MIN-LABEL + | |
204 | LABEL-COUNT, and return them as a list of PROMPT-LABEL, HANDLER-LABEL | |
205 | pairs." | |
206 | (let lp ((label min-label) (prompts '())) | |
9002277d | 207 | (cond |
16af91e8 | 208 | ((= label (+ min-label label-count)) |
9002277d AW |
209 | (reverse prompts)) |
210 | (else | |
16af91e8 | 211 | (match (lookup-cont label dfg) |
9002277d AW |
212 | (($ $kargs names syms body) |
213 | (match (find-expression body) | |
214 | (($ $prompt escape? tag handler) | |
16af91e8 AW |
215 | (lp (1+ label) (acons label handler prompts))) |
216 | (_ (lp (1+ label) prompts)))) | |
217 | (_ (lp (1+ label) prompts))))))) | |
9002277d | 218 | |
16af91e8 | 219 | (define (compute-interval reachable min-label label-count start end) |
9002277d AW |
220 | "Compute and return the set of continuations that may be reached from |
221 | START, inclusive, but not reached by END, exclusive. Returns a | |
222 | bitvector." | |
16af91e8 AW |
223 | (let ((body (make-bitvector label-count #f))) |
224 | (bit-set*! body (vector-ref reachable (- start min-label)) #t) | |
225 | (bit-set*! body (vector-ref reachable (- end min-label)) #f) | |
9002277d AW |
226 | body)) |
227 | ||
16af91e8 AW |
228 | (define (find-prompt-bodies dfg min-label label-count) |
229 | "Find all the prompts in DFG from the LABEL-COUNT continuations | |
230 | starting at MIN-LABEL, and compute the set of continuations that is | |
231 | reachable from the prompt bodies but not from the corresponding handler. | |
232 | Returns a list of PROMPT, HANDLER, BODY lists, where the BODY is a | |
233 | bitvector." | |
234 | (match (find-prompts dfg min-label label-count) | |
9002277d AW |
235 | (() '()) |
236 | (((prompt . handler) ...) | |
16af91e8 | 237 | (let ((reachable (compute-reachable dfg min-label label-count))) |
9002277d AW |
238 | (map (lambda (prompt handler) |
239 | ;; FIXME: It isn't correct to use all continuations | |
240 | ;; reachable from the prompt, because that includes | |
241 | ;; continuations outside the prompt body. This point is | |
242 | ;; moot if the handler's control flow joins with the the | |
243 | ;; body, as is usually but not always the case. | |
244 | ;; | |
245 | ;; One counter-example is when the handler contifies an | |
246 | ;; infinite loop; in that case we compute a too-large | |
16af91e8 AW |
247 | ;; prompt body. This error is currently innocuous, but we |
248 | ;; should fix it at some point. | |
9002277d AW |
249 | ;; |
250 | ;; The fix is to end the body at the corresponding "pop" | |
251 | ;; primcall, if any. | |
16af91e8 AW |
252 | (let ((body (compute-interval reachable min-label label-count |
253 | prompt handler))) | |
9002277d AW |
254 | (list prompt handler body))) |
255 | prompt handler))))) | |
256 | ||
16af91e8 | 257 | (define* (visit-prompt-control-flow dfg min-label label-count f #:key complete?) |
9002277d AW |
258 | "For all prompts in CFA, invoke F with arguments PROMPT, HANDLER, and |
259 | BODY for each body continuation in the prompt." | |
16af91e8 AW |
260 | (define (label->idx label) (- label min-label)) |
261 | (define (idx->label idx) (+ idx min-label)) | |
9002277d AW |
262 | (for-each |
263 | (match-lambda | |
264 | ((prompt handler body) | |
265 | (define (out-or-back-edge? n) | |
266 | ;; Most uses of visit-prompt-control-flow don't need every body | |
267 | ;; continuation, and would be happy getting called only for | |
268 | ;; continuations that postdominate the rest of the body. Unless | |
269 | ;; you pass #:complete? #t, we only invoke F on continuations | |
270 | ;; that can leave the body, or on back-edges in loops. | |
271 | ;; | |
272 | ;; You would think that looking for the final "pop" primcall | |
273 | ;; would be sufficient, but that is incorrect; it's possible for | |
274 | ;; a loop in the prompt body to be contified, and that loop need | |
275 | ;; not continue to the pop if it never terminates. The pop could | |
276 | ;; even be removed by DCE, in that case. | |
277 | (or-map (lambda (succ) | |
16af91e8 | 278 | (let ((succ (label->idx succ))) |
9002277d AW |
279 | (or (not (bitvector-ref body succ)) |
280 | (<= succ n)))) | |
16af91e8 | 281 | (lookup-successors (idx->label n) dfg))) |
9002277d AW |
282 | (let lp ((n 0)) |
283 | (let ((n (bit-position #t body n))) | |
284 | (when n | |
285 | (when (or complete? (out-or-back-edge? n)) | |
16af91e8 | 286 | (f prompt handler (idx->label n))) |
9002277d | 287 | (lp (1+ n))))))) |
16af91e8 AW |
288 | (find-prompt-bodies dfg min-label label-count))) |
289 | ||
290 | (define (analyze-reverse-control-flow fun dfg) | |
16af91e8 AW |
291 | (define (compute-reverse-control-flow-order ktail dfg min-label label-count) |
292 | (let ((order (make-vector label-count #f)) | |
293 | (label-map (make-vector label-count #f)) | |
294 | (next -1)) | |
295 | (define (label->idx label) (- label min-label)) | |
296 | (define (idx->label idx) (+ idx min-label)) | |
297 | ||
298 | (let visit ((k ktail)) | |
299 | ;; Mark this label as visited. | |
300 | (vector-set! label-map (label->idx k) #t) | |
301 | (for-each (lambda (k) | |
302 | ;; Visit predecessors unless they are already visited. | |
303 | (unless (vector-ref label-map (label->idx k)) | |
304 | (visit k))) | |
305 | (lookup-predecessors k dfg)) | |
306 | ;; Add to reverse post-order chain. | |
307 | (vector-set! label-map (label->idx k) next) | |
308 | (set! next k)) | |
309 | ||
310 | (let lp ((n 0) (head next)) | |
311 | (if (< head 0) | |
312 | ;; Add nodes that are not reachable from the tail. | |
313 | (let lp ((n n) (m label-count)) | |
314 | (unless (= n label-count) | |
315 | (let find-unvisited ((m (1- m))) | |
316 | (if (vector-ref label-map m) | |
317 | (find-unvisited (1- m)) | |
318 | (begin | |
319 | (vector-set! label-map m n) | |
320 | (lp (1+ n) m)))))) | |
321 | ;; Pop the head off the chain, give it its | |
322 | ;; reverse-post-order numbering, and continue. | |
323 | (let ((next (vector-ref label-map (label->idx head)))) | |
324 | (vector-set! label-map (label->idx head) n) | |
325 | (lp (1+ n) next)))) | |
326 | ||
327 | (let lp ((n 0)) | |
328 | (when (< n label-count) | |
329 | (vector-set! order (vector-ref label-map n) (idx->label n)) | |
330 | (lp (1+ n)))) | |
331 | ||
332 | (values order label-map))) | |
333 | ||
334 | (define (convert-successors k-map min-label) | |
335 | (define (idx->label idx) (+ idx min-label)) | |
336 | (define (renumber label) | |
337 | (vector-ref k-map (- label min-label))) | |
338 | (let ((succs (make-vector (vector-length k-map) #f))) | |
339 | (let lp ((n 0)) | |
340 | (when (< n (vector-length succs)) | |
341 | (vector-set! succs (vector-ref k-map n) | |
342 | (map renumber | |
343 | (lookup-successors (idx->label n) dfg))) | |
344 | (lp (1+ n)))) | |
345 | succs)) | |
346 | ||
347 | (define (build-cfa ktail min-label label-count order k-map) | |
348 | (let* ((succs (convert-successors k-map min-label)) | |
349 | (cfa (make-cfa min-label k-map order succs))) | |
350 | ;; Any expression in the prompt body could cause an abort to the | |
351 | ;; handler. This code adds links from every block in the prompt | |
352 | ;; body to the handler. This causes all values used by the | |
353 | ;; handler to be seen as live in the prompt body, as indeed they | |
354 | ;; are. | |
355 | (visit-prompt-control-flow | |
356 | dfg min-label label-count | |
357 | (lambda (prompt handler body) | |
358 | (define (renumber label) | |
359 | (vector-ref k-map (- label min-label))) | |
360 | (vector-push! succs (renumber body) (renumber handler)))) | |
9002277d | 361 | cfa)) |
16af91e8 | 362 | |
ae0388b6 AW |
363 | (unless (= (vector-length (dfg-cont-table dfg)) (dfg-label-count dfg)) |
364 | (error "function needs renumbering")) | |
365 | ||
366 | (let ((min-label (dfg-min-label dfg)) | |
367 | (label-count (dfg-label-count dfg))) | |
368 | (match fun | |
369 | (($ $fun src meta free | |
370 | ($ $cont kentry ($ $kentry self ($ $cont ktail tail)))) | |
371 | (call-with-values | |
372 | (lambda () | |
373 | (compute-reverse-control-flow-order ktail dfg | |
374 | min-label label-count)) | |
375 | (lambda (order k-map) | |
376 | (build-cfa ktail min-label label-count order k-map))))))) | |
dda5fd94 AW |
377 | |
378 | ;; Dominator analysis. | |
379 | (define-record-type $dominator-analysis | |
380 | (make-dominator-analysis cfa idoms dom-levels loop-header irreducible) | |
381 | dominator-analysis? | |
382 | ;; The corresponding $cfa | |
383 | (cfa dominator-analysis-cfa) | |
334bd8e3 | 384 | ;; Vector of k-idx -> k-idx |
dda5fd94 | 385 | (idoms dominator-analysis-idoms) |
334bd8e3 | 386 | ;; Vector of k-idx -> dom-level |
dda5fd94 | 387 | (dom-levels dominator-analysis-dom-levels) |
334bd8e3 | 388 | ;; Vector of k-idx -> k-idx or -1 |
dda5fd94 | 389 | (loop-header dominator-analysis-loop-header) |
334bd8e3 | 390 | ;; Vector of k-idx -> true or false value |
dda5fd94 | 391 | (irreducible dominator-analysis-irreducible)) |
334bd8e3 | 392 | |
366eb4d7 AW |
393 | (define (compute-dom-levels idoms) |
394 | (let ((dom-levels (make-vector (vector-length idoms) #f))) | |
3aee6cfd AW |
395 | (define (compute-dom-level n) |
396 | (or (vector-ref dom-levels n) | |
397 | (let ((dom-level (1+ (compute-dom-level (vector-ref idoms n))))) | |
398 | (vector-set! dom-levels n dom-level) | |
399 | dom-level))) | |
400 | (vector-set! dom-levels 0 0) | |
401 | (let lp ((n 0)) | |
366eb4d7 AW |
402 | (when (< n (vector-length idoms)) |
403 | (compute-dom-level n) | |
404 | (lp (1+ n)))) | |
405 | dom-levels)) | |
3aee6cfd | 406 | |
366eb4d7 AW |
407 | (define (compute-idoms preds) |
408 | (let ((idoms (make-vector (vector-length preds) 0))) | |
3aee6cfd AW |
409 | (define (common-idom d0 d1) |
410 | ;; We exploit the fact that a reverse post-order is a topological | |
411 | ;; sort, and so the idom of a node is always numerically less than | |
412 | ;; the node itself. | |
413 | (cond | |
414 | ((= d0 d1) d0) | |
415 | ((< d0 d1) (common-idom d0 (vector-ref idoms d1))) | |
416 | (else (common-idom (vector-ref idoms d0) d1)))) | |
417 | (define (compute-idom preds) | |
418 | (match preds | |
419 | (() 0) | |
420 | ((pred . preds) | |
421 | (let lp ((idom pred) (preds preds)) | |
422 | (match preds | |
423 | (() idom) | |
424 | ((pred . preds) | |
425 | (lp (common-idom idom pred) preds))))))) | |
426 | ;; This is the iterative O(n^2) fixpoint algorithm, originally from | |
427 | ;; Allen and Cocke ("Graph-theoretic constructs for program flow | |
428 | ;; analysis", 1972). See the discussion in Cooper, Harvey, and | |
429 | ;; Kennedy's "A Simple, Fast Dominance Algorithm", 2001. | |
430 | (let iterate ((n 0) (changed? #f)) | |
431 | (cond | |
432 | ((< n (vector-length preds)) | |
433 | (let ((idom (vector-ref idoms n)) | |
434 | (idom* (compute-idom (vector-ref preds n)))) | |
435 | (cond | |
436 | ((eqv? idom idom*) | |
437 | (iterate (1+ n) changed?)) | |
438 | (else | |
439 | (vector-set! idoms n idom*) | |
440 | (iterate (1+ n) #t))))) | |
441 | (changed? | |
442 | (iterate 0 #f)) | |
366eb4d7 AW |
443 | (else idoms))))) |
444 | ||
96b8027c AW |
445 | ;; Compute a vector containing, for each node, a list of the nodes that |
446 | ;; it immediately dominates. These are the "D" edges in the DJ tree. | |
447 | (define (compute-dom-edges idoms) | |
448 | (let ((doms (make-vector (vector-length idoms) '()))) | |
449 | (let lp ((n 0)) | |
450 | (when (< n (vector-length idoms)) | |
451 | (let ((idom (vector-ref idoms n))) | |
452 | (vector-push! doms idom n)) | |
453 | (lp (1+ n)))) | |
454 | doms)) | |
455 | ||
456 | ;; Compute a vector containing, for each node, a list of the successors | |
457 | ;; of that node that are not dominated by that node. These are the "J" | |
458 | ;; edges in the DJ tree. | |
459 | (define (compute-join-edges preds idoms) | |
460 | (define (dominates? n1 n2) | |
461 | (or (= n1 n2) | |
462 | (and (< n1 n2) | |
463 | (dominates? n1 (vector-ref idoms n2))))) | |
464 | (let ((joins (make-vector (vector-length idoms) '()))) | |
465 | (let lp ((n 0)) | |
466 | (when (< n (vector-length preds)) | |
467 | (for-each (lambda (pred) | |
468 | (unless (dominates? pred n) | |
469 | (vector-push! joins pred n))) | |
470 | (vector-ref preds n)) | |
471 | (lp (1+ n)))) | |
472 | joins)) | |
473 | ||
474 | ;; Compute a vector containing, for each node, a list of the back edges | |
475 | ;; to that node. If a node is not the entry of a reducible loop, that | |
476 | ;; list is empty. | |
477 | (define (compute-reducible-back-edges joins idoms) | |
478 | (define (dominates? n1 n2) | |
479 | (or (= n1 n2) | |
480 | (and (< n1 n2) | |
481 | (dominates? n1 (vector-ref idoms n2))))) | |
482 | (let ((back-edges (make-vector (vector-length idoms) '()))) | |
483 | (let lp ((n 0)) | |
484 | (when (< n (vector-length joins)) | |
485 | (for-each (lambda (succ) | |
486 | (when (dominates? succ n) | |
487 | (vector-push! back-edges succ n))) | |
488 | (vector-ref joins n)) | |
489 | (lp (1+ n)))) | |
490 | back-edges)) | |
491 | ||
492 | ;; Compute the levels in the dominator tree at which there are | |
493 | ;; irreducible loops, as an integer. If a bit N is set in the integer, | |
494 | ;; that indicates that at level N in the dominator tree, there is at | |
495 | ;; least one irreducible loop. | |
496 | (define (compute-irreducible-dom-levels doms joins idoms dom-levels) | |
366eb4d7 AW |
497 | (define (dominates? n1 n2) |
498 | (or (= n1 n2) | |
499 | (and (< n1 n2) | |
500 | (dominates? n1 (vector-ref idoms n2))))) | |
96b8027c AW |
501 | (let ((pre-order (make-vector (vector-length doms) #f)) |
502 | (last-pre-order (make-vector (vector-length doms) #f)) | |
503 | (res 0) | |
504 | (count 0)) | |
505 | ;; Is MAYBE-PARENT an ancestor of N on the depth-first spanning tree | |
506 | ;; computed from the DJ graph? See Havlak 1997, "Nesting of | |
507 | ;; Reducible and Irreducible Loops". | |
508 | (define (ancestor? a b) | |
509 | (let ((w (vector-ref pre-order a)) | |
510 | (v (vector-ref pre-order b))) | |
511 | (and (<= w v) | |
512 | (<= v (vector-ref last-pre-order w))))) | |
513 | ;; Compute depth-first spanning tree of DJ graph. | |
514 | (define (recurse n) | |
515 | (unless (vector-ref pre-order n) | |
516 | (visit n))) | |
517 | (define (visit n) | |
518 | ;; Pre-order visitation index. | |
519 | (vector-set! pre-order n count) | |
520 | (set! count (1+ count)) | |
521 | (for-each recurse (vector-ref doms n)) | |
522 | (for-each recurse (vector-ref joins n)) | |
523 | ;; Pre-order visitation index of last descendant. | |
524 | (vector-set! last-pre-order (vector-ref pre-order n) (1- count))) | |
525 | ||
526 | (visit 0) | |
527 | ||
528 | (let lp ((n 0)) | |
529 | (when (< n (vector-length joins)) | |
530 | (for-each (lambda (succ) | |
531 | ;; If this join edge is not a loop back edge but it | |
532 | ;; does go to an ancestor on the DFST of the DJ | |
533 | ;; graph, then we have an irreducible loop. | |
534 | (when (and (not (dominates? succ n)) | |
535 | (ancestor? succ n)) | |
536 | (set! res (logior (ash 1 (vector-ref dom-levels succ)))))) | |
537 | (vector-ref joins n)) | |
538 | (lp (1+ n)))) | |
539 | ||
540 | res)) | |
541 | ||
542 | (define (compute-nodes-by-level dom-levels) | |
543 | (let* ((max-level (let lp ((n 0) (max-level 0)) | |
544 | (if (< n (vector-length dom-levels)) | |
545 | (lp (1+ n) (max (vector-ref dom-levels n) max-level)) | |
546 | max-level))) | |
547 | (nodes-by-level (make-vector (1+ max-level) '()))) | |
548 | (let lp ((n (1- (vector-length dom-levels)))) | |
549 | (when (>= n 0) | |
550 | (vector-push! nodes-by-level (vector-ref dom-levels n) n) | |
551 | (lp (1- n)))) | |
552 | nodes-by-level)) | |
553 | ||
554 | ;; Collect all predecessors to the back-nodes that are strictly | |
555 | ;; dominated by the loop header, and mark them as belonging to the loop. | |
556 | ;; If they already have a loop header, that means they are either in a | |
557 | ;; nested loop, or they have already been visited already. | |
558 | (define (mark-loop-body header back-nodes preds idoms loop-headers) | |
559 | (define (strictly-dominates? n1 n2) | |
560 | (and (< n1 n2) | |
561 | (let ((idom (vector-ref idoms n2))) | |
562 | (or (= n1 idom) | |
563 | (strictly-dominates? n1 idom))))) | |
564 | (define (visit node) | |
565 | (when (strictly-dominates? header node) | |
566 | (cond | |
567 | ((vector-ref loop-headers node) => visit) | |
568 | (else | |
569 | (vector-set! loop-headers node header) | |
570 | (for-each visit (vector-ref preds node)))))) | |
571 | (for-each visit back-nodes)) | |
572 | ||
573 | (define (mark-irreducible-loops level idoms dom-levels loop-headers) | |
574 | ;; FIXME: Identify strongly-connected components that are >= LEVEL in | |
575 | ;; the dominator tree, and somehow mark them as irreducible. | |
576 | (warn 'irreducible-loops-at-level level)) | |
577 | ||
578 | ;; "Identifying Loops Using DJ Graphs" by Sreedhar, Gao, and Lee, ACAPS | |
579 | ;; Technical Memo 98, 1995. | |
580 | (define (identify-loops preds idoms dom-levels) | |
581 | (let* ((doms (compute-dom-edges idoms)) | |
582 | (joins (compute-join-edges preds idoms)) | |
583 | (back-edges (compute-reducible-back-edges joins idoms)) | |
584 | (irreducible-levels | |
585 | (compute-irreducible-dom-levels doms joins idoms dom-levels)) | |
586 | (loop-headers (make-vector (vector-length preds) #f)) | |
587 | (nodes-by-level (compute-nodes-by-level dom-levels))) | |
588 | (let lp ((level (1- (vector-length nodes-by-level)))) | |
589 | (when (>= level 0) | |
590 | (for-each (lambda (n) | |
591 | (let ((edges (vector-ref back-edges n))) | |
592 | (unless (null? edges) | |
593 | (mark-loop-body n edges preds idoms loop-headers)))) | |
594 | (vector-ref nodes-by-level level)) | |
595 | (when (logbit? level irreducible-levels) | |
596 | (mark-irreducible-loops level idoms dom-levels loop-headers)) | |
597 | (lp (1- level)))) | |
598 | loop-headers)) | |
366eb4d7 | 599 | |
dda5fd94 AW |
600 | (define (analyze-dominators cfa) |
601 | (match cfa | |
602 | (($ $cfa k-map order preds) | |
603 | (let* ((idoms (compute-idoms preds)) | |
604 | (dom-levels (compute-dom-levels idoms)) | |
605 | (loop-headers (identify-loops preds idoms dom-levels))) | |
606 | (make-dominator-analysis cfa idoms dom-levels loop-headers #f))))) | |
3aee6cfd | 607 | |
db11440d AW |
608 | |
609 | ;; Compute the maximum fixed point of the data-flow constraint problem. | |
610 | ;; | |
611 | ;; This always completes, as the graph is finite and the in and out sets | |
612 | ;; are complete semi-lattices. If the graph is reducible and the blocks | |
613 | ;; are sorted in reverse post-order, this completes in a maximum of LC + | |
614 | ;; 2 iterations, where LC is the loop connectedness number. See Hecht | |
615 | ;; and Ullman, "Analysis of a simple algorithm for global flow | |
616 | ;; problems", POPL 1973, or the recent summary in "Notes on graph | |
617 | ;; algorithms used in optimizing compilers", Offner 2013. | |
618 | (define (compute-maximum-fixed-point preds inv outv killv genv union?) | |
619 | (define (bitvector-copy! dst src) | |
620 | (bitvector-fill! dst #f) | |
621 | (bit-set*! dst src #t)) | |
622 | (define (bitvector-meet! accum src) | |
623 | (bit-set*! accum src union?)) | |
624 | (let lp ((n 0) (changed? #f)) | |
625 | (cond | |
626 | ((< n (vector-length preds)) | |
627 | (let ((in (vector-ref inv n)) | |
628 | (out (vector-ref outv n)) | |
629 | (kill (vector-ref killv n)) | |
630 | (gen (vector-ref genv n))) | |
631 | (let ((out-count (or changed? (bit-count #t out)))) | |
632 | (for-each | |
633 | (lambda (pred) | |
634 | (bitvector-meet! in (vector-ref outv pred))) | |
635 | (vector-ref preds n)) | |
636 | (bitvector-copy! out in) | |
637 | (for-each (cut bitvector-set! out <> #f) kill) | |
638 | (for-each (cut bitvector-set! out <> #t) gen) | |
639 | (lp (1+ n) | |
640 | (or changed? (not (eqv? out-count (bit-count #t out)))))))) | |
641 | (changed? | |
642 | (lp 0 #f))))) | |
643 | ||
644 | ;; Data-flow analysis. | |
645 | (define-record-type $dfa | |
7c4977e6 | 646 | (make-dfa cfa min-var var-count in out) |
db11440d | 647 | dfa? |
f235f926 AW |
648 | ;; CFA, for its reverse-post-order numbering |
649 | (cfa dfa-cfa) | |
7c4977e6 AW |
650 | ;; Minimum var in this function. |
651 | (min-var dfa-min-var) | |
652 | ;; Minimum var in this function. | |
653 | (var-count dfa-var-count) | |
db11440d AW |
654 | ;; Vector of k-idx -> bitvector |
655 | (in dfa-in) | |
656 | ;; Vector of k-idx -> bitvector | |
657 | (out dfa-out)) | |
658 | ||
659 | (define (dfa-k-idx dfa k) | |
f235f926 | 660 | (cfa-k-idx (dfa-cfa dfa) k)) |
db11440d AW |
661 | |
662 | (define (dfa-k-sym dfa idx) | |
f235f926 | 663 | (cfa-k-sym (dfa-cfa dfa) idx)) |
db11440d AW |
664 | |
665 | (define (dfa-k-count dfa) | |
f235f926 | 666 | (cfa-k-count (dfa-cfa dfa))) |
db11440d AW |
667 | |
668 | (define (dfa-var-idx dfa var) | |
7c4977e6 AW |
669 | (let ((idx (- var (dfa-min-var dfa)))) |
670 | (unless (< -1 idx (dfa-var-count dfa)) | |
671 | (error "var out of range" var)) | |
672 | idx)) | |
db11440d | 673 | |
db11440d | 674 | (define (dfa-var-sym dfa idx) |
7c4977e6 AW |
675 | (unless (< -1 idx (dfa-var-count dfa)) |
676 | (error "idx out of range" idx)) | |
677 | (+ idx (dfa-min-var dfa))) | |
db11440d AW |
678 | |
679 | (define (dfa-k-in dfa idx) | |
680 | (vector-ref (dfa-in dfa) idx)) | |
681 | ||
682 | (define (dfa-k-out dfa idx) | |
683 | (vector-ref (dfa-out dfa) idx)) | |
684 | ||
5bff3125 | 685 | (define (compute-live-variables fun dfg) |
7c4977e6 AW |
686 | (unless (and (= (vector-length (dfg-uses dfg)) (dfg-var-count dfg)) |
687 | (= (vector-length (dfg-cont-table dfg)) (dfg-label-count dfg))) | |
688 | (error "function needs renumbering")) | |
689 | (let* ((min-var (dfg-min-var dfg)) | |
a8430ab1 | 690 | (nvars (dfg-var-count dfg)) |
16af91e8 | 691 | (cfa (analyze-reverse-control-flow fun dfg)) |
5e897908 AW |
692 | (usev (make-vector (cfa-k-count cfa) '())) |
693 | (defv (make-vector (cfa-k-count cfa) '())) | |
694 | (live-in (make-vector (cfa-k-count cfa) #f)) | |
695 | (live-out (make-vector (cfa-k-count cfa) #f))) | |
7c4977e6 AW |
696 | (define (var->idx var) (- var min-var)) |
697 | (define (idx->var idx) (+ idx min-var)) | |
698 | ||
699 | ;; Initialize defv and usev. | |
98c5b69f | 700 | (let ((defs (dfg-defs dfg)) |
7c4977e6 | 701 | (uses (dfg-uses dfg))) |
5e897908 | 702 | (let lp ((n 0)) |
98c5b69f AW |
703 | (when (< n (vector-length defs)) |
704 | (let ((def (vector-ref defs n))) | |
7c4977e6 AW |
705 | (unless def |
706 | (error "internal error -- var array not packed")) | |
707 | (for-each (lambda (def) | |
708 | (vector-push! defv (cfa-k-idx cfa def) n)) | |
709 | (lookup-predecessors def dfg)) | |
710 | (for-each (lambda (use) | |
711 | (vector-push! usev (cfa-k-idx cfa use) n)) | |
712 | (vector-ref uses n)) | |
713 | (lp (1+ n)))))) | |
5e897908 AW |
714 | |
715 | ;; Initialize live-in and live-out sets. | |
716 | (let lp ((n 0)) | |
717 | (when (< n (vector-length live-out)) | |
718 | (vector-set! live-in n (make-bitvector nvars #f)) | |
719 | (vector-set! live-out n (make-bitvector nvars #f)) | |
720 | (lp (1+ n)))) | |
721 | ||
722 | ;; Liveness is a reverse data-flow problem, so we give | |
723 | ;; compute-maximum-fixed-point a reversed graph, swapping in | |
724 | ;; for out, and usev for defv. Note that since we are using | |
725 | ;; a reverse CFA, cfa-preds are actually successors, and | |
726 | ;; continuation 0 is ktail. | |
727 | (compute-maximum-fixed-point (cfa-preds cfa) | |
728 | live-out live-in defv usev #t) | |
729 | ||
7c4977e6 | 730 | (make-dfa cfa min-var nvars live-in live-out))) |
db11440d AW |
731 | |
732 | (define (print-dfa dfa) | |
733 | (match dfa | |
7c4977e6 | 734 | (($ $dfa cfa min-var in out) |
db11440d AW |
735 | (define (print-var-set bv) |
736 | (let lp ((n 0)) | |
737 | (let ((n (bit-position #t bv n))) | |
738 | (when n | |
7c4977e6 | 739 | (format #t " ~A" (+ n min-var)) |
db11440d AW |
740 | (lp (1+ n)))))) |
741 | (let lp ((n 0)) | |
f235f926 AW |
742 | (when (< n (cfa-k-count cfa)) |
743 | (format #t "~A:\n" (cfa-k-sym cfa n)) | |
db11440d AW |
744 | (format #t " in:") |
745 | (print-var-set (vector-ref in n)) | |
746 | (newline) | |
747 | (format #t " out:") | |
748 | (print-var-set (vector-ref out n)) | |
749 | (newline) | |
750 | (lp (1+ n))))))) | |
751 | ||
4bf757b8 | 752 | (define (visit-fun fun conts preds defs uses scopes scope-levels |
5fc40391 | 753 | min-label min-var global?) |
cec43eb8 | 754 | (define (add-def! var def-k) |
98c5b69f | 755 | (vector-set! defs (- var min-var) def-k)) |
6e8ad823 | 756 | |
5e897908 | 757 | (define (add-use! var use-k) |
98c5b69f | 758 | (vector-push! uses (- var min-var) use-k)) |
6e8ad823 | 759 | |
f22979db AW |
760 | (define* (declare-block! label cont parent |
761 | #:optional (level | |
5fc40391 AW |
762 | (1+ (vector-ref |
763 | scope-levels | |
764 | (- parent min-label))))) | |
5e897908 | 765 | (vector-set! conts (- label min-label) cont) |
5fc40391 AW |
766 | (vector-set! scopes (- label min-label) parent) |
767 | (vector-set! scope-levels (- label min-label) level)) | |
f22979db AW |
768 | |
769 | (define (link-blocks! pred succ) | |
21d6d183 | 770 | (vector-push! preds (- succ min-label) pred)) |
6e8ad823 AW |
771 | |
772 | (define (visit exp exp-k) | |
cec43eb8 AW |
773 | (define (def! sym) |
774 | (add-def! sym exp-k)) | |
6e8ad823 AW |
775 | (define (use! sym) |
776 | (add-use! sym exp-k)) | |
3aee6cfd AW |
777 | (define (use-k! k) |
778 | (link-blocks! exp-k k)) | |
6e8ad823 AW |
779 | (define (recur exp) |
780 | (visit exp exp-k)) | |
781 | (match exp | |
6e422a35 | 782 | (($ $letk (($ $cont k cont) ...) body) |
6e8ad823 | 783 | ;; Set up recursive environment before visiting cont bodies. |
48c2a539 AW |
784 | (for-each/2 (lambda (cont k) |
785 | (declare-block! k cont exp-k)) | |
786 | cont k) | |
787 | (for-each/2 visit cont k) | |
6e8ad823 AW |
788 | (recur body)) |
789 | ||
790 | (($ $kargs names syms body) | |
cec43eb8 | 791 | (for-each def! syms) |
6e8ad823 AW |
792 | (recur body)) |
793 | ||
794 | (($ $kif kt kf) | |
f22979db AW |
795 | (use-k! kt) |
796 | (use-k! kf)) | |
6e8ad823 | 797 | |
36527695 | 798 | (($ $kreceive arity k) |
f22979db | 799 | (use-k! k)) |
6e8ad823 AW |
800 | |
801 | (($ $letrec names syms funs body) | |
802 | (unless global? | |
803 | (error "$letrec should not be present when building a local DFG")) | |
cec43eb8 | 804 | (for-each def! syms) |
5e897908 | 805 | (for-each |
4bf757b8 | 806 | (cut visit-fun <> conts preds defs uses scopes scope-levels |
5fc40391 | 807 | min-label min-var global?) |
5e897908 | 808 | funs) |
6e8ad823 AW |
809 | (visit body exp-k)) |
810 | ||
6e422a35 | 811 | (($ $continue k src exp) |
f22979db | 812 | (use-k! k) |
6e8ad823 | 813 | (match exp |
6e8ad823 AW |
814 | (($ $call proc args) |
815 | (use! proc) | |
816 | (for-each use! args)) | |
817 | ||
b3ae2b50 AW |
818 | (($ $callk k proc args) |
819 | (use! proc) | |
820 | (for-each use! args)) | |
821 | ||
6e8ad823 AW |
822 | (($ $primcall name args) |
823 | (for-each use! args)) | |
824 | ||
825 | (($ $values args) | |
826 | (for-each use! args)) | |
827 | ||
7ab76a83 | 828 | (($ $prompt escape? tag handler) |
6e8ad823 | 829 | (use! tag) |
146ce52d | 830 | (use-k! handler)) |
6e8ad823 AW |
831 | |
832 | (($ $fun) | |
833 | (when global? | |
4bf757b8 | 834 | (visit-fun exp conts preds defs uses scopes scope-levels |
5fc40391 | 835 | min-label min-var global?))) |
6e8ad823 AW |
836 | |
837 | (_ #f))))) | |
838 | ||
839 | (match fun | |
6e422a35 AW |
840 | (($ $fun src meta free |
841 | ($ $cont kentry | |
6e8ad823 | 842 | (and entry |
90dce16d | 843 | ($ $kentry self ($ $cont ktail tail) clause)))) |
f22979db | 844 | (declare-block! kentry entry #f 0) |
cec43eb8 | 845 | (add-def! self kentry) |
6e8ad823 | 846 | |
f22979db | 847 | (declare-block! ktail tail kentry) |
6e8ad823 | 848 | |
90dce16d AW |
849 | (let lp ((clause clause)) |
850 | (match clause | |
851 | (#f #t) | |
852 | (($ $cont kclause | |
853 | (and clause ($ $kclause arity ($ $cont kbody body) | |
854 | alternate))) | |
855 | (declare-block! kclause clause kentry) | |
856 | (link-blocks! kentry kclause) | |
6e8ad823 | 857 | |
90dce16d AW |
858 | (declare-block! kbody body kclause) |
859 | (link-blocks! kclause kbody) | |
6e8ad823 | 860 | |
90dce16d AW |
861 | (visit body kbody) |
862 | (lp alternate))))))) | |
6e8ad823 | 863 | |
5e897908 AW |
864 | (define (compute-label-and-var-ranges fun global?) |
865 | (define (min* a b) | |
866 | (if b (min a b) a)) | |
545274a0 AW |
867 | (define-syntax-rule (do-fold global?) |
868 | ((make-cont-folder global? | |
869 | min-label max-label label-count | |
870 | min-var max-var var-count) | |
871 | (lambda (label cont | |
872 | min-label max-label label-count | |
873 | min-var max-var var-count) | |
874 | (let ((min-label (min* label min-label)) | |
875 | (max-label (max label max-label))) | |
876 | (define (visit-letrec body min-var max-var var-count) | |
877 | (match body | |
878 | (($ $letk conts body) | |
879 | (visit-letrec body min-var max-var var-count)) | |
880 | (($ $letrec names vars funs body) | |
881 | (visit-letrec body | |
882 | (cond (min-var (fold min min-var vars)) | |
883 | ((pair? vars) (fold min (car vars) (cdr vars))) | |
884 | (else min-var)) | |
885 | (fold max max-var vars) | |
886 | (+ var-count (length vars)))) | |
887 | (($ $continue) (values min-var max-var var-count)))) | |
888 | (match cont | |
889 | (($ $kargs names vars body) | |
890 | (call-with-values | |
891 | (lambda () | |
892 | (if global? | |
893 | (visit-letrec body min-var max-var var-count) | |
894 | (values min-var max-var var-count))) | |
895 | (lambda (min-var max-var var-count) | |
896 | (values min-label max-label (1+ label-count) | |
897 | (cond (min-var (fold min min-var vars)) | |
898 | ((pair? vars) (fold min (car vars) (cdr vars))) | |
899 | (else min-var)) | |
900 | (fold max max-var vars) | |
901 | (+ var-count (length vars)))))) | |
902 | (($ $kentry self) | |
903 | (values min-label max-label (1+ label-count) | |
904 | (min* self min-var) (max self max-var) (1+ var-count))) | |
905 | (_ (values min-label max-label (1+ label-count) | |
906 | min-var max-var var-count))))) | |
907 | fun | |
908 | #f -1 0 #f -1 0)) | |
909 | (if global? | |
910 | (do-fold #t) | |
911 | (do-fold #f))) | |
5e897908 | 912 | |
6e8ad823 | 913 | (define* (compute-dfg fun #:key (global? #t)) |
5e897908 AW |
914 | (call-with-values (lambda () (compute-label-and-var-ranges fun global?)) |
915 | (lambda (min-label max-label label-count min-var max-var var-count) | |
916 | (when (or (zero? label-count) (zero? var-count)) | |
917 | (error "internal error (no vars or labels for fun?)")) | |
918 | (let* ((nlabels (- (1+ max-label) min-label)) | |
919 | (nvars (- (1+ max-var) min-var)) | |
920 | (conts (make-vector nlabels #f)) | |
21d6d183 | 921 | (preds (make-vector nlabels '())) |
98c5b69f | 922 | (defs (make-vector nvars #f)) |
5fc40391 AW |
923 | (uses (make-vector nvars '())) |
924 | (scopes (make-vector nlabels #f)) | |
925 | (scope-levels (make-vector nlabels #f))) | |
4bf757b8 | 926 | (visit-fun fun conts preds defs uses scopes scope-levels |
5fc40391 | 927 | min-label min-var global?) |
4bf757b8 | 928 | (make-dfg conts preds defs uses scopes scope-levels |
5e897908 AW |
929 | min-label label-count min-var var-count))))) |
930 | ||
f49e994b AW |
931 | (define (lookup-cont label dfg) |
932 | (let ((res (vector-ref (dfg-cont-table dfg) (- label (dfg-min-label dfg))))) | |
933 | (unless res | |
934 | (error "Unknown continuation!" label)) | |
935 | res)) | |
936 | ||
5fc40391 AW |
937 | (define (lookup-predecessors k dfg) |
938 | (vector-ref (dfg-preds dfg) (- k (dfg-min-label dfg)))) | |
939 | ||
940 | (define (lookup-successors k dfg) | |
2c3c086e AW |
941 | (let ((cont (vector-ref (dfg-cont-table dfg) (- k (dfg-min-label dfg))))) |
942 | (visit-cont-successors list cont))) | |
6e8ad823 | 943 | |
5e897908 | 944 | (define (lookup-def var dfg) |
f49e994b | 945 | (vector-ref (dfg-defs dfg) (- var (dfg-min-var dfg)))) |
6e8ad823 | 946 | |
5e897908 | 947 | (define (lookup-uses var dfg) |
f49e994b | 948 | (vector-ref (dfg-uses dfg) (- var (dfg-min-var dfg)))) |
6e8ad823 | 949 | |
c8ad7426 | 950 | (define (lookup-block-scope k dfg) |
5fc40391 | 951 | (vector-ref (dfg-scopes dfg) (- k (dfg-min-label dfg)))) |
f22979db | 952 | |
5fc40391 AW |
953 | (define (lookup-scope-level k dfg) |
954 | (vector-ref (dfg-scope-levels dfg) (- k (dfg-min-label dfg)))) | |
f22979db | 955 | |
6e8ad823 | 956 | (define (find-defining-term sym dfg) |
f22979db | 957 | (match (lookup-predecessors (lookup-def sym dfg) dfg) |
6e8ad823 | 958 | ((def-exp-k) |
fbdb69b2 | 959 | (lookup-cont def-exp-k dfg)) |
6e8ad823 AW |
960 | (else #f))) |
961 | ||
962 | (define (find-call term) | |
963 | (match term | |
964 | (($ $kargs names syms body) (find-call body)) | |
965 | (($ $letk conts body) (find-call body)) | |
966 | (($ $letrec names syms funs body) (find-call body)) | |
967 | (($ $continue) term))) | |
968 | ||
969 | (define (call-expression call) | |
970 | (match call | |
6e422a35 | 971 | (($ $continue k src exp) exp))) |
6e8ad823 AW |
972 | |
973 | (define (find-expression term) | |
974 | (call-expression (find-call term))) | |
975 | ||
976 | (define (find-defining-expression sym dfg) | |
977 | (match (find-defining-term sym dfg) | |
978 | (#f #f) | |
36527695 | 979 | (($ $kreceive) #f) |
f22979db | 980 | (($ $kclause) #f) |
6e8ad823 AW |
981 | (term (find-expression term)))) |
982 | ||
983 | (define (find-constant-value sym dfg) | |
984 | (match (find-defining-expression sym dfg) | |
985 | (($ $const val) | |
986 | (values #t val)) | |
6e422a35 | 987 | (($ $continue k src ($ $void)) |
6e8ad823 AW |
988 | (values #t *unspecified*)) |
989 | (else | |
990 | (values #f #f)))) | |
991 | ||
992 | (define (constant-needs-allocation? sym val dfg) | |
607fe5a6 AW |
993 | (define (immediate-u8? val) |
994 | (and (integer? val) (exact? val) (<= 0 val 255))) | |
995 | ||
6e8ad823 AW |
996 | (define (find-exp term) |
997 | (match term | |
998 | (($ $kargs names syms body) (find-exp body)) | |
999 | (($ $letk conts body) (find-exp body)) | |
1000 | (else term))) | |
f49e994b AW |
1001 | |
1002 | (or-map | |
1003 | (lambda (use) | |
1004 | (match (find-expression (lookup-cont use dfg)) | |
1005 | (($ $call) #f) | |
1006 | (($ $callk) #f) | |
1007 | (($ $values) #f) | |
1008 | (($ $primcall 'free-ref (closure slot)) | |
1009 | (not (eq? sym slot))) | |
1010 | (($ $primcall 'free-set! (closure slot value)) | |
1011 | (not (eq? sym slot))) | |
1012 | (($ $primcall 'cache-current-module! (mod . _)) | |
1013 | (eq? sym mod)) | |
1014 | (($ $primcall 'cached-toplevel-box _) | |
1015 | #f) | |
1016 | (($ $primcall 'cached-module-box _) | |
1017 | #f) | |
1018 | (($ $primcall 'resolve (name bound?)) | |
1019 | (eq? sym name)) | |
1020 | (($ $primcall 'make-vector/immediate (len init)) | |
1021 | (not (eq? sym len))) | |
1022 | (($ $primcall 'vector-ref/immediate (v i)) | |
1023 | (not (eq? sym i))) | |
1024 | (($ $primcall 'vector-set!/immediate (v i x)) | |
1025 | (not (eq? sym i))) | |
1026 | (($ $primcall 'allocate-struct/immediate (vtable nfields)) | |
1027 | (not (eq? sym nfields))) | |
1028 | (($ $primcall 'struct-ref/immediate (s n)) | |
1029 | (not (eq? sym n))) | |
1030 | (($ $primcall 'struct-set!/immediate (s n x)) | |
1031 | (not (eq? sym n))) | |
1032 | (($ $primcall 'builtin-ref (idx)) | |
1033 | #f) | |
1034 | (_ #t))) | |
1035 | (vector-ref (dfg-uses dfg) (- sym (dfg-min-var dfg))))) | |
6e8ad823 | 1036 | |
5e897908 AW |
1037 | (define (continuation-scope-contains? scope-k k dfg) |
1038 | (let ((scope-level (lookup-scope-level scope-k dfg))) | |
f22979db AW |
1039 | (let lp ((k k)) |
1040 | (or (eq? scope-k k) | |
5fc40391 AW |
1041 | (and (< scope-level (lookup-scope-level k dfg)) |
1042 | (lp (lookup-block-scope k dfg))))))) | |
f22979db | 1043 | |
f22979db | 1044 | (define (continuation-bound-in? k use-k dfg) |
21d6d183 | 1045 | (continuation-scope-contains? (lookup-block-scope k dfg) use-k dfg)) |
d51fb1e6 AW |
1046 | |
1047 | (define (variable-free-in? var k dfg) | |
5e897908 AW |
1048 | (or-map (lambda (use) |
1049 | (continuation-scope-contains? k use dfg)) | |
1050 | (lookup-uses var dfg))) | |
6e8ad823 | 1051 | |
e636f424 | 1052 | ;; A continuation is a control point if it has multiple predecessors, or |
a3a45279 | 1053 | ;; if its single predecessor does not have a single successor. |
e636f424 AW |
1054 | (define (control-point? k dfg) |
1055 | (match (lookup-predecessors k dfg) | |
1056 | ((pred) | |
2c3c086e AW |
1057 | (let ((cont (vector-ref (dfg-cont-table dfg) |
1058 | (- pred (dfg-min-label dfg))))) | |
1059 | (visit-cont-successors (case-lambda | |
1060 | (() #t) | |
1061 | ((succ0) #f) | |
1062 | ((succ1 succ2) #t)) | |
1063 | cont))) | |
e636f424 | 1064 | (_ #t))) |
6e8ad823 AW |
1065 | |
1066 | (define (lookup-bound-syms k dfg) | |
fbdb69b2 AW |
1067 | (match (lookup-cont k dfg) |
1068 | (($ $kargs names syms body) | |
1069 | syms))) |