Remove $kif
[bpt/guile.git] / module / language / cps / slot-allocation.scm
CommitLineData
6e8ad823
AW
1;;; Continuation-passing style (CPS) intermediate language (IL)
2
7ab76a83 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;;; 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
987c1f5f 34 lookup-maybe-slot
6e8ad823
AW
35 lookup-constant-value
36 lookup-maybe-constant-value
37 lookup-nlocals
38 lookup-call-proc-slot
02c624fc
AW
39 lookup-parallel-moves
40 lookup-dead-slot-map))
6e8ad823 41
6e8ad823 42(define-record-type $allocation
987c1f5f
AW
43 (make-allocation dfa slots
44 has-constv constant-values
45 call-allocations
46 nlocals)
6e8ad823 47 allocation?
987c1f5f
AW
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 ;;
02c624fc
AW
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.
987c1f5f 81 ;;
36527695 82 ;; $kreceive continuations record a proc slot and a set of return moves
987c1f5f
AW
83 ;; to adapt multiple values from the stack to local variables.
84 ;;
85 ;; Tail calls record arg moves, but no proc slot.
86 ;;
02c624fc
AW
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.
987c1f5f 91 ;;
36527695 92 ;; $prompt handlers are $kreceive continuations like any other.
987c1f5f
AW
93 ;;
94 ;; $values expressions with more than 1 value record moves but have no
02c624fc 95 ;; proc slot or dead slot map.
8d59d55e 96 ;;
987c1f5f
AW
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
02c624fc 99 ;; variable. A dead slot map is a bitfield, as an integer.
6e8ad823 100 ;;
987c1f5f
AW
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
02c624fc 108 (make-call-allocation proc-slot moves dead-slot-map)
987c1f5f
AW
109 call-allocation?
110 (proc-slot call-allocation-proc-slot)
02c624fc
AW
111 (moves call-allocation-moves)
112 (dead-slot-map call-allocation-dead-slot-map))
6e8ad823
AW
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
987c1f5f
AW
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)))))))
6e8ad823
AW
129 (if (or (zero? slot) (logbit? (1- slot) n))
130 slot
131 (lp (1- slot)))))
132
987c1f5f
AW
133(define (lookup-maybe-slot sym allocation)
134 (match allocation
135 (($ $allocation dfa slots)
136 (vector-ref slots (dfa-var-idx dfa sym)))))
6e8ad823
AW
137
138(define (lookup-slot sym allocation)
987c1f5f
AW
139 (or (lookup-maybe-slot sym allocation)
140 (error "Variable not allocated to a slot" sym)))
6e8ad823
AW
141
142(define (lookup-constant-value sym allocation)
987c1f5f
AW
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))))))
6e8ad823
AW
149
150(define (lookup-maybe-constant-value sym allocation)
987c1f5f
AW
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))))))
6e8ad823 156
987c1f5f
AW
157(define (lookup-call-allocation k allocation)
158 (or (hashq-ref (allocation-call-allocations allocation) k)
159 (error "Continuation not a call" k)))
6e8ad823 160
987c1f5f
AW
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)))
6e8ad823
AW
164
165(define (lookup-parallel-moves k allocation)
987c1f5f
AW
166 (or (call-allocation-moves (lookup-call-allocation k allocation))
167 (error "Call has no use parallel moves slot" k)))
168
02c624fc
AW
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
987c1f5f
AW
173(define (lookup-nlocals k allocation)
174 (or (hashq-ref (allocation-nlocals allocation) k)
175 (error "Not a clause continuation" k)))
6e8ad823
AW
176
177(define (solve-parallel-move src dst tmp)
178 "Solve the parallel move problem between src and dst slot lists, which
179are 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
863034a8
AW
226(define (dead-after-def? k-idx v-idx dfa)
227 (not (bitvector-ref (dfa-k-in dfa k-idx) v-idx)))
e636f424 228
863034a8
AW
229(define (dead-after-use? k-idx v-idx dfa)
230 (not (bitvector-ref (dfa-k-out dfa k-idx) v-idx)))
e636f424 231
d258fccc 232(define (allocate-slots fun dfg)
987c1f5f 233 (let* ((dfa (compute-live-variables fun dfg))
7dbf40ea
AW
234 (min-label (dfg-min-label dfg))
235 (label-count (dfg-label-count dfg))
236 (usev (make-vector label-count '()))
237 (defv (make-vector label-count '()))
987c1f5f
AW
238 (slots (make-vector (dfa-var-count dfa) #f))
239 (constant-values (make-vector (dfa-var-count dfa) #f))
240 (has-constv (make-bitvector (dfa-var-count dfa) #f))
241 (has-slotv (make-bitvector (dfa-var-count dfa) #t))
242 (needs-slotv (make-bitvector (dfa-var-count dfa) #t))
0c247a2f 243 (needs-hintv (make-bitvector (dfa-var-count dfa) #f))
987c1f5f
AW
244 (call-allocations (make-hash-table))
245 (nlocals 0) ; Mutable. It pains me.
246 (nlocals-table (make-hash-table)))
247
7dbf40ea
AW
248 (define (label->idx label) (- label min-label))
249 (define (idx->label idx) (+ idx min-label))
250
987c1f5f
AW
251 (define (bump-nlocals! nlocals*)
252 (when (< nlocals nlocals*)
253 (set! nlocals nlocals*)))
254
255 (define (empty-live-slots)
256 #b0)
257
258 (define (add-live-slot slot live-slots)
259 (logior live-slots (ash 1 slot)))
260
261 (define (kill-dead-slot slot live-slots)
262 (logand live-slots (lognot (ash 1 slot))))
263
264 (define (compute-slot live-slots hint)
d4b3a36d
AW
265 ;; Slots 253-255 are reserved for shuffling; see comments in
266 ;; assembler.scm.
267 (if (and hint (not (logbit? hint live-slots))
268 (or (< hint 253) (> hint 255)))
987c1f5f 269 hint
d4b3a36d
AW
270 (let ((slot (find-first-zero live-slots)))
271 (if (or (< slot 253) (> slot 255))
272 slot
273 (+ 256 (find-first-zero (ash live-slots -256)))))))
987c1f5f
AW
274
275 (define (compute-call-proc-slot live-slots)
f8085163 276 (+ 2 (find-first-trailing-zero live-slots)))
987c1f5f
AW
277
278 (define (compute-prompt-handler-proc-slot live-slots)
f5765cc2
AW
279 (if (zero? live-slots)
280 0
281 (1- (find-first-trailing-zero live-slots))))
987c1f5f
AW
282
283 (define (recompute-live-slots k nargs)
863034a8 284 (let ((in (dfa-k-in dfa (label->idx k))))
f5765cc2 285 (let lp ((v 0) (live-slots 0))
987c1f5f
AW
286 (let ((v (bit-position #t in v)))
287 (if v
288 (let ((slot (vector-ref slots v)))
289 (lp (1+ v)
290 (if slot
291 (add-live-slot slot live-slots)
292 live-slots)))
293 live-slots)))))
294
295 (define* (allocate! var-idx hint live)
296 (cond
297 ((not (bitvector-ref needs-slotv var-idx)) live)
298 ((vector-ref slots var-idx) => (cut add-live-slot <> live))
c79f873e 299 ((and (not hint) (bitvector-ref needs-hintv var-idx)) live)
987c1f5f
AW
300 (else
301 (let ((slot (compute-slot live hint)))
302 (bump-nlocals! (1+ slot))
303 (vector-set! slots var-idx slot)
304 (add-live-slot slot live)))))
305
306 ;; Although some parallel moves may proceed without a temporary
307 ;; slot, in general one is needed. That temporary slot must not be
308 ;; part of the source or destination sets, and that slot should not
309 ;; correspond to a live variable. Usually the source and
310 ;; destination sets are a subset of the union of the live sets
311 ;; before and after the move. However for stack slots that don't
312 ;; have names -- those slots that correspond to function arguments
313 ;; or to function return values -- it could be that they are out of
314 ;; the computed live set. In that case they need to be adjoined to
315 ;; the live set, used when choosing a temporary slot.
d4b3a36d
AW
316 ;;
317 ;; Note that although we reserve slots 253-255 for shuffling
318 ;; operands that address less than the full 24-bit range of locals,
319 ;; that reservation doesn't apply here, because this temporary
320 ;; itself is used while doing parallel assignment via "mov", and
321 ;; "mov" does not need shuffling.
987c1f5f
AW
322 (define (compute-tmp-slot live stack-slots)
323 (find-first-zero (fold add-live-slot live stack-slots)))
324
325 (define (parallel-move src-slots dst-slots tmp-slot)
326 (let ((moves (solve-parallel-move src-slots dst-slots tmp-slot)))
327 (when (assv tmp-slot moves)
328 (bump-nlocals! (1+ tmp-slot)))
329 moves))
330
331 ;; Find variables that are actually constant, and determine which
332 ;; of those can avoid slot allocation.
333 (define (compute-constants!)
334 (let lp ((n 0))
335 (when (< n (vector-length constant-values))
336 (let ((sym (dfa-var-sym dfa n)))
337 (call-with-values (lambda () (find-constant-value sym dfg))
338 (lambda (has-const? const)
339 (when has-const?
340 (bitvector-set! has-constv n has-const?)
341 (vector-set! constant-values n const)
342 (when (not (constant-needs-allocation? sym const dfg))
343 (bitvector-set! needs-slotv n #f)))
344 (lp (1+ n))))))))
345
346 ;; Record uses and defs, as lists of variable indexes, indexed by
7dbf40ea 347 ;; label index.
987c1f5f
AW
348 (define (compute-uses-and-defs!)
349 (let lp ((n 0))
350 (when (< n (vector-length usev))
7dbf40ea 351 (match (lookup-cont (idx->label n) dfg)
8320f504 352 (($ $kfun src meta self)
987c1f5f
AW
353 (vector-set! defv n (list (dfa-var-idx dfa self))))
354 (($ $kargs names syms body)
355 (vector-set! defv n (map (cut dfa-var-idx dfa <>) syms))
356 (vector-set! usev n
357 (map (cut dfa-var-idx dfa <>)
358 (match (find-expression body)
359 (($ $call proc args)
360 (cons proc args))
b3ae2b50
AW
361 (($ $callk k proc args)
362 (cons proc args))
987c1f5f
AW
363 (($ $primcall name args)
364 args)
92805e21
AW
365 (($ $branch kt ($ $primcall name args))
366 args)
367 (($ $branch kt ($ $values args))
368 args)
987c1f5f
AW
369 (($ $values args)
370 args)
7ab76a83 371 (($ $prompt escape? tag handler)
987c1f5f
AW
372 (list tag))
373 (_ '())))))
374 (_ #f))
375 (lp (1+ n)))))
376
fa48a2f7
AW
377 ;; Results of function calls that are not used don't need to be
378 ;; allocated to slots.
379 (define (compute-unused-results!)
7dbf40ea
AW
380 (define (kreceive-get-kargs kreceive)
381 (match (lookup-cont kreceive dfg)
382 (($ $kreceive arity kargs) kargs)
fa48a2f7 383 (_ #f)))
7dbf40ea 384 (let ((candidates (make-bitvector label-count #f)))
36527695 385 ;; Find all $kargs that are the successors of $kreceive nodes.
fa48a2f7 386 (let lp ((n 0))
7dbf40ea
AW
387 (when (< n label-count)
388 (and=> (kreceive-get-kargs (idx->label n))
fa48a2f7 389 (lambda (kargs)
7dbf40ea 390 (bitvector-set! candidates (label->idx kargs) #t)))
fa48a2f7 391 (lp (1+ n))))
36527695 392 ;; For $kargs that only have $kreceive predecessors, remove unused
fa48a2f7
AW
393 ;; variables from the needs-slotv set.
394 (let lp ((n 0))
395 (let ((n (bit-position #t candidates n)))
396 (when n
7dbf40ea 397 (match (lookup-predecessors (idx->label n) dfg)
36527695 398 ;; At least one kreceive is in the predecessor set, so we
fa48a2f7
AW
399 ;; only need to do the check for nodes with >1
400 ;; predecessor.
36527695 401 ((or (_) ((? kreceive-get-kargs) ...))
fa48a2f7 402 (for-each (lambda (var)
863034a8 403 (when (dead-after-def? n var dfa)
fa48a2f7
AW
404 (bitvector-set! needs-slotv var #f)))
405 (vector-ref defv n)))
406 (_ #f))
407 (lp (1+ n)))))))
408
0c247a2f
AW
409 ;; Compute the set of variables whose allocation should be delayed
410 ;; until a "hint" is known about where to allocate them. This is
411 ;; the case for some procedure arguments.
412 ;;
413 ;; This algorithm used is a conservative approximation of what
414 ;; really should happen, which would be eager allocation of call
415 ;; frames as soon as it's known that a call will happen. It would
416 ;; be nice to recast this as a proper data-flow problem.
417 (define (compute-needs-hint!)
0c247a2f 418 (define (live-before n)
863034a8 419 (dfa-k-in dfa n))
0c247a2f 420 (define (live-after n)
863034a8 421 (dfa-k-out dfa n))
0c247a2f
AW
422
423 ;; Walk backwards. At a call, compute the set of variables that
424 ;; have allocated slots and are live before but not after. This
425 ;; set contains candidates for needs-hintv.
426 (define (scan-for-call n)
427 (when (<= 0 n)
7dbf40ea 428 (match (lookup-cont (idx->label n) dfg)
0c247a2f
AW
429 (($ $kargs names syms body)
430 (match (find-expression body)
b3ae2b50 431 ((or ($ $call) ($ $callk))
0c247a2f
AW
432 (let ((args (make-bitvector (bitvector-length needs-slotv) #f)))
433 (bit-set*! args (live-before n) #t)
434 (bit-set*! args (live-after n) #f)
435 (bit-set*! args no-slot-needed #f)
436 (if (bit-position #t args 0)
437 (scan-for-hints (1- n) args)
438 (scan-for-call (1- n)))))
439 (_ (scan-for-call (1- n)))))
440 (_ (scan-for-call (1- n))))))
441
442 ;; Walk backwards in the current basic block. Stop when the block
443 ;; ends, we reach a call, or when an expression kills a value.
444 (define (scan-for-hints n args)
445 (when (< 0 n)
7dbf40ea 446 (match (lookup-cont (idx->label n) dfg)
0c247a2f 447 (($ $kargs names syms body)
7dbf40ea
AW
448 (match (lookup-predecessors (idx->label (1+ n)) dfg)
449 (((? (cut eqv? <> (idx->label n))))
0c247a2f
AW
450 ;; If we are indeed in the same basic block, then if we
451 ;; are finished with the scan, we kill uses of the
452 ;; terminator, but leave its definitions.
453 (match (find-expression body)
cf8bb037 454 ((or ($ $void) ($ $const) ($ $prim) ($ $closure)
f4092958
AW
455 ($ $primcall) ($ $prompt)
456 ;; If $values has more than one argument, it may
457 ;; use a temporary, which would invalidate our
458 ;; assumptions that slots not allocated are not
459 ;; used.
460 ($ $values (or () (_))))
0c247a2f
AW
461 (let ((dead (make-bitvector (bitvector-length args) #f)))
462 (bit-set*! dead (live-before n) #t)
463 (bit-set*! dead (live-after n) #f)
464 (bit-set*! dead no-slot-needed #f)
465 (if (bit-position #t dead 0)
466 (finish-hints n (live-before n) args)
467 (scan-for-hints (1- n) args))))
92805e21 468 ((or ($ $call) ($ $callk) ($ $values) ($ $branch))
0c247a2f
AW
469 (finish-hints n (live-before n) args))))
470 ;; Otherwise we kill uses of the block entry.
471 (_ (finish-hints n (live-before (1+ n)) args))))
472 (_ (finish-hints n (live-before (1+ n)) args)))))
473
474 ;; Add definitions ARGS minus KILL to NEED-HINTS, and go back to
475 ;; looking for calls.
476 (define (finish-hints n kill args)
477 (bit-invert! args)
478 (bit-set*! args kill #t)
479 (bit-invert! args)
480 (bit-set*! needs-hintv args #t)
481 (scan-for-call n))
482
483 (define no-slot-needed
484 (make-bitvector (bitvector-length needs-slotv) #f))
485
486 (bit-set*! no-slot-needed needs-slotv #t)
487 (bit-invert! no-slot-needed)
7dbf40ea 488 (scan-for-call (1- label-count)))
0c247a2f 489
987c1f5f 490 (define (allocate-call label k uses pre-live post-live)
7dbf40ea 491 (match (lookup-cont k dfg)
987c1f5f
AW
492 (($ $ktail)
493 (let* ((tail-nlocals (length uses))
494 (tail-slots (iota tail-nlocals))
0c247a2f 495 (pre-live (fold allocate! pre-live uses tail-slots))
987c1f5f
AW
496 (moves (parallel-move (map (cut vector-ref slots <>) uses)
497 tail-slots
498 (compute-tmp-slot pre-live tail-slots))))
499 (bump-nlocals! tail-nlocals)
500 (hashq-set! call-allocations label
02c624fc 501 (make-call-allocation #f moves #f))))
36527695 502 (($ $kreceive arity kargs)
987c1f5f
AW
503 (let* ((proc-slot (compute-call-proc-slot post-live))
504 (call-slots (map (cut + proc-slot <>) (iota (length uses))))
0c247a2f 505 (pre-live (fold allocate! pre-live uses call-slots))
987c1f5f
AW
506 (arg-moves (parallel-move (map (cut vector-ref slots <>) uses)
507 call-slots
508 (compute-tmp-slot pre-live
509 call-slots)))
7dbf40ea 510 (result-vars (vector-ref defv (label->idx kargs)))
987c1f5f
AW
511 (value-slots (map (cut + proc-slot 1 <>)
512 (iota (length result-vars))))
ad4f6be1
AW
513 ;; Shuffle the first result down to the lowest slot, and
514 ;; leave any remaining results where they are. This
515 ;; strikes a balance between avoiding shuffling,
516 ;; especially for unused extra values, and avoiding
517 ;; frame size growth due to sparse locals.
518 (result-live (match (cons result-vars value-slots)
519 ((() . ()) post-live)
520 (((var . vars) . (slot . slots))
521 (fold allocate!
522 (allocate! var #f post-live)
523 vars slots))))
987c1f5f 524 (result-slots (map (cut vector-ref slots <>) result-vars))
fa48a2f7
AW
525 ;; Filter out unused results.
526 (value-slots (filter-map (lambda (val result) (and result val))
527 value-slots result-slots))
528 (result-slots (filter (lambda (x) x) result-slots))
987c1f5f
AW
529 (result-moves (parallel-move value-slots
530 result-slots
531 (compute-tmp-slot result-live
02c624fc
AW
532 value-slots)))
533 (dead-slot-map (logand (1- (ash 1 (- proc-slot 2)))
534 (lognot post-live))))
987c1f5f
AW
535 (bump-nlocals! (+ proc-slot (length uses)))
536 (hashq-set! call-allocations label
02c624fc 537 (make-call-allocation proc-slot arg-moves dead-slot-map))
987c1f5f 538 (hashq-set! call-allocations k
02c624fc 539 (make-call-allocation proc-slot result-moves #f))))
987c1f5f 540
6e8ad823 541 (_
987c1f5f
AW
542 (let* ((proc-slot (compute-call-proc-slot post-live))
543 (call-slots (map (cut + proc-slot <>) (iota (length uses))))
0c247a2f 544 (pre-live (fold allocate! pre-live uses call-slots))
987c1f5f
AW
545 (arg-moves (parallel-move (map (cut vector-ref slots <>) uses)
546 call-slots
547 (compute-tmp-slot pre-live
548 call-slots))))
549 (bump-nlocals! (+ proc-slot (length uses)))
550 (hashq-set! call-allocations label
02c624fc 551 (make-call-allocation proc-slot arg-moves #f))))))
987c1f5f
AW
552
553 (define (allocate-values label k uses pre-live post-live)
7dbf40ea 554 (match (lookup-cont k dfg)
8a2d420f
AW
555 (($ $ktail)
556 (let* ((src-slots (map (cut vector-ref slots <>) uses))
557 (tail-nlocals (1+ (length uses)))
558 (dst-slots (cdr (iota tail-nlocals)))
559 (moves (parallel-move src-slots dst-slots
560 (compute-tmp-slot pre-live dst-slots))))
561 (bump-nlocals! tail-nlocals)
562 (hashq-set! call-allocations label
02c624fc 563 (make-call-allocation #f moves #f))))
8a2d420f
AW
564 (($ $kargs (_) (_))
565 ;; When there is only one value in play, we allow the dst to be
566 ;; hinted (see scan-for-hints). If the src doesn't have a
567 ;; slot, then the actual slot for the dst would end up being
568 ;; decided by the call that uses it. Because we don't know the
569 ;; slot, we can't really compute the parallel moves in that
570 ;; case, so just bail and rely on the bytecode emitter to
571 ;; handle the one-value case specially.
7dbf40ea 572 (match (cons uses (vector-ref defv (label->idx k)))
8a2d420f
AW
573 (((src) . (dst))
574 (allocate! dst (vector-ref slots src) post-live))))
575 (($ $kargs)
576 (let* ((src-slots (map (cut vector-ref slots <>) uses))
7dbf40ea 577 (dst-vars (vector-ref defv (label->idx k)))
8a2d420f
AW
578 (result-live (fold allocate! post-live dst-vars src-slots))
579 (dst-slots (map (cut vector-ref slots <>) dst-vars))
580 (moves (parallel-move src-slots dst-slots
581 (compute-tmp-slot (logior pre-live result-live)
582 '()))))
583 (hashq-set! call-allocations label
59258f7c 584 (make-call-allocation #f moves #f))))))
987c1f5f
AW
585
586 (define (allocate-prompt label k handler nargs)
7dbf40ea 587 (match (lookup-cont handler dfg)
36527695 588 (($ $kreceive arity kargs)
987c1f5f
AW
589 (let* ((handler-live (recompute-live-slots handler nargs))
590 (proc-slot (compute-prompt-handler-proc-slot handler-live))
7dbf40ea 591 (result-vars (vector-ref defv (label->idx kargs)))
987c1f5f
AW
592 (value-slots (map (cut + proc-slot 1 <>)
593 (iota (length result-vars))))
594 (result-live (fold allocate!
595 handler-live result-vars value-slots))
596 (result-slots (map (cut vector-ref slots <>) result-vars))
fa48a2f7
AW
597 ;; Filter out unused results.
598 (value-slots (filter-map (lambda (val result) (and result val))
599 value-slots result-slots))
600 (result-slots (filter (lambda (x) x) result-slots))
987c1f5f
AW
601 (moves (parallel-move value-slots
602 result-slots
603 (compute-tmp-slot result-live
604 value-slots))))
605 (bump-nlocals! (+ proc-slot 1 (length result-vars)))
606 (hashq-set! call-allocations handler
02c624fc 607 (make-call-allocation proc-slot moves #f))))))
987c1f5f
AW
608
609 (define (allocate-defs! n live)
610 (fold (cut allocate! <> #f <>) live (vector-ref defv n)))
611
612 ;; This traversal will visit definitions before uses, as
613 ;; definitions dominate uses and a block's dominator will appear
614 ;; before it, in reverse post-order.
615 (define (visit-clause n nargs live)
f5765cc2 616 (let lp ((n n) (live (recompute-live-slots (idx->label n) nargs)))
7dbf40ea 617 (define (kill-dead live vars-by-label-idx pred)
987c1f5f
AW
618 (fold (lambda (v live)
619 (let ((slot (vector-ref slots v)))
f5765cc2 620 (if (and slot (pred n v dfa))
987c1f5f
AW
621 (kill-dead-slot slot live)
622 live)))
623 live
7dbf40ea 624 (vector-ref vars-by-label-idx n)))
987c1f5f
AW
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?))
7dbf40ea 629 (if (= n label-count)
987c1f5f 630 n
7dbf40ea 631 (let* ((label (idx->label n))
987c1f5f
AW
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.
7dbf40ea 640 (match (lookup-cont (idx->label n) dfg)
987c1f5f
AW
641 (($ $kclause) n)
642 (($ $kargs names syms body)
643 (let ((uses (vector-ref usev n)))
644 (match (find-call body)
b3ae2b50 645 (($ $continue k src (or ($ $call) ($ $callk)))
987c1f5f
AW
646 (allocate-call label k uses live post-live))
647 (($ $continue k src ($ $primcall)) #t)
8a2d420f 648 (($ $continue k src ($ $values))
987c1f5f 649 (allocate-values label k uses live post-live))
7ab76a83 650 (($ $continue k src ($ $prompt escape? tag handler))
987c1f5f
AW
651 (allocate-prompt label k handler nargs))
652 (_ #f)))
653 (lp (1+ n) post-live))
59258f7c 654 ((or ($ $kreceive) ($ $ktail))
987c1f5f
AW
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)
7dbf40ea 662 (match (lookup-cont (idx->label n) dfg)
90dce16d 663 (($ $kclause arity ($ $cont kbody ($ $kargs names)) alternate)
7dbf40ea
AW
664 (unless (eq? (idx->label (1+ n)) kbody)
665 (error "Unexpected label order"))
0c247a2f
AW
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)))))))
7dbf40ea
AW
672 (hashq-set! nlocals-table (idx->label n) nlocals)
673 (when (< next label-count)
90dce16d
AW
674 (match alternate
675 (($ $cont kalt)
7dbf40ea 676 (unless (eq? kalt (idx->label next))
90dce16d 677 (error "Unexpected clause order"))))
0c247a2f 678 (visit-clauses next live))))))
7dbf40ea 679 (match (lookup-cont (idx->label 0) dfg)
8320f504 680 (($ $kfun src meta self)
987c1f5f
AW
681 (visit-clauses 1 (allocate-defs! 0 (empty-live-slots))))))
682
683 (compute-constants!)
684 (compute-uses-and-defs!)
fa48a2f7 685 (compute-unused-results!)
0c247a2f 686 (compute-needs-hint!)
987c1f5f
AW
687 (visit-entry)
688
689 (make-allocation dfa slots
690 has-constv constant-values
691 call-allocations
692 nlocals-table)))