Merge commit 'f66cbb99ee096186837536885d3436bb334df34d'
[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 residualize?
289 copyable? residual-value constant-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 (residualize? operand-residualize? set-operand-residualize?!)
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
301 (define* (make-operand var sym #:optional source visit)
302 ;; Bind SYM to VAR, with value SOURCE. Unassigned bound operands are
303 ;; considered copyable until we prove otherwise. If we have a source
304 ;; expression, truncate it to one value. Copy propagation does not
305 ;; work on multiply-valued expressions.
306 (let ((source (and=> source truncate-values)))
307 (%make-operand var sym visit source 0 #f
308 (and source (not (var-set? var))) #f #f)))
309
310 (define (make-bound-operands vars syms sources visit)
311 (map (lambda (x y z) (make-operand x y z visit)) vars syms sources))
312
313 (define (make-unbound-operands vars syms)
314 (map make-operand vars syms))
315
316 (define (set-operand-residual-value! op val)
317 (%set-operand-residual-value!
318 op
319 (match val
320 (($ <primcall> src 'values (first))
321 ;; The continuation of a residualized binding does not need the
322 ;; introduced `values' node, so undo the effects of truncation.
323 first)
324 (else
325 val))))
326
327 (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
328 ;; Peval is O(N) in call sites of the source program. However,
329 ;; visiting an operand can introduce new call sites. If we visit an
330 ;; operand outside a counter -- i.e., outside an inlining attempt --
331 ;; this can lead to divergence. So, if we are visiting an operand to
332 ;; try to copy it, and there is no counter, make a new one.
333 ;;
334 ;; This will only happen at most as many times as there are lexical
335 ;; references in the source program.
336 (and (zero? (operand-visit-count op))
337 (dynamic-wind
338 (lambda ()
339 (set-operand-visit-count! op (1+ (operand-visit-count op))))
340 (lambda ()
341 (and (operand-source op)
342 (if (or counter (and (not effort-limit) (not size-limit)))
343 ((%operand-visit op) (operand-source op) counter ctx)
344 (let/ec k
345 (define (abort) (k #f))
346 ((%operand-visit op)
347 (operand-source op)
348 (make-top-counter effort-limit size-limit abort op)
349 ctx)))))
350 (lambda ()
351 (set-operand-visit-count! op (1- (operand-visit-count op)))))))
352
353 ;; A helper for constant folding.
354 ;;
355 (define (types-check? primitive-name args)
356 (case primitive-name
357 ((values) #t)
358 ((not pair? null? list? symbol? vector? struct?)
359 (= (length args) 1))
360 ((eq? eqv? equal?)
361 (= (length args) 2))
362 ;; FIXME: add more cases?
363 (else #f)))
364
365 (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
366 #:key
367 (operator-size-limit 40)
368 (operand-size-limit 20)
369 (value-size-limit 10)
370 (effort-limit 500)
371 (recursive-effort-limit 100))
372 "Partially evaluate EXP in compilation environment CENV, with
373 top-level bindings from ENV and return the resulting expression."
374
375 ;; This is a simple partial evaluator. It effectively performs
376 ;; constant folding, copy propagation, dead code elimination, and
377 ;; inlining.
378
379 ;; TODO:
380 ;;
381 ;; Propagate copies across toplevel bindings, if we can prove the
382 ;; bindings to be immutable.
383 ;;
384 ;; Specialize lambda expressions with invariant arguments.
385
386 (define local-toplevel-env
387 ;; The top-level environment of the module being compiled.
388 (let ()
389 (define (env-folder x env)
390 (match x
391 (($ <toplevel-define> _ name)
392 (vhash-consq name #t env))
393 (($ <seq> _ head tail)
394 (env-folder tail (env-folder head env)))
395 (_ env)))
396 (env-folder exp vlist-null)))
397
398 (define (local-toplevel? name)
399 (vhash-assq name local-toplevel-env))
400
401 ;; gensym -> <var>
402 ;; renamed-term -> original-term
403 ;;
404 (define store (build-var-table exp))
405
406 (define (record-new-temporary! name sym refcount)
407 (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
408
409 (define (lookup-var sym)
410 (let ((v (vhash-assq sym store)))
411 (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
412
413 (define (fresh-gensyms vars)
414 (map (lambda (var)
415 (let ((new (gensym (string-append (symbol->string (var-name var))
416 "-"))))
417 (set! store (vhash-consq new var store))
418 new))
419 vars))
420
421 (define (assigned-lexical? sym)
422 (var-set? (lookup-var sym)))
423
424 (define (lexical-refcount sym)
425 (var-refcount (lookup-var sym)))
426
427 ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
428 ;; from it to ORIG.
429 ;;
430 (define (record-source-expression! orig new)
431 (set! store (vhash-consq new (source-expression orig) store))
432 new)
433
434 ;; Find the source expression corresponding to NEW. Used to detect
435 ;; recursive inlining attempts.
436 ;;
437 (define (source-expression new)
438 (let ((x (vhash-assq new store)))
439 (if x (cdr x) new)))
440
441 (define* (residualize-lexical op #:optional ctx val)
442 (log 'residualize op)
443 (set-operand-residualize?! op #t)
444 (if (eq? ctx 'value)
445 (set-operand-residual-value! op val))
446 (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
447
448 (define (fold-constants src name args ctx)
449 (define (apply-primitive name args)
450 ;; todo: further optimize commutative primitives
451 (catch #t
452 (lambda ()
453 (call-with-values
454 (lambda ()
455 (apply (module-ref the-scm-module name) args))
456 (lambda results
457 (values #t results))))
458 (lambda _
459 (values #f '()))))
460 (define (make-values src values)
461 (match values
462 ((single) single) ; 1 value
463 ((_ ...) ; 0, or 2 or more values
464 (make-primcall src 'values values))))
465 (define (residualize-call)
466 (make-primcall src name args))
467 (cond
468 ((every const? args)
469 (let-values (((success? values)
470 (apply-primitive name (map const-exp args))))
471 (log 'fold success? values name args)
472 (if success?
473 (case ctx
474 ((effect) (make-void src))
475 ((test)
476 ;; Values truncation: only take the first
477 ;; value.
478 (if (pair? values)
479 (make-const src (car values))
480 (make-values src '())))
481 (else
482 (make-values src (map (cut make-const src <>) values))))
483 (residualize-call))))
484 ((and (eq? ctx 'effect) (types-check? name args))
485 (make-void #f))
486 (else
487 (residualize-call))))
488
489 (define (inline-values exp src names gensyms body)
490 (let loop ((exp exp))
491 (match exp
492 ;; Some expression types are always singly-valued.
493 ((or ($ <const>)
494 ($ <void>)
495 ($ <lambda>)
496 ($ <lexical-ref>)
497 ($ <toplevel-ref>)
498 ($ <module-ref>)
499 ($ <primitive-ref>)
500 ($ <dynref>)
501 ($ <lexical-set>) ; FIXME: these set! expressions
502 ($ <toplevel-set>) ; could return zero values in
503 ($ <toplevel-define>) ; the future
504 ($ <module-set>) ;
505 ($ <dynset>)) ;
506 (and (= (length names) 1)
507 (make-let src names gensyms (list exp) body)))
508 (($ <primcall> src (? singly-valued-primitive? name))
509 (and (= (length names) 1)
510 (make-let src names gensyms (list exp) body)))
511
512 ;; Statically-known number of values.
513 (($ <primcall> src 'values vals)
514 (and (= (length names) (length vals))
515 (make-let src names gensyms vals body)))
516
517 ;; Not going to copy code into both branches.
518 (($ <conditional>) #f)
519
520 ;; Bail on other applications.
521 (($ <call>) #f)
522 (($ <primcall>) #f)
523
524 ;; Bail on prompt and abort.
525 (($ <prompt>) #f)
526 (($ <abort>) #f)
527
528 ;; Propagate to tail positions.
529 (($ <let> src names gensyms vals body)
530 (let ((body (loop body)))
531 (and body
532 (make-let src names gensyms vals body))))
533 (($ <letrec> src in-order? names gensyms vals body)
534 (let ((body (loop body)))
535 (and body
536 (make-letrec src in-order? names gensyms vals body))))
537 (($ <fix> src names gensyms vals body)
538 (let ((body (loop body)))
539 (and body
540 (make-fix src names gensyms vals body))))
541 (($ <let-values> src exp
542 ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
543 (let ((body (loop body)))
544 (and body
545 (make-let-values src exp
546 (make-lambda-case src2 req opt rest kw
547 inits gensyms body #f)))))
548 (($ <dynwind> src winder pre body post unwinder)
549 (let ((body (loop body)))
550 (and body
551 (make-dynwind src winder pre body post unwinder))))
552 (($ <dynlet> src fluids vals body)
553 (let ((body (loop body)))
554 (and body
555 (make-dynlet src fluids vals body))))
556 (($ <seq> src head tail)
557 (let ((tail (loop tail)))
558 (and tail (make-seq src head tail)))))))
559
560 (define compute-effects
561 (make-effects-analyzer assigned-lexical?))
562
563 (define (constant-expression? x)
564 ;; Return true if X is constant, for the purposes of copying or
565 ;; elision---i.e., if it is known to have no effects, does not
566 ;; allocate storage for a mutable object, and does not access
567 ;; mutable data (like `car' or toplevel references).
568 (constant? (compute-effects x)))
569
570 (define (prune-bindings ops in-order? body counter ctx build-result)
571 ;; This helper handles both `let' and `letrec'/`fix'. In the latter
572 ;; cases we need to make sure that if referenced binding A needs
573 ;; as-yet-unreferenced binding B, that B is processed for value.
574 ;; Likewise if C, when processed for effect, needs otherwise
575 ;; unreferenced D, then D needs to be processed for value too.
576 ;;
577 (define (referenced? op)
578 ;; When we visit lambdas in operator context, we just copy them,
579 ;; as we will process their body later. However this does have
580 ;; the problem that any free var referenced by the lambda is not
581 ;; marked as needing residualization. Here we hack around this
582 ;; and treat all bindings as referenced if we are in operator
583 ;; context.
584 (or (eq? ctx 'operator) (operand-residualize? op)))
585
586 ;; values := (op ...)
587 ;; effects := (op ...)
588 (define (residualize values effects)
589 ;; Note, values and effects are reversed.
590 (cond
591 (in-order?
592 (let ((values (filter operand-residual-value ops)))
593 (if (null? values)
594 body
595 (build-result (map (compose var-name operand-var) values)
596 (map operand-sym values)
597 (map operand-residual-value values)
598 body))))
599 (else
600 (let ((body
601 (if (null? effects)
602 body
603 (let ((effect-vals (map operand-residual-value effects)))
604 (list->seq #f (reverse (cons body effect-vals)))))))
605 (if (null? values)
606 body
607 (let ((values (reverse values)))
608 (build-result (map (compose var-name operand-var) values)
609 (map operand-sym values)
610 (map operand-residual-value values)
611 body)))))))
612
613 ;; old := (bool ...)
614 ;; values := (op ...)
615 ;; effects := ((op . value) ...)
616 (let prune ((old (map referenced? ops)) (values '()) (effects '()))
617 (let lp ((ops* ops) (values values) (effects effects))
618 (cond
619 ((null? ops*)
620 (let ((new (map referenced? ops)))
621 (if (not (equal? new old))
622 (prune new values '())
623 (residualize values
624 (map (lambda (op val)
625 (set-operand-residual-value! op val)
626 op)
627 (map car effects) (map cdr effects))))))
628 (else
629 (let ((op (car ops*)))
630 (cond
631 ((memq op values)
632 (lp (cdr ops*) values effects))
633 ((operand-residual-value op)
634 (lp (cdr ops*) (cons op values) effects))
635 ((referenced? op)
636 (set-operand-residual-value! op (visit-operand op counter 'value))
637 (lp (cdr ops*) (cons op values) effects))
638 (else
639 (lp (cdr ops*)
640 values
641 (let ((effect (visit-operand op counter 'effect)))
642 (if (void? effect)
643 effects
644 (acons op effect effects))))))))))))
645
646 (define (small-expression? x limit)
647 (let/ec k
648 (tree-il-fold
649 (lambda (x res) ; leaf
650 (1+ res))
651 (lambda (x res) ; down
652 (1+ res))
653 (lambda (x res) ; up
654 (if (< res limit)
655 res
656 (k #f)))
657 0 x)
658 #t))
659
660 (define (extend-env sym op env)
661 (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
662
663 (let loop ((exp exp)
664 (env vlist-null) ; vhash of gensym -> <operand>
665 (counter #f) ; inlined call stack
666 (ctx 'values)) ; effect, value, values, test, operator, or call
667 (define (lookup var)
668 (cond
669 ((vhash-assq var env) => cdr)
670 (else (error "unbound var" var))))
671
672 (define (visit exp ctx)
673 (loop exp env counter ctx))
674
675 (define (for-value exp) (visit exp 'value))
676 (define (for-values exp) (visit exp 'values))
677 (define (for-test exp) (visit exp 'test))
678 (define (for-effect exp) (visit exp 'effect))
679 (define (for-call exp) (visit exp 'call))
680 (define (for-tail exp) (visit exp ctx))
681
682 (if counter
683 (record-effort! counter))
684
685 (log 'visit ctx (and=> counter effort-counter)
686 (unparse-tree-il exp))
687
688 (match exp
689 (($ <const>)
690 (case ctx
691 ((effect) (make-void #f))
692 (else exp)))
693 (($ <void>)
694 (case ctx
695 ((test) (make-const #f #t))
696 (else exp)))
697 (($ <lexical-ref> _ _ gensym)
698 (log 'begin-copy gensym)
699 (let ((op (lookup gensym)))
700 (cond
701 ((eq? ctx 'effect)
702 (log 'lexical-for-effect gensym)
703 (make-void #f))
704 ((eq? ctx 'call)
705 ;; Don't propagate copies if we are residualizing a call.
706 (log 'residualize-lexical-call gensym op)
707 (residualize-lexical op))
708 ((var-set? (operand-var op))
709 ;; Assigned lexicals don't copy-propagate.
710 (log 'assigned-var gensym op)
711 (residualize-lexical op))
712 ((not (operand-copyable? op))
713 ;; We already know that this operand is not copyable.
714 (log 'not-copyable gensym op)
715 (residualize-lexical op))
716 ((and=> (operand-constant-value op)
717 (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
718 ;; A cache hit.
719 (let ((val (operand-constant-value op)))
720 (log 'memoized-constant gensym val)
721 (for-tail val)))
722 ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
723 recursive-effort-limit operand-size-limit)
724 =>
725 ;; If we end up deciding to residualize this value instead of
726 ;; copying it, save that residualized value.
727 (lambda (val)
728 (cond
729 ((not (constant-expression? val))
730 (log 'not-constant gensym op)
731 ;; At this point, ctx is operator, test, or value. A
732 ;; value that is non-constant in one context will be
733 ;; non-constant in the others, so it's safe to record
734 ;; that here, and avoid future visits.
735 (set-operand-copyable?! op #f)
736 (residualize-lexical op ctx val))
737 ((or (const? val)
738 (void? val)
739 (primitive-ref? val))
740 ;; Always propagate simple values that cannot lead to
741 ;; code bloat.
742 (log 'copy-simple gensym val)
743 ;; It could be this constant is the result of folding.
744 ;; If that is the case, cache it. This helps loop
745 ;; unrolling get farther.
746 (if (or (eq? ctx 'value) (eq? ctx 'values))
747 (begin
748 (log 'memoize-constant gensym val)
749 (set-operand-constant-value! op val)))
750 val)
751 ((= 1 (var-refcount (operand-var op)))
752 ;; Always propagate values referenced only once.
753 (log 'copy-single gensym val)
754 val)
755 ;; FIXME: do demand-driven size accounting rather than
756 ;; these heuristics.
757 ((eq? ctx 'operator)
758 ;; A pure expression in the operator position. Inline
759 ;; if it's a lambda that's small enough.
760 (if (and (lambda? val)
761 (small-expression? val operator-size-limit))
762 (begin
763 (log 'copy-operator gensym val)
764 val)
765 (begin
766 (log 'too-big-for-operator gensym val)
767 (residualize-lexical op ctx val))))
768 (else
769 ;; A pure expression, processed for call or for value.
770 ;; Don't inline lambdas, because they will probably won't
771 ;; fold because we don't know the operator.
772 (if (and (small-expression? val value-size-limit)
773 (not (tree-il-any lambda? val)))
774 (begin
775 (log 'copy-value gensym val)
776 val)
777 (begin
778 (log 'too-big-or-has-lambda gensym val)
779 (residualize-lexical op ctx val)))))))
780 (else
781 ;; Visit failed. Either the operand isn't bound, as in
782 ;; lambda formal parameters, or the copy was aborted.
783 (log 'unbound-or-aborted gensym op)
784 (residualize-lexical op)))))
785 (($ <lexical-set> src name gensym exp)
786 (let ((op (lookup gensym)))
787 (if (zero? (var-refcount (operand-var op)))
788 (let ((exp (for-effect exp)))
789 (if (void? exp)
790 exp
791 (make-seq src exp (make-void #f))))
792 (begin
793 (set-operand-residualize?! op #t)
794 (make-lexical-set src name (operand-sym op) (for-value exp))))))
795 (($ <let> src names gensyms vals body)
796 (let* ((vars (map lookup-var gensyms))
797 (new (fresh-gensyms vars))
798 (ops (make-bound-operands vars new vals
799 (lambda (exp counter ctx)
800 (loop exp env counter ctx))))
801 (env (fold extend-env env gensyms ops))
802 (body (loop body env counter ctx)))
803 (cond
804 ((const? body)
805 (for-tail (list->seq src (append vals (list body)))))
806 ((and (lexical-ref? body)
807 (memq (lexical-ref-gensym body) new))
808 (let ((sym (lexical-ref-gensym body))
809 (pairs (map cons new vals)))
810 ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
811 (for-tail
812 (list->seq
813 src
814 (append (map cdr (alist-delete sym pairs eq?))
815 (list (assq-ref pairs sym)))))))
816 (else
817 ;; Only include bindings for which lexical references
818 ;; have been residualized.
819 (prune-bindings ops #f body counter ctx
820 (lambda (names gensyms vals body)
821 (if (null? names) (error "what!" names))
822 (make-let src names gensyms vals body)))))))
823 (($ <letrec> src in-order? names gensyms vals body)
824 ;; Note the difference from the `let' case: here we use letrec*
825 ;; so that the `visit' procedure for the new operands closes over
826 ;; an environment that includes the operands.
827 (letrec* ((visit (lambda (exp counter ctx)
828 (loop exp env* counter ctx)))
829 (vars (map lookup-var gensyms))
830 (new (fresh-gensyms vars))
831 (ops (make-bound-operands vars new vals visit))
832 (env* (fold extend-env env gensyms ops))
833 (body* (visit body counter ctx)))
834 (if (and (const? body*) (every constant-expression? vals))
835 ;; We may have folded a loop completely, even though there
836 ;; might be cyclical references between the bound values.
837 ;; Handle this degenerate case specially.
838 body*
839 (prune-bindings ops in-order? body* counter ctx
840 (lambda (names gensyms vals body)
841 (make-letrec src in-order?
842 names gensyms vals body))))))
843 (($ <fix> src names gensyms vals body)
844 (letrec* ((visit (lambda (exp counter ctx)
845 (loop exp env* counter ctx)))
846 (vars (map lookup-var gensyms))
847 (new (fresh-gensyms vars))
848 (ops (make-bound-operands vars new vals visit))
849 (env* (fold extend-env env gensyms ops))
850 (body* (visit body counter ctx)))
851 (if (const? body*)
852 body*
853 (prune-bindings ops #f body* counter ctx
854 (lambda (names gensyms vals body)
855 (make-fix src names gensyms vals body))))))
856 (($ <let-values> lv-src producer consumer)
857 ;; Peval the producer, then try to inline the consumer into
858 ;; the producer. If that succeeds, peval again. Otherwise
859 ;; reconstruct the let-values, pevaling the consumer.
860 (let ((producer (for-values producer)))
861 (or (match consumer
862 (($ <lambda-case> src req #f #f #f () gensyms body #f)
863 (cond
864 ((inline-values producer src req gensyms body)
865 => for-tail)
866 (else #f)))
867 (_ #f))
868 (make-let-values lv-src producer (for-tail consumer)))))
869 (($ <dynwind> src winder pre body post unwinder)
870 (make-dynwind src (for-value winder) (for-effect pre)
871 (for-tail body)
872 (for-effect post) (for-value unwinder)))
873 (($ <dynlet> src fluids vals body)
874 (make-dynlet src (map for-value fluids) (map for-value vals)
875 (for-tail body)))
876 (($ <dynref> src fluid)
877 (make-dynref src (for-value fluid)))
878 (($ <dynset> src fluid exp)
879 (make-dynset src (for-value fluid) (for-value exp)))
880 (($ <toplevel-ref> src (? effect-free-primitive? name))
881 exp)
882 (($ <toplevel-ref>)
883 ;; todo: open private local bindings.
884 exp)
885 (($ <module-ref> src module (? effect-free-primitive? name) #f)
886 (let ((module (false-if-exception
887 (resolve-module module #:ensure #f))))
888 (if (module? module)
889 (let ((var (module-variable module name)))
890 (if (eq? var (module-variable the-scm-module name))
891 (make-primitive-ref src name)
892 exp))
893 exp)))
894 (($ <module-ref>)
895 exp)
896 (($ <module-set> src mod name public? exp)
897 (make-module-set src mod name public? (for-value exp)))
898 (($ <toplevel-define> src name exp)
899 (make-toplevel-define src name (for-value exp)))
900 (($ <toplevel-set> src name exp)
901 (make-toplevel-set src name (for-value exp)))
902 (($ <primitive-ref>)
903 (case ctx
904 ((effect) (make-void #f))
905 ((test) (make-const #f #t))
906 (else exp)))
907 (($ <conditional> src condition subsequent alternate)
908 (match (for-test condition)
909 (($ <const> _ val)
910 (if val
911 (for-tail subsequent)
912 (for-tail alternate)))
913 ;; Swap the arms of (if (not FOO) A B), to simplify.
914 (($ <primcall> _ 'not (c))
915 (make-conditional src c
916 (for-tail alternate)
917 (for-tail subsequent)))
918 (c
919 (make-conditional src c
920 (for-tail subsequent)
921 (for-tail alternate)))))
922 (($ <primcall> src '@call-with-values
923 (producer
924 ($ <lambda> _ _
925 (and consumer
926 ;; No optional or kwargs.
927 ($ <lambda-case>
928 _ req #f rest #f () gensyms body #f)))))
929 (for-tail (make-let-values src (make-call src producer '())
930 consumer)))
931 (($ <primcall> src 'dynamic-wind (w thunk u))
932 (for-tail
933 (cond
934 ((not (constant-expression? w))
935 (cond
936 ((not (constant-expression? u))
937 (let ((w-sym (gensym "w ")) (u-sym (gensym "u ")))
938 (record-new-temporary! 'w w-sym 2)
939 (record-new-temporary! 'u u-sym 2)
940 (make-let src '(w u) (list w-sym u-sym) (list w u)
941 (make-dynwind
942 src
943 (make-lexical-ref #f 'w w-sym)
944 (make-call #f (make-lexical-ref #f 'w w-sym) '())
945 (make-call #f thunk '())
946 (make-call #f (make-lexical-ref #f 'u u-sym) '())
947 (make-lexical-ref #f 'u u-sym)))))
948 (else
949 (let ((w-sym (gensym "w ")))
950 (record-new-temporary! 'w w-sym 2)
951 (make-let src '(w) (list w-sym) (list w)
952 (make-dynwind
953 src
954 (make-lexical-ref #f 'w w-sym)
955 (make-call #f (make-lexical-ref #f 'w w-sym) '())
956 (make-call #f thunk '())
957 (make-call #f u '())
958 u))))))
959 ((not (constant-expression? u))
960 (let ((u-sym (gensym "u ")))
961 (record-new-temporary! 'u u-sym 2)
962 (make-let src '(u) (list u-sym) (list u)
963 (make-dynwind
964 src
965 w
966 (make-call #f w '())
967 (make-call #f thunk '())
968 (make-call #f (make-lexical-ref #f 'u u-sym) '())
969 (make-lexical-ref #f 'u u-sym)))))
970 (else
971 (make-dynwind src w (make-call #f w '()) (make-call #f thunk '())
972 (make-call #f u '()) u)))))
973
974 (($ <primcall> src 'values exps)
975 (cond
976 ((null? exps)
977 (if (eq? ctx 'effect)
978 (make-void #f)
979 exp))
980 (else
981 (let ((vals (map for-value exps)))
982 (if (and (case ctx
983 ((value test effect) #t)
984 (else (null? (cdr vals))))
985 (every singly-valued-expression? vals))
986 (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
987 (make-primcall src 'values vals))))))
988
989 (($ <primcall> src (? constructor-primitive? name) args)
990 (cond
991 ((and (memq ctx '(effect test))
992 (match (cons name args)
993 ((or ('cons _ _)
994 ('list . _)
995 ('vector . _)
996 ('make-prompt-tag)
997 ('make-prompt-tag ($ <const> _ (? string?))))
998 #t)
999 (_ #f)))
1000 ;; Some expressions can be folded without visiting the
1001 ;; arguments for value.
1002 (let ((res (if (eq? ctx 'effect)
1003 (make-void #f)
1004 (make-const #f #t))))
1005 (for-tail (list->seq src (append args (list res))))))
1006 (else
1007 (match (cons name (map for-value args))
1008 (('cons x ($ <const> _ (? (cut eq? <> '()))))
1009 (make-primcall src 'list (list x)))
1010 (('cons x ($ <primcall> _ 'list elts))
1011 (make-primcall src 'list (cons x elts)))
1012 ((name . args)
1013 (make-primcall src name args))))))
1014
1015 (($ <primcall> src (? accessor-primitive? name) args)
1016 (match (cons name (map for-value args))
1017 ;; FIXME: these for-tail recursions could take place outside
1018 ;; an effort counter.
1019 (('car ($ <primcall> src 'cons (head tail)))
1020 (for-tail (make-seq src tail head)))
1021 (('cdr ($ <primcall> src 'cons (head tail)))
1022 (for-tail (make-seq src head tail)))
1023 (('car ($ <primcall> src 'list (head . tail)))
1024 (for-tail (list->seq src (append tail (list head)))))
1025 (('cdr ($ <primcall> src 'list (head . tail)))
1026 (for-tail (make-seq src head (make-primcall #f 'list tail))))
1027
1028 (('car ($ <const> src (head . tail)))
1029 (for-tail (make-const src head)))
1030 (('cdr ($ <const> src (head . tail)))
1031 (for-tail (make-const src tail)))
1032 (((or 'memq 'memv) k ($ <const> _ (elts ...)))
1033 ;; FIXME: factor
1034 (case ctx
1035 ((effect)
1036 (for-tail
1037 (make-seq src k (make-void #f))))
1038 ((test)
1039 (cond
1040 ((const? k)
1041 ;; A shortcut. The `else' case would handle it, but
1042 ;; this way is faster.
1043 (let ((member (case name ((memq) memq) ((memv) memv))))
1044 (make-const #f (and (member (const-exp k) elts) #t))))
1045 ((null? elts)
1046 (for-tail
1047 (make-seq src k (make-const #f #f))))
1048 (else
1049 (let ((t (gensym "t "))
1050 (eq (if (eq? name 'memq) 'eq? 'eqv?)))
1051 (record-new-temporary! 't t (length elts))
1052 (for-tail
1053 (make-let
1054 src (list 't) (list t) (list k)
1055 (let lp ((elts elts))
1056 (define test
1057 (make-primcall #f eq
1058 (list (make-lexical-ref #f 't t)
1059 (make-const #f (car elts)))))
1060 (if (null? (cdr elts))
1061 test
1062 (make-conditional src test
1063 (make-const #f #t)
1064 (lp (cdr elts)))))))))))
1065 (else
1066 (cond
1067 ((const? k)
1068 (let ((member (case name ((memq) memq) ((memv) memv))))
1069 (make-const #f (member (const-exp k) elts))))
1070 ((null? elts)
1071 (for-tail (make-seq src k (make-const #f #f))))
1072 (else
1073 (make-primcall src name (list k (make-const #f elts))))))))
1074 ((name . args)
1075 (fold-constants src name args ctx))))
1076
1077 (($ <primcall> src (? equality-primitive? name) (a b))
1078 (let ((val-a (for-value a))
1079 (val-b (for-value b)))
1080 (log 'equality-primitive name val-a val-b)
1081 (cond ((and (lexical-ref? val-a) (lexical-ref? val-b)
1082 (eq? (lexical-ref-gensym val-a)
1083 (lexical-ref-gensym val-b)))
1084 (for-tail (make-const #f #t)))
1085 (else
1086 (fold-constants src name (list val-a val-b) ctx)))))
1087
1088 (($ <primcall> src (? effect-free-primitive? name) args)
1089 (fold-constants src name (map for-value args) ctx))
1090
1091 (($ <primcall> src name args)
1092 (make-primcall src name (map for-value args)))
1093
1094 (($ <call> src orig-proc orig-args)
1095 ;; todo: augment the global env with specialized functions
1096 (let ((proc (visit orig-proc 'operator)))
1097 (match proc
1098 (($ <primitive-ref> _ name)
1099 (for-tail (make-primcall src name orig-args)))
1100 (($ <lambda> _ _
1101 ($ <lambda-case> _ req opt #f #f inits gensyms body #f))
1102 ;; Simple case: no rest, no keyword arguments.
1103 ;; todo: handle the more complex cases
1104 (let* ((nargs (length orig-args))
1105 (nreq (length req))
1106 (nopt (if opt (length opt) 0))
1107 (key (source-expression proc)))
1108 (cond
1109 ((or (< nargs nreq) (> nargs (+ nreq nopt)))
1110 ;; An error, or effecting arguments.
1111 (make-call src (for-call orig-proc) (map for-value orig-args)))
1112 ((or (and=> (find-counter key counter) counter-recursive?)
1113 (lambda? orig-proc))
1114 ;; A recursive call, or a lambda in the operator
1115 ;; position of the source expression. Process again in
1116 ;; tail context.
1117 ;;
1118 ;; In the recursive case, mark intervening counters as
1119 ;; recursive, so we can handle a toplevel counter that
1120 ;; recurses mutually with some other procedure.
1121 ;; Otherwise, the next time we see the other procedure,
1122 ;; the effort limit would be clamped to 100.
1123 ;;
1124 (let ((found (find-counter key counter)))
1125 (if (and found (counter-recursive? found))
1126 (let lp ((counter counter))
1127 (if (not (eq? counter found))
1128 (begin
1129 (set-counter-recursive?! counter #t)
1130 (lp (counter-prev counter)))))))
1131
1132 (log 'inline-recurse key)
1133 (loop (make-let src (append req (or opt '()))
1134 gensyms
1135 (append orig-args
1136 (drop inits (- nargs nreq)))
1137 body)
1138 env counter ctx))
1139 (else
1140 ;; An integration at the top-level, the first
1141 ;; recursion of a recursive procedure, or a nested
1142 ;; integration of a procedure that hasn't been seen
1143 ;; yet.
1144 (log 'inline-begin exp)
1145 (let/ec k
1146 (define (abort)
1147 (log 'inline-abort exp)
1148 (k (make-call src (for-call orig-proc)
1149 (map for-value orig-args))))
1150 (define new-counter
1151 (cond
1152 ;; These first two cases will transfer effort
1153 ;; from the current counter into the new
1154 ;; counter.
1155 ((find-counter key counter)
1156 => (lambda (prev)
1157 (make-recursive-counter recursive-effort-limit
1158 operand-size-limit
1159 prev counter)))
1160 (counter
1161 (make-nested-counter abort key counter))
1162 ;; This case opens a new account, effectively
1163 ;; printing money. It should only do so once
1164 ;; for each call site in the source program.
1165 (else
1166 (make-top-counter effort-limit operand-size-limit
1167 abort key))))
1168 (define result
1169 (loop (make-let src (append req (or opt '()))
1170 gensyms
1171 (append orig-args
1172 (drop inits (- nargs nreq)))
1173 body)
1174 env new-counter ctx))
1175
1176 (if counter
1177 ;; The nested inlining attempt succeeded.
1178 ;; Deposit the unspent effort and size back
1179 ;; into the current counter.
1180 (transfer! new-counter counter))
1181
1182 (log 'inline-end result exp)
1183 result)))))
1184 (_
1185 (make-call src (for-call orig-proc) (map for-value orig-args))))))
1186 (($ <lambda> src meta body)
1187 (case ctx
1188 ((effect) (make-void #f))
1189 ((test) (make-const #f #t))
1190 ((operator) exp)
1191 (else (record-source-expression!
1192 exp
1193 (make-lambda src meta (for-values body))))))
1194 (($ <lambda-case> src req opt rest kw inits gensyms body alt)
1195 (define (lift-applied-lambda body gensyms)
1196 (and (not opt) rest (not kw)
1197 (match body
1198 (($ <primcall> _ '@apply
1199 (($ <lambda> _ _ lcase)
1200 ($ <lexical-ref> _ _ sym)
1201 ...))
1202 (and (equal? sym gensyms)
1203 (not (lambda-case-alternate lcase))
1204 lcase))
1205 (_ #f))))
1206 (let* ((vars (map lookup-var gensyms))
1207 (new (fresh-gensyms vars))
1208 (env (fold extend-env env gensyms
1209 (make-unbound-operands vars new)))
1210 (new-sym (lambda (old)
1211 (operand-sym (cdr (vhash-assq old env)))))
1212 (body (loop body env counter ctx)))
1213 (or
1214 ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
1215 (lift-applied-lambda body new)
1216 (make-lambda-case src req opt rest
1217 (match kw
1218 ((aok? (kw name old) ...)
1219 (cons aok? (map list kw name (map new-sym old))))
1220 (_ #f))
1221 (map (cut loop <> env counter 'value) inits)
1222 new
1223 body
1224 (and alt (for-tail alt))))))
1225 (($ <seq> src head tail)
1226 (let ((head (for-effect head))
1227 (tail (for-tail tail)))
1228 (if (void? head)
1229 tail
1230 (make-seq src
1231 (if (and (seq? head)
1232 (void? (seq-tail head)))
1233 (seq-head head)
1234 head)
1235 tail))))
1236 (($ <prompt> src tag body handler)
1237 (define (singly-used-definition x)
1238 (cond
1239 ((and (lexical-ref? x)
1240 ;; Only fetch definitions with single uses.
1241 (= (lexical-refcount (lexical-ref-gensym x)) 1)
1242 (lookup (lexical-ref-gensym x)))
1243 => (lambda (x)
1244 (singly-used-definition (visit-operand x counter 'value 10 10))))
1245 (else x)))
1246 (match (singly-used-definition tag)
1247 (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
1248 ;; There is no way that an <abort> could know the tag
1249 ;; for this <prompt>, so we can elide the <prompt>
1250 ;; entirely.
1251 (for-tail body))
1252 (_
1253 (make-prompt src (for-value tag) (for-tail body)
1254 (for-value handler)))))
1255 (($ <abort> src tag args tail)
1256 (make-abort src (for-value tag) (map for-value args)
1257 (for-value tail))))))