Merge commit '45a28515c13348dfd18e53038ad63dd091a5a3c1'
[bpt/guile.git] / module / language / cps / slot-allocation.scm
1 ;;; Continuation-passing style (CPS) intermediate language (IL)
2
3 ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
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 ;;; A module to assign stack slots to variables in a CPS term.
22 ;;;
23 ;;; Code:
24
25 (define-module (language cps slot-allocation)
26 #:use-module (ice-9 match)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-9)
29 #:use-module (srfi srfi-26)
30 #:use-module (language cps)
31 #:use-module (language cps dfg)
32 #:export (allocate-slots
33 lookup-slot
34 lookup-maybe-slot
35 lookup-constant-value
36 lookup-maybe-constant-value
37 lookup-nlocals
38 lookup-call-proc-slot
39 lookup-parallel-moves
40 lookup-dead-slot-map))
41
42 (define-record-type $allocation
43 (make-allocation dfa slots
44 has-constv constant-values
45 call-allocations
46 nlocals)
47 allocation?
48
49 ;; A DFA records all variables bound in a function, and assigns them
50 ;; indices. The slot in which a variable is stored at runtime can be
51 ;; had by indexing into the SLOTS vector with the variable's index.
52 ;;
53 (dfa allocation-dfa)
54 (slots allocation-slots)
55
56 ;; Not all variables have slots allocated. Variables that are
57 ;; constant and that are only used by primcalls that can accept
58 ;; constants directly are not allocated to slots, and their SLOT value
59 ;; is false. Likewise constants that are only used by calls are not
60 ;; allocated into slots, to avoid needless copying. If a variable is
61 ;; constant, its constant value is set in the CONSTANT-VALUES vector
62 ;; and the corresponding bit in the HAS-CONSTV bitvector is set.
63 ;;
64 (has-constv allocation-has-constv)
65 (constant-values allocation-constant-values)
66
67 ;; Some continuations have additional associated information. This
68 ;; addition information is a /call allocation/. Call allocations
69 ;; record the way that functions are passed values, and how their
70 ;; return values are rebound to local variables.
71 ;;
72 ;; A call allocation contains three pieces of information: the call's
73 ;; /proc slot/, a set of /parallel moves/, and a /dead slot map/. The
74 ;; proc slot indicates the slot of a procedure in a procedure call, or
75 ;; where the procedure would be in a multiple-value return. The
76 ;; parallel moves shuffle locals into position for a call, or shuffle
77 ;; returned values back into place. Though they use the same slot,
78 ;; moves for a call are called "call moves", and moves to handle a
79 ;; return are "return moves". The dead slot map indicates, for a
80 ;; call, what slots should be ignored by GC when marking the frame.
81 ;;
82 ;; $kreceive continuations record a proc slot and a set of return moves
83 ;; to adapt multiple values from the stack to local variables.
84 ;;
85 ;; Tail calls record arg moves, but no proc slot.
86 ;;
87 ;; Non-tail calls record arg moves, a call slot, and a dead slot map.
88 ;; Multiple-valued returns will have an associated $kreceive
89 ;; continuation, which records the same proc slot, but has return
90 ;; moves and no dead slot map.
91 ;;
92 ;; $prompt handlers are $kreceive continuations like any other.
93 ;;
94 ;; $values expressions with more than 1 value record moves but have no
95 ;; proc slot or dead slot map.
96 ;;
97 ;; A set of moves is expressed as an ordered list of (SRC . DST)
98 ;; moves, where SRC and DST are slots. This may involve a temporary
99 ;; variable. A dead slot map is a bitfield, as an integer.
100 ;;
101 (call-allocations allocation-call-allocations)
102
103 ;; The number of locals for a $kclause.
104 ;;
105 (nlocals allocation-nlocals))
106
107 (define-record-type $call-allocation
108 (make-call-allocation proc-slot moves dead-slot-map)
109 call-allocation?
110 (proc-slot call-allocation-proc-slot)
111 (moves call-allocation-moves)
112 (dead-slot-map call-allocation-dead-slot-map))
113
114 (define (find-first-zero n)
115 ;; Naive implementation.
116 (let lp ((slot 0))
117 (if (logbit? slot n)
118 (lp (1+ slot))
119 slot)))
120
121 (define (find-first-trailing-zero n)
122 (let lp ((slot (let lp ((count 2))
123 (if (< n (ash 1 (1- count)))
124 count
125 ;; Grow upper bound slower than factor 2 to avoid
126 ;; needless bignum allocation on 32-bit systems
127 ;; when there are more than 16 locals.
128 (lp (+ count (ash count -1)))))))
129 (if (or (zero? slot) (logbit? (1- slot) n))
130 slot
131 (lp (1- slot)))))
132
133 (define (lookup-maybe-slot sym allocation)
134 (match allocation
135 (($ $allocation dfa slots)
136 (vector-ref slots (dfa-var-idx dfa sym)))))
137
138 (define (lookup-slot sym allocation)
139 (or (lookup-maybe-slot sym allocation)
140 (error "Variable not allocated to a slot" sym)))
141
142 (define (lookup-constant-value sym allocation)
143 (match allocation
144 (($ $allocation dfa slots has-constv constant-values)
145 (let ((idx (dfa-var-idx dfa sym)))
146 (if (bitvector-ref has-constv idx)
147 (vector-ref constant-values idx)
148 (error "Variable does not have constant value" sym))))))
149
150 (define (lookup-maybe-constant-value sym allocation)
151 (match allocation
152 (($ $allocation dfa slots has-constv constant-values)
153 (let ((idx (dfa-var-idx dfa sym)))
154 (values (bitvector-ref has-constv idx)
155 (vector-ref constant-values idx))))))
156
157 (define (lookup-call-allocation k allocation)
158 (or (hashq-ref (allocation-call-allocations allocation) k)
159 (error "Continuation not a call" k)))
160
161 (define (lookup-call-proc-slot k allocation)
162 (or (call-allocation-proc-slot (lookup-call-allocation k allocation))
163 (error "Call has no proc slot" k)))
164
165 (define (lookup-parallel-moves k allocation)
166 (or (call-allocation-moves (lookup-call-allocation k allocation))
167 (error "Call has no use parallel moves slot" k)))
168
169 (define (lookup-dead-slot-map k allocation)
170 (or (call-allocation-dead-slot-map (lookup-call-allocation k allocation))
171 (error "Call has no dead slot map" k)))
172
173 (define (lookup-nlocals k allocation)
174 (or (hashq-ref (allocation-nlocals allocation) k)
175 (error "Not a clause continuation" k)))
176
177 (define (solve-parallel-move src dst tmp)
178 "Solve the parallel move problem between src and dst slot lists, which
179 are comparable with eqv?. A tmp slot may be used."
180
181 ;; This algorithm is taken from: "Tilting at windmills with Coq:
182 ;; formal verification of a compilation algorithm for parallel moves"
183 ;; by Laurence Rideau, Bernard Paul Serpette, and Xavier Leroy
184 ;; <http://gallium.inria.fr/~xleroy/publi/parallel-move.pdf>
185
186 (define (split-move moves reg)
187 (let loop ((revhead '()) (tail moves))
188 (match tail
189 (((and s+d (s . d)) . rest)
190 (if (eqv? s reg)
191 (cons d (append-reverse revhead rest))
192 (loop (cons s+d revhead) rest)))
193 (_ #f))))
194
195 (define (replace-last-source reg moves)
196 (match moves
197 ((moves ... (s . d))
198 (append moves (list (cons reg d))))))
199
200 (let loop ((to-move (map cons src dst))
201 (being-moved '())
202 (moved '())
203 (last-source #f))
204 ;; 'last-source' should always be equivalent to:
205 ;; (and (pair? being-moved) (car (last being-moved)))
206 (match being-moved
207 (() (match to-move
208 (() (reverse moved))
209 (((and s+d (s . d)) . t1)
210 (if (or (eqv? s d) ; idempotent
211 (not s)) ; src is a constant and can be loaded directly
212 (loop t1 '() moved #f)
213 (loop t1 (list s+d) moved s)))))
214 (((and s+d (s . d)) . b)
215 (match (split-move to-move d)
216 ((r . t1) (loop t1 (acons d r being-moved) moved last-source))
217 (#f (match b
218 (() (loop to-move '() (cons s+d moved) #f))
219 (_ (if (eqv? d last-source)
220 (loop to-move
221 (replace-last-source tmp b)
222 (cons s+d (acons d tmp moved))
223 tmp)
224 (loop to-move b (cons s+d moved) last-source))))))))))
225
226 (define (dead-after-def? def-k v-idx dfa)
227 (let ((l (dfa-k-idx dfa def-k)))
228 (not (bitvector-ref (dfa-k-in dfa l) v-idx))))
229
230 (define (dead-after-use? use-k v-idx dfa)
231 (let ((l (dfa-k-idx dfa use-k)))
232 (not (bitvector-ref (dfa-k-out dfa l) v-idx))))
233
234 (define (allocate-slots fun dfg)
235 (let* ((dfa (compute-live-variables fun dfg))
236 (cfa (analyze-control-flow fun dfg))
237 (usev (make-vector (cfa-k-count cfa) '()))
238 (defv (make-vector (cfa-k-count cfa) '()))
239 (contv (make-vector (cfa-k-count cfa) #f))
240 (slots (make-vector (dfa-var-count dfa) #f))
241 (constant-values (make-vector (dfa-var-count dfa) #f))
242 (has-constv (make-bitvector (dfa-var-count dfa) #f))
243 (has-slotv (make-bitvector (dfa-var-count dfa) #t))
244 (needs-slotv (make-bitvector (dfa-var-count dfa) #t))
245 (needs-hintv (make-bitvector (dfa-var-count dfa) #f))
246 (call-allocations (make-hash-table))
247 (nlocals 0) ; Mutable. It pains me.
248 (nlocals-table (make-hash-table)))
249
250 (define (bump-nlocals! nlocals*)
251 (when (< nlocals nlocals*)
252 (set! nlocals nlocals*)))
253
254 (define (empty-live-slots)
255 #b0)
256
257 (define (add-live-slot slot live-slots)
258 (logior live-slots (ash 1 slot)))
259
260 (define (kill-dead-slot slot live-slots)
261 (logand live-slots (lognot (ash 1 slot))))
262
263 (define (compute-slot live-slots hint)
264 (if (and hint (not (logbit? hint live-slots)))
265 hint
266 (find-first-zero live-slots)))
267
268 (define (compute-call-proc-slot live-slots)
269 (+ 2 (find-first-trailing-zero live-slots)))
270
271 (define (compute-prompt-handler-proc-slot live-slots)
272 (1- (find-first-trailing-zero live-slots)))
273
274 (define (recompute-live-slots k nargs)
275 (let ((in (dfa-k-in dfa (dfa-k-idx dfa k))))
276 (let lp ((v 0) (live-slots (1- (ash 1 (1+ nargs)))))
277 (let ((v (bit-position #t in v)))
278 (if v
279 (let ((slot (vector-ref slots v)))
280 (lp (1+ v)
281 (if slot
282 (add-live-slot slot live-slots)
283 live-slots)))
284 live-slots)))))
285
286 (define* (allocate! var-idx hint live)
287 (cond
288 ((not (bitvector-ref needs-slotv var-idx)) live)
289 ((vector-ref slots var-idx) => (cut add-live-slot <> live))
290 ((and (not hint) (bitvector-ref needs-hintv var-idx)) live)
291 (else
292 (let ((slot (compute-slot live hint)))
293 (bump-nlocals! (1+ slot))
294 (vector-set! slots var-idx slot)
295 (add-live-slot slot live)))))
296
297 ;; Although some parallel moves may proceed without a temporary
298 ;; slot, in general one is needed. That temporary slot must not be
299 ;; part of the source or destination sets, and that slot should not
300 ;; correspond to a live variable. Usually the source and
301 ;; destination sets are a subset of the union of the live sets
302 ;; before and after the move. However for stack slots that don't
303 ;; have names -- those slots that correspond to function arguments
304 ;; or to function return values -- it could be that they are out of
305 ;; the computed live set. In that case they need to be adjoined to
306 ;; the live set, used when choosing a temporary slot.
307 (define (compute-tmp-slot live stack-slots)
308 (find-first-zero (fold add-live-slot live stack-slots)))
309
310 (define (parallel-move src-slots dst-slots tmp-slot)
311 (let ((moves (solve-parallel-move src-slots dst-slots tmp-slot)))
312 (when (assv tmp-slot moves)
313 (bump-nlocals! (1+ tmp-slot)))
314 moves))
315
316 ;; Find variables that are actually constant, and determine which
317 ;; of those can avoid slot allocation.
318 (define (compute-constants!)
319 (let lp ((n 0))
320 (when (< n (vector-length constant-values))
321 (let ((sym (dfa-var-sym dfa n)))
322 (call-with-values (lambda () (find-constant-value sym dfg))
323 (lambda (has-const? const)
324 (when has-const?
325 (bitvector-set! has-constv n has-const?)
326 (vector-set! constant-values n const)
327 (when (not (constant-needs-allocation? sym const dfg))
328 (bitvector-set! needs-slotv n #f)))
329 (lp (1+ n))))))))
330
331 ;; Transform the DFG's continuation table to a vector, for easy
332 ;; access.
333 (define (compute-conts!)
334 (let ((cont-table (dfg-cont-table dfg)))
335 (let lp ((n 0))
336 (when (< n (vector-length contv))
337 (vector-set! contv n (lookup-cont (cfa-k-sym cfa n) cont-table))
338 (lp (1+ n))))))
339
340 ;; Record uses and defs, as lists of variable indexes, indexed by
341 ;; CFA continuation index.
342 (define (compute-uses-and-defs!)
343 (let lp ((n 0))
344 (when (< n (vector-length usev))
345 (match (vector-ref contv n)
346 (($ $kentry self)
347 (vector-set! defv n (list (dfa-var-idx dfa self))))
348 (($ $kargs names syms body)
349 (vector-set! defv n (map (cut dfa-var-idx dfa <>) syms))
350 (vector-set! usev n
351 (map (cut dfa-var-idx dfa <>)
352 (match (find-expression body)
353 (($ $call proc args)
354 (cons proc args))
355 (($ $callk k proc args)
356 (cons proc args))
357 (($ $primcall name args)
358 args)
359 (($ $values args)
360 args)
361 (($ $prompt escape? tag handler)
362 (list tag))
363 (_ '())))))
364 (_ #f))
365 (lp (1+ n)))))
366
367 ;; Results of function calls that are not used don't need to be
368 ;; allocated to slots.
369 (define (compute-unused-results!)
370 (define (kreceive-get-kargs n)
371 (match (vector-ref contv n)
372 (($ $kreceive arity kargs) (cfa-k-idx cfa kargs))
373 (_ #f)))
374 (let ((candidates (make-bitvector (vector-length contv) #f)))
375 ;; Find all $kargs that are the successors of $kreceive nodes.
376 (let lp ((n 0))
377 (when (< n (vector-length contv))
378 (and=> (kreceive-get-kargs n)
379 (lambda (kargs)
380 (bitvector-set! candidates kargs #t)))
381 (lp (1+ n))))
382 ;; For $kargs that only have $kreceive predecessors, remove unused
383 ;; variables from the needs-slotv set.
384 (let lp ((n 0))
385 (let ((n (bit-position #t candidates n)))
386 (when n
387 (match (cfa-predecessors cfa n)
388 ;; At least one kreceive is in the predecessor set, so we
389 ;; only need to do the check for nodes with >1
390 ;; predecessor.
391 ((or (_) ((? kreceive-get-kargs) ...))
392 (for-each (lambda (var)
393 (when (dead-after-def? (cfa-k-sym cfa n) var dfa)
394 (bitvector-set! needs-slotv var #f)))
395 (vector-ref defv n)))
396 (_ #f))
397 (lp (1+ n)))))))
398
399 ;; Compute the set of variables whose allocation should be delayed
400 ;; until a "hint" is known about where to allocate them. This is
401 ;; the case for some procedure arguments.
402 ;;
403 ;; This algorithm used is a conservative approximation of what
404 ;; really should happen, which would be eager allocation of call
405 ;; frames as soon as it's known that a call will happen. It would
406 ;; be nice to recast this as a proper data-flow problem.
407 (define (compute-needs-hint!)
408 ;; We traverse the graph using reverse-post-order on a forward
409 ;; control-flow graph, but we did the live variable analysis in
410 ;; the opposite direction -- so the continuation numbers don't
411 ;; correspond. This helper adapts them.
412 (define (cfa-k-idx->dfa-k-idx n)
413 (dfa-k-idx dfa (cfa-k-sym cfa n)))
414
415 (define (live-before n)
416 (dfa-k-in dfa (cfa-k-idx->dfa-k-idx n)))
417 (define (live-after n)
418 (dfa-k-out dfa (cfa-k-idx->dfa-k-idx n)))
419
420 ;; Walk backwards. At a call, compute the set of variables that
421 ;; have allocated slots and are live before but not after. This
422 ;; set contains candidates for needs-hintv.
423 (define (scan-for-call n)
424 (when (<= 0 n)
425 (match (vector-ref contv n)
426 (($ $kargs names syms body)
427 (match (find-expression body)
428 ((or ($ $call) ($ $callk))
429 (let ((args (make-bitvector (bitvector-length needs-slotv) #f)))
430 (bit-set*! args (live-before n) #t)
431 (bit-set*! args (live-after n) #f)
432 (bit-set*! args no-slot-needed #f)
433 (if (bit-position #t args 0)
434 (scan-for-hints (1- n) args)
435 (scan-for-call (1- n)))))
436 (_ (scan-for-call (1- n)))))
437 (_ (scan-for-call (1- n))))))
438
439 ;; Walk backwards in the current basic block. Stop when the block
440 ;; ends, we reach a call, or when an expression kills a value.
441 (define (scan-for-hints n args)
442 (when (< 0 n)
443 (match (vector-ref contv n)
444 (($ $kargs names syms body)
445 (match (cfa-predecessors cfa (1+ n))
446 (((? (cut eqv? <> n)))
447 ;; If we are indeed in the same basic block, then if we
448 ;; are finished with the scan, we kill uses of the
449 ;; terminator, but leave its definitions.
450 (match (find-expression body)
451 ((or ($ $void) ($ $const) ($ $prim) ($ $fun)
452 ($ $primcall) ($ $prompt)
453 ;; If $values has more than one argument, it may
454 ;; use a temporary, which would invalidate our
455 ;; assumptions that slots not allocated are not
456 ;; used.
457 ($ $values (or () (_))))
458 (let ((dead (make-bitvector (bitvector-length args) #f)))
459 (bit-set*! dead (live-before n) #t)
460 (bit-set*! dead (live-after n) #f)
461 (bit-set*! dead no-slot-needed #f)
462 (if (bit-position #t dead 0)
463 (finish-hints n (live-before n) args)
464 (scan-for-hints (1- n) args))))
465 ((or ($ $call) ($ $callk) ($ $values))
466 (finish-hints n (live-before n) args))))
467 ;; Otherwise we kill uses of the block entry.
468 (_ (finish-hints n (live-before (1+ n)) args))))
469 (_ (finish-hints n (live-before (1+ n)) args)))))
470
471 ;; Add definitions ARGS minus KILL to NEED-HINTS, and go back to
472 ;; looking for calls.
473 (define (finish-hints n kill args)
474 (bit-invert! args)
475 (bit-set*! args kill #t)
476 (bit-invert! args)
477 (bit-set*! needs-hintv args #t)
478 (scan-for-call n))
479
480 (define no-slot-needed
481 (make-bitvector (bitvector-length needs-slotv) #f))
482
483 (bit-set*! no-slot-needed needs-slotv #t)
484 (bit-invert! no-slot-needed)
485 (scan-for-call (1- (vector-length contv))))
486
487 (define (allocate-call label k uses pre-live post-live)
488 (match (vector-ref contv (cfa-k-idx cfa k))
489 (($ $ktail)
490 (let* ((tail-nlocals (length uses))
491 (tail-slots (iota tail-nlocals))
492 (pre-live (fold allocate! pre-live uses tail-slots))
493 (moves (parallel-move (map (cut vector-ref slots <>) uses)
494 tail-slots
495 (compute-tmp-slot pre-live tail-slots))))
496 (bump-nlocals! tail-nlocals)
497 (hashq-set! call-allocations label
498 (make-call-allocation #f moves #f))))
499 (($ $kreceive arity kargs)
500 (let* ((proc-slot (compute-call-proc-slot post-live))
501 (call-slots (map (cut + proc-slot <>) (iota (length uses))))
502 (pre-live (fold allocate! pre-live uses call-slots))
503 (arg-moves (parallel-move (map (cut vector-ref slots <>) uses)
504 call-slots
505 (compute-tmp-slot pre-live
506 call-slots)))
507 (result-vars (vector-ref defv (cfa-k-idx cfa kargs)))
508 (value-slots (map (cut + proc-slot 1 <>)
509 (iota (length result-vars))))
510 ;; Shuffle the first result down to the lowest slot, and
511 ;; leave any remaining results where they are. This
512 ;; strikes a balance between avoiding shuffling,
513 ;; especially for unused extra values, and avoiding
514 ;; frame size growth due to sparse locals.
515 (result-live (match (cons result-vars value-slots)
516 ((() . ()) post-live)
517 (((var . vars) . (slot . slots))
518 (fold allocate!
519 (allocate! var #f post-live)
520 vars slots))))
521 (result-slots (map (cut vector-ref slots <>) result-vars))
522 ;; Filter out unused results.
523 (value-slots (filter-map (lambda (val result) (and result val))
524 value-slots result-slots))
525 (result-slots (filter (lambda (x) x) result-slots))
526 (result-moves (parallel-move value-slots
527 result-slots
528 (compute-tmp-slot result-live
529 value-slots)))
530 (dead-slot-map (logand (1- (ash 1 (- proc-slot 2)))
531 (lognot post-live))))
532 (bump-nlocals! (+ proc-slot (length uses)))
533 (hashq-set! call-allocations label
534 (make-call-allocation proc-slot arg-moves dead-slot-map))
535 (hashq-set! call-allocations k
536 (make-call-allocation proc-slot result-moves #f))))
537
538 (_
539 (let* ((proc-slot (compute-call-proc-slot post-live))
540 (call-slots (map (cut + proc-slot <>) (iota (length uses))))
541 (pre-live (fold allocate! pre-live uses call-slots))
542 (arg-moves (parallel-move (map (cut vector-ref slots <>) uses)
543 call-slots
544 (compute-tmp-slot pre-live
545 call-slots))))
546 (bump-nlocals! (+ proc-slot (length uses)))
547 (hashq-set! call-allocations label
548 (make-call-allocation proc-slot arg-moves #f))))))
549
550 (define (allocate-values label k uses pre-live post-live)
551 (match (vector-ref contv (cfa-k-idx cfa k))
552 (($ $ktail)
553 (let* ((src-slots (map (cut vector-ref slots <>) uses))
554 (tail-nlocals (1+ (length uses)))
555 (dst-slots (cdr (iota tail-nlocals)))
556 (moves (parallel-move src-slots dst-slots
557 (compute-tmp-slot pre-live dst-slots))))
558 (bump-nlocals! tail-nlocals)
559 (hashq-set! call-allocations label
560 (make-call-allocation #f moves #f))))
561 (($ $kargs (_) (_))
562 ;; When there is only one value in play, we allow the dst to be
563 ;; hinted (see scan-for-hints). If the src doesn't have a
564 ;; slot, then the actual slot for the dst would end up being
565 ;; decided by the call that uses it. Because we don't know the
566 ;; slot, we can't really compute the parallel moves in that
567 ;; case, so just bail and rely on the bytecode emitter to
568 ;; handle the one-value case specially.
569 (match (cons uses (vector-ref defv (cfa-k-idx cfa k)))
570 (((src) . (dst))
571 (allocate! dst (vector-ref slots src) post-live))))
572 (($ $kargs)
573 (let* ((src-slots (map (cut vector-ref slots <>) uses))
574 (dst-vars (vector-ref defv (cfa-k-idx cfa k)))
575 (result-live (fold allocate! post-live dst-vars src-slots))
576 (dst-slots (map (cut vector-ref slots <>) dst-vars))
577 (moves (parallel-move src-slots dst-slots
578 (compute-tmp-slot (logior pre-live result-live)
579 '()))))
580 (hashq-set! call-allocations label
581 (make-call-allocation #f moves #f))))
582 (($ $kif) #f)))
583
584 (define (allocate-prompt label k handler nargs)
585 (match (vector-ref contv (cfa-k-idx cfa handler))
586 (($ $kreceive arity kargs)
587 (let* ((handler-live (recompute-live-slots handler nargs))
588 (proc-slot (compute-prompt-handler-proc-slot handler-live))
589 (result-vars (vector-ref defv (cfa-k-idx cfa kargs)))
590 (value-slots (map (cut + proc-slot 1 <>)
591 (iota (length result-vars))))
592 (result-live (fold allocate!
593 handler-live result-vars value-slots))
594 (result-slots (map (cut vector-ref slots <>) result-vars))
595 ;; Filter out unused results.
596 (value-slots (filter-map (lambda (val result) (and result val))
597 value-slots result-slots))
598 (result-slots (filter (lambda (x) x) result-slots))
599 (moves (parallel-move value-slots
600 result-slots
601 (compute-tmp-slot result-live
602 value-slots))))
603 (bump-nlocals! (+ proc-slot 1 (length result-vars)))
604 (hashq-set! call-allocations handler
605 (make-call-allocation proc-slot moves #f))))))
606
607 (define (allocate-defs! n live)
608 (fold (cut allocate! <> #f <>) live (vector-ref defv n)))
609
610 ;; This traversal will visit definitions before uses, as
611 ;; definitions dominate uses and a block's dominator will appear
612 ;; before it, in reverse post-order.
613 (define (visit-clause n nargs live)
614 (let lp ((n n) (live live))
615 (define (kill-dead live vars-by-cfa-idx pred)
616 (fold (lambda (v live)
617 (let ((slot (vector-ref slots v)))
618 (if (and slot
619 (> slot nargs)
620 (pred (cfa-k-sym cfa n) v dfa))
621 (kill-dead-slot slot live)
622 live)))
623 live
624 (vector-ref vars-by-cfa-idx n)))
625 (define (kill-dead-defs live)
626 (kill-dead live defv dead-after-def?))
627 (define (kill-dead-uses live)
628 (kill-dead live usev dead-after-use?))
629 (if (= n (cfa-k-count cfa))
630 n
631 (let* ((label (cfa-k-sym cfa n))
632 (live (if (control-point? label dfg)
633 (recompute-live-slots label nargs)
634 live))
635 (live (kill-dead-defs (allocate-defs! n live)))
636 (post-live (kill-dead-uses live)))
637 ;; LIVE are the live slots coming into the term.
638 ;; POST-LIVE is the subset that is still live after the
639 ;; term uses its inputs.
640 (match (vector-ref contv n)
641 (($ $kclause) n)
642 (($ $kargs names syms body)
643 (let ((uses (vector-ref usev n)))
644 (match (find-call body)
645 (($ $continue k src (or ($ $call) ($ $callk)))
646 (allocate-call label k uses live post-live))
647 (($ $continue k src ($ $primcall)) #t)
648 (($ $continue k src ($ $values))
649 (allocate-values label k uses live post-live))
650 (($ $continue k src ($ $prompt escape? tag handler))
651 (allocate-prompt label k handler nargs))
652 (_ #f)))
653 (lp (1+ n) post-live))
654 ((or ($ $kreceive) ($ $kif) ($ $ktail))
655 (lp (1+ n) post-live)))))))
656
657 (define (visit-entry)
658 (define (visit-clauses n live)
659 (unless (eqv? live (add-live-slot 0 (empty-live-slots)))
660 (error "Unexpected clause live set"))
661 (set! nlocals 1)
662 (match (vector-ref contv n)
663 (($ $kclause arity ($ $cont kbody ($ $kargs names)))
664 (unless (eq? (cfa-k-sym cfa (1+ n)) kbody)
665 (error "Unexpected CFA order"))
666 (let* ((nargs (length names))
667 (next (visit-clause (1+ n)
668 nargs
669 (fold allocate! live
670 (vector-ref defv (1+ n))
671 (cdr (iota (1+ nargs)))))))
672 (hashq-set! nlocals-table (cfa-k-sym cfa n) nlocals)
673 (when (< next (cfa-k-count cfa))
674 (visit-clauses next live))))))
675 (match (vector-ref contv 0)
676 (($ $kentry self)
677 (visit-clauses 1 (allocate-defs! 0 (empty-live-slots))))))
678
679 (compute-conts!)
680 (compute-constants!)
681 (compute-uses-and-defs!)
682 (compute-unused-results!)
683 (compute-needs-hint!)
684 (visit-entry)
685
686 (make-allocation dfa slots
687 has-constv constant-values
688 call-allocations
689 nlocals-table)))