Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / module / language / tree-il / peval.scm
1 ;;; Tree-IL partial evaluator
2
3 ;; Copyright (C) 2011, 2012 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 (define-module (language tree-il peval)
20 #:use-module (language tree-il)
21 #:use-module (language tree-il primitives)
22 #:use-module (language tree-il effects)
23 #:use-module (ice-9 vlist)
24 #:use-module (ice-9 match)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-9)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-26)
29 #:export (peval))
30
31 ;;;
32 ;;; Partial evaluation is Guile's most important source-to-source
33 ;;; optimization pass. It performs copy propagation, dead code
34 ;;; elimination, inlining, and constant folding, all while preserving
35 ;;; the order of effects in the residual program.
36 ;;;
37 ;;; For more on partial evaluation, see William Cook’s excellent
38 ;;; tutorial on partial evaluation at DSL 2011, called “Build your own
39 ;;; partial evaluator in 90 minutes”[0].
40 ;;;
41 ;;; Our implementation of this algorithm was heavily influenced by
42 ;;; Waddell and Dybvig's paper, "Fast and Effective Procedure Inlining",
43 ;;; IU CS Dept. TR 484.
44 ;;;
45 ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
46 ;;;
47
48 ;; First, some helpers.
49 ;;
50 (define-syntax *logging* (identifier-syntax #f))
51
52 ;; For efficiency we define *logging* to inline to #f, so that the call
53 ;; to log* gets optimized out. If you want to log, uncomment these
54 ;; lines:
55 ;;
56 ;; (define %logging #f)
57 ;; (define-syntax *logging* (identifier-syntax %logging))
58 ;;
59 ;; Then you can change %logging at runtime.
60
61 (define-syntax log
62 (syntax-rules (quote)
63 ((log 'event arg ...)
64 (if (and *logging*
65 (or (eq? *logging* #t)
66 (memq 'event *logging*)))
67 (log* 'event arg ...)))))
68
69 (define (log* event . args)
70 (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
71 'pretty-print)))
72 (pp `(log ,event . ,args))
73 (newline)
74 (values)))
75
76 (define-syntax-rule (let/ec k e e* ...)
77 (let ((tag (make-prompt-tag)))
78 (call-with-prompt
79 tag
80 (lambda ()
81 (let ((k (lambda args (apply abort-to-prompt tag args))))
82 e e* ...))
83 (lambda (_ res) res))))
84
85 (define (tree-il-any proc exp)
86 (let/ec k
87 (tree-il-fold (lambda (exp res)
88 (let ((res (proc exp)))
89 (if res (k res) #f)))
90 (lambda (exp res)
91 (let ((res (proc exp)))
92 (if res (k res) #f)))
93 (lambda (exp res) #f)
94 #f exp)))
95
96 (define (vlist-any proc vlist)
97 (let ((len (vlist-length vlist)))
98 (let lp ((i 0))
99 (and (< i len)
100 (or (proc (vlist-ref vlist i))
101 (lp (1+ i)))))))
102
103 (define (singly-valued-expression? exp)
104 (match exp
105 (($ <const>) #t)
106 (($ <lexical-ref>) #t)
107 (($ <void>) #t)
108 (($ <lexical-ref>) #t)
109 (($ <primitive-ref>) #t)
110 (($ <module-ref>) #t)
111 (($ <toplevel-ref>) #t)
112 (($ <primcall> _ (? singly-valued-primitive?)) #t)
113 (($ <primcall> _ 'values (val)) #t)
114 (($ <lambda>) #t)
115 (else #f)))
116
117 (define (truncate-values x)
118 "Discard all but the first value of X."
119 (if (singly-valued-expression? x)
120 x
121 (make-primcall (tree-il-src x) 'values (list x))))
122
123 ;; Peval will do a one-pass analysis on the source program to determine
124 ;; the set of assigned lexicals, and to identify unreferenced and
125 ;; singly-referenced lexicals.
126 ;;
127 (define-record-type <var>
128 (make-var name gensym refcount set?)
129 var?
130 (name var-name)
131 (gensym var-gensym)
132 (refcount var-refcount set-var-refcount!)
133 (set? var-set? set-var-set?!))
134
135 (define* (build-var-table exp #:optional (table vlist-null))
136 (tree-il-fold
137 (lambda (exp res)
138 (match exp
139 (($ <lexical-ref> src name gensym)
140 (let ((var (cdr (vhash-assq gensym res))))
141 (set-var-refcount! var (1+ (var-refcount var)))
142 res))
143 (_ res)))
144 (lambda (exp res)
145 (match exp
146 (($ <lambda-case> src req opt rest kw init gensyms body alt)
147 (fold (lambda (name sym res)
148 (vhash-consq sym (make-var name sym 0 #f) res))
149 res
150 (append req (or opt '()) (if rest (list rest) '())
151 (match kw
152 ((aok? (kw name sym) ...) name)
153 (_ '())))
154 gensyms))
155 (($ <let> src names gensyms vals body)
156 (fold (lambda (name sym res)
157 (vhash-consq sym (make-var name sym 0 #f) res))
158 res names gensyms))
159 (($ <letrec> src in-order? names gensyms vals body)
160 (fold (lambda (name sym res)
161 (vhash-consq sym (make-var name sym 0 #f) res))
162 res names gensyms))
163 (($ <fix> src names gensyms vals body)
164 (fold (lambda (name sym res)
165 (vhash-consq sym (make-var name sym 0 #f) res))
166 res names gensyms))
167 (($ <lexical-set> src name gensym exp)
168 (set-var-set?! (cdr (vhash-assq gensym res)) #t)
169 res)
170 (_ res)))
171 (lambda (exp res) res)
172 table exp))
173
174 ;; Counters are data structures used to limit the effort that peval
175 ;; spends on particular inlining attempts. Each call site in the source
176 ;; program is allocated some amount of effort. If peval exceeds the
177 ;; effort counter while attempting to inline a call site, it aborts the
178 ;; inlining attempt and residualizes a call instead.
179 ;;
180 ;; As there is a fixed number of call sites, that makes `peval' O(N) in
181 ;; the number of call sites in the source program.
182 ;;
183 ;; Counters should limit the size of the residual program as well, but
184 ;; currently this is not implemented.
185 ;;
186 ;; At the top level, before seeing any peval call, there is no counter,
187 ;; because inlining will terminate as there is no recursion. When peval
188 ;; sees a call at the top level, it will make a new counter, allocating
189 ;; it some amount of effort and size.
190 ;;
191 ;; This top-level effort counter effectively "prints money". Within a
192 ;; toplevel counter, no more effort is printed ex nihilo; for a nested
193 ;; inlining attempt to proceed, effort must be transferred from the
194 ;; toplevel counter to the nested counter.
195 ;;
196 ;; Via `data' and `prev', counters form a linked list, terminating in a
197 ;; toplevel counter. In practice `data' will be the a pointer to the
198 ;; source expression of the procedure being inlined.
199 ;;
200 ;; In this way peval can detect a recursive inlining attempt, by walking
201 ;; back on the `prev' links looking for matching `data'. Recursive
202 ;; counters receive a more limited effort allocation, as we don't want
203 ;; to spend all of the effort for a toplevel inlining site on loops.
204 ;; Also, recursive counters don't need a prompt at each inlining site:
205 ;; either the call chain folds entirely, or it will be residualized at
206 ;; its original call.
207 ;;
208 (define-record-type <counter>
209 (%make-counter effort size continuation recursive? data prev)
210 counter?
211 (effort effort-counter)
212 (size size-counter)
213 (continuation counter-continuation)
214 (recursive? counter-recursive? set-counter-recursive?!)
215 (data counter-data)
216 (prev counter-prev))
217
218 (define (abort-counter c)
219 ((counter-continuation c)))
220
221 (define (record-effort! c)
222 (let ((e (effort-counter c)))
223 (if (zero? (variable-ref e))
224 (abort-counter c)
225 (variable-set! e (1- (variable-ref e))))))
226
227 (define (record-size! c)
228 (let ((s (size-counter c)))
229 (if (zero? (variable-ref s))
230 (abort-counter c)
231 (variable-set! s (1- (variable-ref s))))))
232
233 (define (find-counter data counter)
234 (and counter
235 (if (eq? data (counter-data counter))
236 counter
237 (find-counter data (counter-prev counter)))))
238
239 (define* (transfer! from to #:optional
240 (effort (variable-ref (effort-counter from)))
241 (size (variable-ref (size-counter from))))
242 (define (transfer-counter! from-v to-v amount)
243 (let* ((from-balance (variable-ref from-v))
244 (to-balance (variable-ref to-v))
245 (amount (min amount from-balance)))
246 (variable-set! from-v (- from-balance amount))
247 (variable-set! to-v (+ to-balance amount))))
248
249 (transfer-counter! (effort-counter from) (effort-counter to) effort)
250 (transfer-counter! (size-counter from) (size-counter to) size))
251
252 (define (make-top-counter effort-limit size-limit continuation data)
253 (%make-counter (make-variable effort-limit)
254 (make-variable size-limit)
255 continuation
256 #t
257 data
258 #f))
259
260 (define (make-nested-counter continuation data current)
261 (let ((c (%make-counter (make-variable 0)
262 (make-variable 0)
263 continuation
264 #f
265 data
266 current)))
267 (transfer! current c)
268 c))
269
270 (define (make-recursive-counter effort-limit size-limit orig current)
271 (let ((c (%make-counter (make-variable 0)
272 (make-variable 0)
273 (counter-continuation orig)
274 #t
275 (counter-data orig)
276 current)))
277 (transfer! current c effort-limit size-limit)
278 c))
279
280 ;; Operand structures allow bindings to be processed lazily instead of
281 ;; eagerly. By doing so, hopefully we can get process them in a way
282 ;; appropriate to their use contexts. Operands also prevent values from
283 ;; being visited multiple times, wasting effort.
284 ;;
285 ;; TODO: Record value size in operand structure?
286 ;;
287 (define-record-type <operand>
288 (%make-operand var sym visit source visit-count use-count
289 copyable? residual-value constant-value alias-value)
290 operand?
291 (var operand-var)
292 (sym operand-sym)
293 (visit %operand-visit)
294 (source operand-source)
295 (visit-count operand-visit-count set-operand-visit-count!)
296 (use-count operand-use-count set-operand-use-count!)
297 (copyable? operand-copyable? set-operand-copyable?!)
298 (residual-value operand-residual-value %set-operand-residual-value!)
299 (constant-value operand-constant-value set-operand-constant-value!)
300 (alias-value operand-alias-value set-operand-alias-value!))
301
302 (define* (make-operand var sym #:optional source visit alias)
303 ;; Bind SYM to VAR, with value SOURCE. Unassigned bound operands are
304 ;; considered copyable until we prove otherwise. If we have a source
305 ;; expression, truncate it to one value. Copy propagation does not
306 ;; work on multiply-valued expressions.
307 (let ((source (and=> source truncate-values)))
308 (%make-operand var sym visit source 0 0
309 (and source (not (var-set? var))) #f #f
310 (and (not (var-set? var)) alias))))
311
312 (define* (make-bound-operands vars syms sources visit #:optional aliases)
313 (if aliases
314 (map (lambda (name sym source alias)
315 (make-operand name sym source visit alias))
316 vars syms sources aliases)
317 (map (lambda (name sym source)
318 (make-operand name sym source visit #f))
319 vars syms sources)))
320
321 (define (make-unbound-operands vars syms)
322 (map make-operand vars syms))
323
324 (define (set-operand-residual-value! op val)
325 (%set-operand-residual-value!
326 op
327 (match val
328 (($ <primcall> src 'values (first))
329 ;; The continuation of a residualized binding does not need the
330 ;; introduced `values' node, so undo the effects of truncation.
331 first)
332 (else
333 val))))
334
335 (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
336 ;; Peval is O(N) in call sites of the source program. However,
337 ;; visiting an operand can introduce new call sites. If we visit an
338 ;; operand outside a counter -- i.e., outside an inlining attempt --
339 ;; this can lead to divergence. So, if we are visiting an operand to
340 ;; try to copy it, and there is no counter, make a new one.
341 ;;
342 ;; This will only happen at most as many times as there are lexical
343 ;; references in the source program.
344 (and (zero? (operand-visit-count op))
345 (dynamic-wind
346 (lambda ()
347 (set-operand-visit-count! op (1+ (operand-visit-count op))))
348 (lambda ()
349 (and (operand-source op)
350 (if (or counter (and (not effort-limit) (not size-limit)))
351 ((%operand-visit op) (operand-source op) counter ctx)
352 (let/ec k
353 (define (abort)
354 ;; If we abort when visiting the value in a
355 ;; fresh context, we won't succeed in any future
356 ;; attempt, so don't try to copy it again.
357 (set-operand-copyable?! op #f)
358 (k #f))
359 ((%operand-visit op)
360 (operand-source op)
361 (make-top-counter effort-limit size-limit abort op)
362 ctx)))))
363 (lambda ()
364 (set-operand-visit-count! op (1- (operand-visit-count op)))))))
365
366 ;; A helper for constant folding.
367 ;;
368 (define (types-check? primitive-name args)
369 (case primitive-name
370 ((values) #t)
371 ((not pair? null? list? symbol? vector? struct?)
372 (= (length args) 1))
373 ((eq? eqv? equal?)
374 (= (length args) 2))
375 ;; FIXME: add more cases?
376 (else #f)))
377
378 (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
379 #:key
380 (operator-size-limit 40)
381 (operand-size-limit 20)
382 (value-size-limit 10)
383 (effort-limit 500)
384 (recursive-effort-limit 100))
385 "Partially evaluate EXP in compilation environment CENV, with
386 top-level bindings from ENV and return the resulting expression."
387
388 ;; This is a simple partial evaluator. It effectively performs
389 ;; constant folding, copy propagation, dead code elimination, and
390 ;; inlining.
391
392 ;; TODO:
393 ;;
394 ;; Propagate copies across toplevel bindings, if we can prove the
395 ;; bindings to be immutable.
396 ;;
397 ;; Specialize lambda expressions with invariant arguments.
398
399 (define local-toplevel-env
400 ;; The top-level environment of the module being compiled.
401 (let ()
402 (define (env-folder x env)
403 (match x
404 (($ <toplevel-define> _ name)
405 (vhash-consq name #t env))
406 (($ <seq> _ head tail)
407 (env-folder tail (env-folder head env)))
408 (_ env)))
409 (env-folder exp vlist-null)))
410
411 (define (local-toplevel? name)
412 (vhash-assq name local-toplevel-env))
413
414 ;; gensym -> <var>
415 ;; renamed-term -> original-term
416 ;;
417 (define store (build-var-table exp))
418
419 (define (record-new-temporary! name sym refcount)
420 (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
421
422 (define (lookup-var sym)
423 (let ((v (vhash-assq sym store)))
424 (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
425
426 (define (fresh-gensyms vars)
427 (map (lambda (var)
428 (let ((new (gensym (string-append (symbol->string (var-name var))
429 "-"))))
430 (set! store (vhash-consq new var store))
431 new))
432 vars))
433
434 (define (assigned-lexical? sym)
435 (var-set? (lookup-var sym)))
436
437 (define (lexical-refcount sym)
438 (var-refcount (lookup-var sym)))
439
440 ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
441 ;; from it to ORIG.
442 ;;
443 (define (record-source-expression! orig new)
444 (set! store (vhash-consq new (source-expression orig) store))
445 new)
446
447 ;; Find the source expression corresponding to NEW. Used to detect
448 ;; recursive inlining attempts.
449 ;;
450 (define (source-expression new)
451 (let ((x (vhash-assq new store)))
452 (if x (cdr x) new)))
453
454 (define (record-operand-use op)
455 (set-operand-use-count! op (1+ (operand-use-count op))))
456
457 (define (unrecord-operand-uses op n)
458 (let ((count (- (operand-use-count op) n)))
459 (when (zero? count)
460 (set-operand-residual-value! op #f))
461 (set-operand-use-count! op count)))
462
463 (define* (residualize-lexical op #:optional ctx val)
464 (log 'residualize op)
465 (record-operand-use op)
466 (if (memq ctx '(value values))
467 (set-operand-residual-value! op val))
468 (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
469
470 (define (fold-constants src name args ctx)
471 (define (apply-primitive name args)
472 ;; todo: further optimize commutative primitives
473 (catch #t
474 (lambda ()
475 (call-with-values
476 (lambda ()
477 (apply (module-ref the-scm-module name) args))
478 (lambda results
479 (values #t results))))
480 (lambda _
481 (values #f '()))))
482 (define (make-values src values)
483 (match values
484 ((single) single) ; 1 value
485 ((_ ...) ; 0, or 2 or more values
486 (make-primcall src 'values values))))
487 (define (residualize-call)
488 (make-primcall src name args))
489 (cond
490 ((every const? args)
491 (let-values (((success? values)
492 (apply-primitive name (map const-exp args))))
493 (log 'fold success? values name args)
494 (if success?
495 (case ctx
496 ((effect) (make-void src))
497 ((test)
498 ;; Values truncation: only take the first
499 ;; value.
500 (if (pair? values)
501 (make-const src (car values))
502 (make-values src '())))
503 (else
504 (make-values src (map (cut make-const src <>) values))))
505 (residualize-call))))
506 ((and (eq? ctx 'effect) (types-check? name args))
507 (make-void #f))
508 (else
509 (residualize-call))))
510
511 (define (inline-values exp src names gensyms body)
512 (let loop ((exp exp))
513 (match exp
514 ;; Some expression types are always singly-valued.
515 ((or ($ <const>)
516 ($ <void>)
517 ($ <lambda>)
518 ($ <lexical-ref>)
519 ($ <toplevel-ref>)
520 ($ <module-ref>)
521 ($ <primitive-ref>)
522 ($ <dynref>)
523 ($ <lexical-set>) ; FIXME: these set! expressions
524 ($ <toplevel-set>) ; could return zero values in
525 ($ <toplevel-define>) ; the future
526 ($ <module-set>) ;
527 ($ <dynset>)) ;
528 (and (= (length names) 1)
529 (make-let src names gensyms (list exp) body)))
530 (($ <primcall> src (? singly-valued-primitive? name))
531 (and (= (length names) 1)
532 (make-let src names gensyms (list exp) body)))
533
534 ;; Statically-known number of values.
535 (($ <primcall> src 'values vals)
536 (and (= (length names) (length vals))
537 (make-let src names gensyms vals body)))
538
539 ;; Not going to copy code into both branches.
540 (($ <conditional>) #f)
541
542 ;; Bail on other applications.
543 (($ <call>) #f)
544 (($ <primcall>) #f)
545
546 ;; Bail on prompt and abort.
547 (($ <prompt>) #f)
548 (($ <abort>) #f)
549
550 ;; Propagate to tail positions.
551 (($ <let> src names gensyms vals body)
552 (let ((body (loop body)))
553 (and body
554 (make-let src names gensyms vals body))))
555 (($ <letrec> src in-order? names gensyms vals body)
556 (let ((body (loop body)))
557 (and body
558 (make-letrec src in-order? names gensyms vals body))))
559 (($ <fix> src names gensyms vals body)
560 (let ((body (loop body)))
561 (and body
562 (make-fix src names gensyms vals body))))
563 (($ <let-values> src exp
564 ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
565 (let ((body (loop body)))
566 (and body
567 (make-let-values src exp
568 (make-lambda-case src2 req opt rest kw
569 inits gensyms body #f)))))
570 (($ <dynwind> src winder pre body post unwinder)
571 (let ((body (loop body)))
572 (and body
573 (make-dynwind src winder pre body post unwinder))))
574 (($ <dynlet> src fluids vals body)
575 (let ((body (loop body)))
576 (and body
577 (make-dynlet src fluids vals body))))
578 (($ <seq> src head tail)
579 (let ((tail (loop tail)))
580 (and tail (make-seq src head tail)))))))
581
582 (define compute-effects
583 (make-effects-analyzer assigned-lexical?))
584
585 (define (constant-expression? x)
586 ;; Return true if X is constant, for the purposes of copying or
587 ;; elision---i.e., if it is known to have no effects, does not
588 ;; allocate storage for a mutable object, and does not access
589 ;; mutable data (like `car' or toplevel references).
590 (constant? (compute-effects x)))
591
592 (define (prune-bindings ops in-order? body counter ctx build-result)
593 ;; This helper handles both `let' and `letrec'/`fix'. In the latter
594 ;; cases we need to make sure that if referenced binding A needs
595 ;; as-yet-unreferenced binding B, that B is processed for value.
596 ;; Likewise if C, when processed for effect, needs otherwise
597 ;; unreferenced D, then D needs to be processed for value too.
598 ;;
599 (define (referenced? op)
600 ;; When we visit lambdas in operator context, we just copy them,
601 ;; as we will process their body later. However this does have
602 ;; the problem that any free var referenced by the lambda is not
603 ;; marked as needing residualization. Here we hack around this
604 ;; and treat all bindings as referenced if we are in operator
605 ;; context.
606 (or (eq? ctx 'operator)
607 (not (zero? (operand-use-count op)))))
608
609 ;; values := (op ...)
610 ;; effects := (op ...)
611 (define (residualize values effects)
612 ;; Note, values and effects are reversed.
613 (cond
614 (in-order?
615 (let ((values (filter operand-residual-value ops)))
616 (if (null? values)
617 body
618 (build-result (map (compose var-name operand-var) values)
619 (map operand-sym values)
620 (map operand-residual-value values)
621 body))))
622 (else
623 (let ((body
624 (if (null? effects)
625 body
626 (let ((effect-vals (map operand-residual-value effects)))
627 (list->seq #f (reverse (cons body effect-vals)))))))
628 (if (null? values)
629 body
630 (let ((values (reverse values)))
631 (build-result (map (compose var-name operand-var) values)
632 (map operand-sym values)
633 (map operand-residual-value values)
634 body)))))))
635
636 ;; old := (bool ...)
637 ;; values := (op ...)
638 ;; effects := ((op . value) ...)
639 (let prune ((old (map referenced? ops)) (values '()) (effects '()))
640 (let lp ((ops* ops) (values values) (effects effects))
641 (cond
642 ((null? ops*)
643 (let ((new (map referenced? ops)))
644 (if (not (equal? new old))
645 (prune new values '())
646 (residualize values
647 (map (lambda (op val)
648 (set-operand-residual-value! op val)
649 op)
650 (map car effects) (map cdr effects))))))
651 (else
652 (let ((op (car ops*)))
653 (cond
654 ((memq op values)
655 (lp (cdr ops*) values effects))
656 ((operand-residual-value op)
657 (lp (cdr ops*) (cons op values) effects))
658 ((referenced? op)
659 (set-operand-residual-value! op (visit-operand op counter 'value))
660 (lp (cdr ops*) (cons op values) effects))
661 (else
662 (lp (cdr ops*)
663 values
664 (let ((effect (visit-operand op counter 'effect)))
665 (if (void? effect)
666 effects
667 (acons op effect effects))))))))))))
668
669 (define (small-expression? x limit)
670 (let/ec k
671 (tree-il-fold
672 (lambda (x res) ; leaf
673 (1+ res))
674 (lambda (x res) ; down
675 (1+ res))
676 (lambda (x res) ; up
677 (if (< res limit)
678 res
679 (k #f)))
680 0 x)
681 #t))
682
683 (define (extend-env sym op env)
684 (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
685
686 (let loop ((exp exp)
687 (env vlist-null) ; vhash of gensym -> <operand>
688 (counter #f) ; inlined call stack
689 (ctx 'values)) ; effect, value, values, test, operator, or call
690 (define (lookup var)
691 (cond
692 ((vhash-assq var env) => cdr)
693 (else (error "unbound var" var))))
694
695 (define (visit exp ctx)
696 (loop exp env counter ctx))
697
698 (define (for-value exp) (visit exp 'value))
699 (define (for-values exp) (visit exp 'values))
700 (define (for-test exp) (visit exp 'test))
701 (define (for-effect exp) (visit exp 'effect))
702 (define (for-call exp) (visit exp 'call))
703 (define (for-tail exp) (visit exp ctx))
704
705 (if counter
706 (record-effort! counter))
707
708 (log 'visit ctx (and=> counter effort-counter)
709 (unparse-tree-il exp))
710
711 (match exp
712 (($ <const>)
713 (case ctx
714 ((effect) (make-void #f))
715 (else exp)))
716 (($ <void>)
717 (case ctx
718 ((test) (make-const #f #t))
719 (else exp)))
720 (($ <lexical-ref> _ _ gensym)
721 (log 'begin-copy gensym)
722 (let ((op (lookup gensym)))
723 (cond
724 ((eq? ctx 'effect)
725 (log 'lexical-for-effect gensym)
726 (make-void #f))
727 ((operand-alias-value op)
728 ;; This is an unassigned operand that simply aliases some
729 ;; other operand. Recurse to avoid residualizing the leaf
730 ;; binding.
731 => for-tail)
732 ((eq? ctx 'call)
733 ;; Don't propagate copies if we are residualizing a call.
734 (log 'residualize-lexical-call gensym op)
735 (residualize-lexical op))
736 ((var-set? (operand-var op))
737 ;; Assigned lexicals don't copy-propagate.
738 (log 'assigned-var gensym op)
739 (residualize-lexical op))
740 ((not (operand-copyable? op))
741 ;; We already know that this operand is not copyable.
742 (log 'not-copyable gensym op)
743 (residualize-lexical op))
744 ((and=> (operand-constant-value op)
745 (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
746 ;; A cache hit.
747 (let ((val (operand-constant-value op)))
748 (log 'memoized-constant gensym val)
749 (for-tail val)))
750 ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
751 recursive-effort-limit operand-size-limit)
752 =>
753 ;; If we end up deciding to residualize this value instead of
754 ;; copying it, save that residualized value.
755 (lambda (val)
756 (cond
757 ((not (constant-expression? val))
758 (log 'not-constant gensym op)
759 ;; At this point, ctx is operator, test, or value. A
760 ;; value that is non-constant in one context will be
761 ;; non-constant in the others, so it's safe to record
762 ;; that here, and avoid future visits.
763 (set-operand-copyable?! op #f)
764 (residualize-lexical op ctx val))
765 ((or (const? val)
766 (void? val)
767 (primitive-ref? val))
768 ;; Always propagate simple values that cannot lead to
769 ;; code bloat.
770 (log 'copy-simple gensym val)
771 ;; It could be this constant is the result of folding.
772 ;; If that is the case, cache it. This helps loop
773 ;; unrolling get farther.
774 (if (or (eq? ctx 'value) (eq? ctx 'values))
775 (begin
776 (log 'memoize-constant gensym val)
777 (set-operand-constant-value! op val)))
778 val)
779 ((= 1 (var-refcount (operand-var op)))
780 ;; Always propagate values referenced only once.
781 (log 'copy-single gensym val)
782 val)
783 ;; FIXME: do demand-driven size accounting rather than
784 ;; these heuristics.
785 ((eq? ctx 'operator)
786 ;; A pure expression in the operator position. Inline
787 ;; if it's a lambda that's small enough.
788 (if (and (lambda? val)
789 (small-expression? val operator-size-limit))
790 (begin
791 (log 'copy-operator gensym val)
792 val)
793 (begin
794 (log 'too-big-for-operator gensym val)
795 (residualize-lexical op ctx val))))
796 (else
797 ;; A pure expression, processed for call or for value.
798 ;; Don't inline lambdas, because they will probably won't
799 ;; fold because we don't know the operator.
800 (if (and (small-expression? val value-size-limit)
801 (not (tree-il-any lambda? val)))
802 (begin
803 (log 'copy-value gensym val)
804 val)
805 (begin
806 (log 'too-big-or-has-lambda gensym val)
807 (residualize-lexical op ctx val)))))))
808 (else
809 ;; Visit failed. Either the operand isn't bound, as in
810 ;; lambda formal parameters, or the copy was aborted.
811 (log 'unbound-or-aborted gensym op)
812 (residualize-lexical op)))))
813 (($ <lexical-set> src name gensym exp)
814 (let ((op (lookup gensym)))
815 (if (zero? (var-refcount (operand-var op)))
816 (let ((exp (for-effect exp)))
817 (if (void? exp)
818 exp
819 (make-seq src exp (make-void #f))))
820 (begin
821 (record-operand-use op)
822 (make-lexical-set src name (operand-sym op) (for-value exp))))))
823 (($ <let> src names gensyms vals body)
824 (define (compute-alias exp)
825 ;; It's very common for macros to introduce something like:
826 ;;
827 ;; ((lambda (x y) ...) x-exp y-exp)
828 ;;
829 ;; In that case you might end up trying to inline something like:
830 ;;
831 ;; (let ((x x-exp) (y y-exp)) ...)
832 ;;
833 ;; But if x-exp is itself a lexical-ref that aliases some much
834 ;; larger expression, perhaps it will fail to inline due to
835 ;; size. However we don't want to introduce a useless alias
836 ;; (in this case, x). So if the RHS of a let expression is a
837 ;; lexical-ref, we record that expression. If we end up having
838 ;; to residualize X, then instead we residualize X-EXP, as long
839 ;; as it isn't assigned.
840 ;;
841 (match exp
842 (($ <lexical-ref> _ _ sym)
843 (let ((op (lookup sym)))
844 (and (not (var-set? (operand-var op)))
845 (or (operand-alias-value op)
846 exp))))
847 (_ #f)))
848
849 (let* ((vars (map lookup-var gensyms))
850 (new (fresh-gensyms vars))
851 (ops (make-bound-operands vars new vals
852 (lambda (exp counter ctx)
853 (loop exp env counter ctx))
854 (map compute-alias vals)))
855 (env (fold extend-env env gensyms ops))
856 (body (loop body env counter ctx)))
857 (cond
858 ((const? body)
859 (for-tail (list->seq src (append vals (list body)))))
860 ((and (lexical-ref? body)
861 (memq (lexical-ref-gensym body) new))
862 (let ((sym (lexical-ref-gensym body))
863 (pairs (map cons new vals)))
864 ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
865 (for-tail
866 (list->seq
867 src
868 (append (map cdr (alist-delete sym pairs eq?))
869 (list (assq-ref pairs sym)))))))
870 (else
871 ;; Only include bindings for which lexical references
872 ;; have been residualized.
873 (prune-bindings ops #f body counter ctx
874 (lambda (names gensyms vals body)
875 (if (null? names) (error "what!" names))
876 (make-let src names gensyms vals body)))))))
877 (($ <letrec> src in-order? names gensyms vals body)
878 ;; Note the difference from the `let' case: here we use letrec*
879 ;; so that the `visit' procedure for the new operands closes over
880 ;; an environment that includes the operands. Also we don't try
881 ;; to elide aliases, because we can't sensibly reduce something
882 ;; like (letrec ((a b) (b a)) a).
883 (letrec* ((visit (lambda (exp counter ctx)
884 (loop exp env* counter ctx)))
885 (vars (map lookup-var gensyms))
886 (new (fresh-gensyms vars))
887 (ops (make-bound-operands vars new vals visit))
888 (env* (fold extend-env env gensyms ops))
889 (body* (visit body counter ctx)))
890 (if (and (const? body*) (every constant-expression? vals))
891 ;; We may have folded a loop completely, even though there
892 ;; might be cyclical references between the bound values.
893 ;; Handle this degenerate case specially.
894 body*
895 (prune-bindings ops in-order? body* counter ctx
896 (lambda (names gensyms vals body)
897 (make-letrec src in-order?
898 names gensyms vals body))))))
899 (($ <fix> src names gensyms vals body)
900 (letrec* ((visit (lambda (exp counter ctx)
901 (loop exp env* counter ctx)))
902 (vars (map lookup-var gensyms))
903 (new (fresh-gensyms vars))
904 (ops (make-bound-operands vars new vals visit))
905 (env* (fold extend-env env gensyms ops))
906 (body* (visit body counter ctx)))
907 (if (const? body*)
908 body*
909 (prune-bindings ops #f body* counter ctx
910 (lambda (names gensyms vals body)
911 (make-fix src names gensyms vals body))))))
912 (($ <let-values> lv-src producer consumer)
913 ;; Peval the producer, then try to inline the consumer into
914 ;; the producer. If that succeeds, peval again. Otherwise
915 ;; reconstruct the let-values, pevaling the consumer.
916 (let ((producer (for-values producer)))
917 (or (match consumer
918 (($ <lambda-case> src req #f #f #f () gensyms body #f)
919 (cond
920 ((inline-values producer src req gensyms body)
921 => for-tail)
922 (else #f)))
923 (_ #f))
924 (make-let-values lv-src producer (for-tail consumer)))))
925 (($ <dynwind> src winder pre body post unwinder)
926 (make-dynwind src (for-value winder) (for-effect pre)
927 (for-tail body)
928 (for-effect post) (for-value unwinder)))
929 (($ <dynlet> src fluids vals body)
930 (make-dynlet src (map for-value fluids) (map for-value vals)
931 (for-tail body)))
932 (($ <dynref> src fluid)
933 (make-dynref src (for-value fluid)))
934 (($ <dynset> src fluid exp)
935 (make-dynset src (for-value fluid) (for-value exp)))
936 (($ <toplevel-ref> src (? effect-free-primitive? name))
937 exp)
938 (($ <toplevel-ref>)
939 ;; todo: open private local bindings.
940 exp)
941 (($ <module-ref> src module (? effect-free-primitive? name) #f)
942 (let ((module (false-if-exception
943 (resolve-module module #:ensure #f))))
944 (if (module? module)
945 (let ((var (module-variable module name)))
946 (if (eq? var (module-variable the-scm-module name))
947 (make-primitive-ref src name)
948 exp))
949 exp)))
950 (($ <module-ref>)
951 exp)
952 (($ <module-set> src mod name public? exp)
953 (make-module-set src mod name public? (for-value exp)))
954 (($ <toplevel-define> src name exp)
955 (make-toplevel-define src name (for-value exp)))
956 (($ <toplevel-set> src name exp)
957 (make-toplevel-set src name (for-value exp)))
958 (($ <primitive-ref>)
959 (case ctx
960 ((effect) (make-void #f))
961 ((test) (make-const #f #t))
962 (else exp)))
963 (($ <conditional> src condition subsequent alternate)
964 (define (call-with-failure-thunk exp proc)
965 (match exp
966 (($ <call> _ _ ()) (proc exp))
967 (($ <primcall> _ _ ()) (proc exp))
968 (($ <const>) (proc exp))
969 (($ <void>) (proc exp))
970 (($ <lexical-ref>) (proc exp))
971 (_
972 (let ((t (gensym "failure-")))
973 (record-new-temporary! 'failure t 2)
974 (make-let
975 src (list 'failure) (list t)
976 (list
977 (make-lambda
978 #f '()
979 (make-lambda-case #f '() #f #f #f '() '() exp #f)))
980 (proc (make-call #f (make-lexical-ref #f 'failure t)
981 '())))))))
982 (define (simplify-conditional c)
983 (match c
984 ;; Swap the arms of (if (not FOO) A B), to simplify.
985 (($ <conditional> src ($ <primcall> _ 'not (pred))
986 subsequent alternate)
987 (simplify-conditional
988 (make-conditional src pred alternate subsequent)))
989 ;; Special cases for common tests in the predicates of chains
990 ;; of if expressions.
991 (($ <conditional> src
992 ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
993 inner-subsequent
994 alternate)
995 (let lp ((alternate alternate))
996 (match alternate
997 ;; Lift a common repeated test out of a chain of if
998 ;; expressions.
999 (($ <conditional> _ (? (cut tree-il=? outer-test <>))
1000 other-subsequent alternate)
1001 (make-conditional
1002 src outer-test
1003 (simplify-conditional
1004 (make-conditional src* inner-test inner-subsequent
1005 other-subsequent))
1006 alternate))
1007 ;; Likewise, but punching through any surrounding
1008 ;; failure continuations.
1009 (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
1010 (make-let
1011 let-src (list name) (list sym) (list thunk)
1012 (lp body)))
1013 ;; Otherwise, rotate AND tests to expose a simple
1014 ;; condition in the front. Although this may result in
1015 ;; lexically binding failure thunks, the thunks will be
1016 ;; compiled to labels allocation, so there's no actual
1017 ;; code growth.
1018 (_
1019 (call-with-failure-thunk
1020 alternate
1021 (lambda (failure)
1022 (make-conditional
1023 src outer-test
1024 (simplify-conditional
1025 (make-conditional src* inner-test inner-subsequent failure))
1026 failure)))))))
1027 (_ c)))
1028 (match (for-test condition)
1029 (($ <const> _ val)
1030 (if val
1031 (for-tail subsequent)
1032 (for-tail alternate)))
1033 (c
1034 (simplify-conditional
1035 (make-conditional src c (for-tail subsequent)
1036 (for-tail alternate))))))
1037 (($ <primcall> src '@call-with-values
1038 (producer
1039 ($ <lambda> _ _
1040 (and consumer
1041 ;; No optional or kwargs.
1042 ($ <lambda-case>
1043 _ req #f rest #f () gensyms body #f)))))
1044 (for-tail (make-let-values src (make-call src producer '())
1045 consumer)))
1046 (($ <primcall> src 'dynamic-wind (w thunk u))
1047 (for-tail
1048 (cond
1049 ((not (constant-expression? w))
1050 (cond
1051 ((not (constant-expression? u))
1052 (let ((w-sym (gensym "w ")) (u-sym (gensym "u ")))
1053 (record-new-temporary! 'w w-sym 2)
1054 (record-new-temporary! 'u u-sym 2)
1055 (make-let src '(w u) (list w-sym u-sym) (list w u)
1056 (make-dynwind
1057 src
1058 (make-lexical-ref #f 'w w-sym)
1059 (make-call #f (make-lexical-ref #f 'w w-sym) '())
1060 (make-call #f thunk '())
1061 (make-call #f (make-lexical-ref #f 'u u-sym) '())
1062 (make-lexical-ref #f 'u u-sym)))))
1063 (else
1064 (let ((w-sym (gensym "w ")))
1065 (record-new-temporary! 'w w-sym 2)
1066 (make-let src '(w) (list w-sym) (list w)
1067 (make-dynwind
1068 src
1069 (make-lexical-ref #f 'w w-sym)
1070 (make-call #f (make-lexical-ref #f 'w w-sym) '())
1071 (make-call #f thunk '())
1072 (make-call #f u '())
1073 u))))))
1074 ((not (constant-expression? u))
1075 (let ((u-sym (gensym "u ")))
1076 (record-new-temporary! 'u u-sym 2)
1077 (make-let src '(u) (list u-sym) (list u)
1078 (make-dynwind
1079 src
1080 w
1081 (make-call #f w '())
1082 (make-call #f thunk '())
1083 (make-call #f (make-lexical-ref #f 'u u-sym) '())
1084 (make-lexical-ref #f 'u u-sym)))))
1085 (else
1086 (make-dynwind src w (make-call #f w '()) (make-call #f thunk '())
1087 (make-call #f u '()) u)))))
1088
1089 (($ <primcall> src 'values exps)
1090 (cond
1091 ((null? exps)
1092 (if (eq? ctx 'effect)
1093 (make-void #f)
1094 exp))
1095 (else
1096 (let ((vals (map for-value exps)))
1097 (if (and (case ctx
1098 ((value test effect) #t)
1099 (else (null? (cdr vals))))
1100 (every singly-valued-expression? vals))
1101 (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
1102 (make-primcall src 'values vals))))))
1103
1104 (($ <primcall> src (or 'apply '@apply) (proc args ... tail))
1105 (match (for-value tail)
1106 (($ <const> _ (args* ...))
1107 (let ((args* (map (lambda (x) (make-const #f x)) args*)))
1108 (for-tail (make-call src proc (append args args*)))))
1109 (($ <primcall> _ 'list args*)
1110 (for-tail (make-call src proc (append args args*))))
1111 (tail
1112 (let ((args (append (map for-value args) (list tail))))
1113 (make-primcall src '@apply (cons (for-value proc) args))))))
1114
1115 (($ <primcall> src (? constructor-primitive? name) args)
1116 (cond
1117 ((and (memq ctx '(effect test))
1118 (match (cons name args)
1119 ((or ('cons _ _)
1120 ('list . _)
1121 ('vector . _)
1122 ('make-prompt-tag)
1123 ('make-prompt-tag ($ <const> _ (? string?))))
1124 #t)
1125 (_ #f)))
1126 ;; Some expressions can be folded without visiting the
1127 ;; arguments for value.
1128 (let ((res (if (eq? ctx 'effect)
1129 (make-void #f)
1130 (make-const #f #t))))
1131 (for-tail (list->seq src (append args (list res))))))
1132 (else
1133 (match (cons name (map for-value args))
1134 (('cons x ($ <const> _ (? (cut eq? <> '()))))
1135 (make-primcall src 'list (list x)))
1136 (('cons x ($ <primcall> _ 'list elts))
1137 (make-primcall src 'list (cons x elts)))
1138 ((name . args)
1139 (make-primcall src name args))))))
1140
1141 (($ <primcall> src (? accessor-primitive? name) args)
1142 (match (cons name (map for-value args))
1143 ;; FIXME: these for-tail recursions could take place outside
1144 ;; an effort counter.
1145 (('car ($ <primcall> src 'cons (head tail)))
1146 (for-tail (make-seq src tail head)))
1147 (('cdr ($ <primcall> src 'cons (head tail)))
1148 (for-tail (make-seq src head tail)))
1149 (('car ($ <primcall> src 'list (head . tail)))
1150 (for-tail (list->seq src (append tail (list head)))))
1151 (('cdr ($ <primcall> src 'list (head . tail)))
1152 (for-tail (make-seq src head (make-primcall #f 'list tail))))
1153
1154 (('car ($ <const> src (head . tail)))
1155 (for-tail (make-const src head)))
1156 (('cdr ($ <const> src (head . tail)))
1157 (for-tail (make-const src tail)))
1158 (((or 'memq 'memv) k ($ <const> _ (elts ...)))
1159 ;; FIXME: factor
1160 (case ctx
1161 ((effect)
1162 (for-tail
1163 (make-seq src k (make-void #f))))
1164 ((test)
1165 (cond
1166 ((const? k)
1167 ;; A shortcut. The `else' case would handle it, but
1168 ;; this way is faster.
1169 (let ((member (case name ((memq) memq) ((memv) memv))))
1170 (make-const #f (and (member (const-exp k) elts) #t))))
1171 ((null? elts)
1172 (for-tail
1173 (make-seq src k (make-const #f #f))))
1174 (else
1175 (let ((t (gensym "t "))
1176 (eq (if (eq? name 'memq) 'eq? 'eqv?)))
1177 (record-new-temporary! 't t (length elts))
1178 (for-tail
1179 (make-let
1180 src (list 't) (list t) (list k)
1181 (let lp ((elts elts))
1182 (define test
1183 (make-primcall #f eq
1184 (list (make-lexical-ref #f 't t)
1185 (make-const #f (car elts)))))
1186 (if (null? (cdr elts))
1187 test
1188 (make-conditional src test
1189 (make-const #f #t)
1190 (lp (cdr elts)))))))))))
1191 (else
1192 (cond
1193 ((const? k)
1194 (let ((member (case name ((memq) memq) ((memv) memv))))
1195 (make-const #f (member (const-exp k) elts))))
1196 ((null? elts)
1197 (for-tail (make-seq src k (make-const #f #f))))
1198 (else
1199 (make-primcall src name (list k (make-const #f elts))))))))
1200 ((name . args)
1201 (fold-constants src name args ctx))))
1202
1203 (($ <primcall> src (? equality-primitive? name) (a b))
1204 (let ((val-a (for-value a))
1205 (val-b (for-value b)))
1206 (log 'equality-primitive name val-a val-b)
1207 (cond ((and (lexical-ref? val-a) (lexical-ref? val-b)
1208 (eq? (lexical-ref-gensym val-a)
1209 (lexical-ref-gensym val-b)))
1210 (for-tail (make-const #f #t)))
1211 (else
1212 (fold-constants src name (list val-a val-b) ctx)))))
1213
1214 (($ <primcall> src (? effect-free-primitive? name) args)
1215 (fold-constants src name (map for-value args) ctx))
1216
1217 (($ <primcall> src name args)
1218 (make-primcall src name (map for-value args)))
1219
1220 (($ <call> src orig-proc orig-args)
1221 ;; todo: augment the global env with specialized functions
1222 (let ((proc (visit orig-proc 'operator)))
1223 (match proc
1224 (($ <primitive-ref> _ name)
1225 (for-tail (make-primcall src name orig-args)))
1226 (($ <lambda> _ _
1227 ($ <lambda-case> _ req opt #f #f inits gensyms body #f))
1228 ;; Simple case: no rest, no keyword arguments.
1229 ;; todo: handle the more complex cases
1230 (let* ((nargs (length orig-args))
1231 (nreq (length req))
1232 (nopt (if opt (length opt) 0))
1233 (key (source-expression proc)))
1234 (cond
1235 ((or (< nargs nreq) (> nargs (+ nreq nopt)))
1236 ;; An error, or effecting arguments.
1237 (make-call src (for-call orig-proc) (map for-value orig-args)))
1238 ((or (and=> (find-counter key counter) counter-recursive?)
1239 (lambda? orig-proc))
1240 ;; A recursive call, or a lambda in the operator
1241 ;; position of the source expression. Process again in
1242 ;; tail context.
1243 ;;
1244 ;; In the recursive case, mark intervening counters as
1245 ;; recursive, so we can handle a toplevel counter that
1246 ;; recurses mutually with some other procedure.
1247 ;; Otherwise, the next time we see the other procedure,
1248 ;; the effort limit would be clamped to 100.
1249 ;;
1250 (let ((found (find-counter key counter)))
1251 (if (and found (counter-recursive? found))
1252 (let lp ((counter counter))
1253 (if (not (eq? counter found))
1254 (begin
1255 (set-counter-recursive?! counter #t)
1256 (lp (counter-prev counter)))))))
1257
1258 (log 'inline-recurse key)
1259 (loop (make-let src (append req (or opt '()))
1260 gensyms
1261 (append orig-args
1262 (drop inits (- nargs nreq)))
1263 body)
1264 env counter ctx))
1265 (else
1266 ;; An integration at the top-level, the first
1267 ;; recursion of a recursive procedure, or a nested
1268 ;; integration of a procedure that hasn't been seen
1269 ;; yet.
1270 (log 'inline-begin exp)
1271 (let/ec k
1272 (define (abort)
1273 (log 'inline-abort exp)
1274 (k (make-call src (for-call orig-proc)
1275 (map for-value orig-args))))
1276 (define new-counter
1277 (cond
1278 ;; These first two cases will transfer effort
1279 ;; from the current counter into the new
1280 ;; counter.
1281 ((find-counter key counter)
1282 => (lambda (prev)
1283 (make-recursive-counter recursive-effort-limit
1284 operand-size-limit
1285 prev counter)))
1286 (counter
1287 (make-nested-counter abort key counter))
1288 ;; This case opens a new account, effectively
1289 ;; printing money. It should only do so once
1290 ;; for each call site in the source program.
1291 (else
1292 (make-top-counter effort-limit operand-size-limit
1293 abort key))))
1294 (define result
1295 (loop (make-let src (append req (or opt '()))
1296 gensyms
1297 (append orig-args
1298 (drop inits (- nargs nreq)))
1299 body)
1300 env new-counter ctx))
1301
1302 (if counter
1303 ;; The nested inlining attempt succeeded.
1304 ;; Deposit the unspent effort and size back
1305 ;; into the current counter.
1306 (transfer! new-counter counter))
1307
1308 (log 'inline-end result exp)
1309 result)))))
1310 (_
1311 (make-call src (for-call orig-proc) (map for-value orig-args))))))
1312 (($ <lambda> src meta body)
1313 (case ctx
1314 ((effect) (make-void #f))
1315 ((test) (make-const #f #t))
1316 ((operator) exp)
1317 (else (record-source-expression!
1318 exp
1319 (make-lambda src meta (for-values body))))))
1320 (($ <lambda-case> src req opt rest kw inits gensyms body alt)
1321 (define (lift-applied-lambda body gensyms)
1322 (and (not opt) rest (not kw)
1323 (match body
1324 (($ <primcall> _ '@apply
1325 (($ <lambda> _ _ lcase)
1326 ($ <lexical-ref> _ _ sym)
1327 ...))
1328 (and (equal? sym gensyms)
1329 (not (lambda-case-alternate lcase))
1330 lcase))
1331 (_ #f))))
1332 (let* ((vars (map lookup-var gensyms))
1333 (new (fresh-gensyms vars))
1334 (env (fold extend-env env gensyms
1335 (make-unbound-operands vars new)))
1336 (new-sym (lambda (old)
1337 (operand-sym (cdr (vhash-assq old env)))))
1338 (body (loop body env counter ctx)))
1339 (or
1340 ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
1341 (lift-applied-lambda body new)
1342 (make-lambda-case src req opt rest
1343 (match kw
1344 ((aok? (kw name old) ...)
1345 (cons aok? (map list kw name (map new-sym old))))
1346 (_ #f))
1347 (map (cut loop <> env counter 'value) inits)
1348 new
1349 body
1350 (and alt (for-tail alt))))))
1351 (($ <seq> src head tail)
1352 (let ((head (for-effect head))
1353 (tail (for-tail tail)))
1354 (if (void? head)
1355 tail
1356 (make-seq src
1357 (if (and (seq? head)
1358 (void? (seq-tail head)))
1359 (seq-head head)
1360 head)
1361 tail))))
1362 (($ <prompt> src tag body handler)
1363 (define (make-prompt-tag? x)
1364 (match x
1365 (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
1366 #t)
1367 (_ #f)))
1368 (define (find-definition x n-aliases)
1369 (cond
1370 ((lexical-ref? x)
1371 (cond
1372 ((lookup (lexical-ref-gensym x))
1373 => (lambda (op)
1374 (let ((y (or (operand-residual-value op)
1375 (visit-operand op counter 'value 10 10))))
1376 (cond
1377 ((and (lexical-ref? y)
1378 (= (lexical-refcount (lexical-ref-gensym x)) 1))
1379 ;; X is a simple alias for Y. Recurse, regardless of
1380 ;; the number of aliases we were expecting.
1381 (find-definition y n-aliases))
1382 ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
1383 ;; We found a definition that is aliased the right
1384 ;; number of times. We still recurse in case it is a
1385 ;; lexical.
1386 (values (find-definition y 1)
1387 op))
1388 (else
1389 ;; We can't account for our aliases.
1390 (values #f #f))))))
1391 (else
1392 ;; A formal parameter. Can't say anything about that.
1393 (values #f #f))))
1394 ((= n-aliases 1)
1395 ;; Not a lexical: success, but only if we are looking for an
1396 ;; unaliased value.
1397 (values x #f))
1398 (else (values #f #f))))
1399
1400 (let ((tag (for-value tag))
1401 (body (for-tail body)))
1402 (cond
1403 ((find-definition tag 1)
1404 (lambda (val op)
1405 (make-prompt-tag? val))
1406 => (lambda (val op)
1407 ;; There is no way that an <abort> could know the tag
1408 ;; for this <prompt>, so we can elide the <prompt>
1409 ;; entirely.
1410 (unrecord-operand-uses op 1)
1411 body))
1412 ((find-definition tag 2)
1413 (lambda (val op)
1414 (and (make-prompt-tag? val)
1415 (abort? body)
1416 (tree-il=? (abort-tag body) tag)))
1417 => (lambda (val op)
1418 ;; (let ((t (make-prompt-tag)))
1419 ;; (call-with-prompt t
1420 ;; (lambda () (abort-to-prompt t val ...))
1421 ;; (lambda (k arg ...) e ...)))
1422 ;; => (let-values (((k arg ...) (values values val ...)))
1423 ;; e ...)
1424 (unrecord-operand-uses op 2)
1425 (for-tail
1426 (make-let-values
1427 src
1428 (make-primcall #f 'apply
1429 `(,(make-primitive-ref #f 'values)
1430 ,(make-primitive-ref #f 'values)
1431 ,@(abort-args body)
1432 ,(abort-tail body)))
1433 (for-value handler)))))
1434 (else
1435 (make-prompt src tag body (for-value handler))))))
1436 (($ <abort> src tag args tail)
1437 (make-abort src (for-value tag) (map for-value args)
1438 (for-value tail))))))