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, 2013 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 (fresh-temporaries ls)
435 (map (lambda (elt)
436 (let ((new (gensym "tmp ")))
437 (record-new-temporary! 'tmp new 1)
438 new))
439 ls))
440
441 (define (assigned-lexical? sym)
442 (var-set? (lookup-var sym)))
443
444 (define (lexical-refcount sym)
445 (var-refcount (lookup-var sym)))
446
447 ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
448 ;; from it to ORIG.
449 ;;
450 (define (record-source-expression! orig new)
451 (set! store (vhash-consq new (source-expression orig) store))
452 new)
453
454 ;; Find the source expression corresponding to NEW. Used to detect
455 ;; recursive inlining attempts.
456 ;;
457 (define (source-expression new)
458 (let ((x (vhash-assq new store)))
459 (if x (cdr x) new)))
460
461 (define (record-operand-use op)
462 (set-operand-use-count! op (1+ (operand-use-count op))))
463
464 (define (unrecord-operand-uses op n)
465 (let ((count (- (operand-use-count op) n)))
466 (when (zero? count)
467 (set-operand-residual-value! op #f))
468 (set-operand-use-count! op count)))
469
470 (define* (residualize-lexical op #:optional ctx val)
471 (log 'residualize op)
472 (record-operand-use op)
473 (if (memq ctx '(value values))
474 (set-operand-residual-value! op val))
475 (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
476
477 (define (fold-constants src name args ctx)
478 (define (apply-primitive name args)
479 ;; todo: further optimize commutative primitives
480 (catch #t
481 (lambda ()
482 (call-with-values
483 (lambda ()
484 (apply (module-ref the-scm-module name) args))
485 (lambda results
486 (values #t results))))
487 (lambda _
488 (values #f '()))))
489 (define (make-values src values)
490 (match values
491 ((single) single) ; 1 value
492 ((_ ...) ; 0, or 2 or more values
493 (make-primcall src 'values values))))
494 (define (residualize-call)
495 (make-primcall src name args))
496 (cond
497 ((every const? args)
498 (let-values (((success? values)
499 (apply-primitive name (map const-exp args))))
500 (log 'fold success? values name args)
501 (if success?
502 (case ctx
503 ((effect) (make-void src))
504 ((test)
505 ;; Values truncation: only take the first
506 ;; value.
507 (if (pair? values)
508 (make-const src (car values))
509 (make-values src '())))
510 (else
511 (make-values src (map (cut make-const src <>) values))))
512 (residualize-call))))
513 ((and (eq? ctx 'effect) (types-check? name args))
514 (make-void #f))
515 (else
516 (residualize-call))))
517
518 (define (inline-values src exp nmin nmax consumer)
519 (let loop ((exp exp))
520 (match exp
521 ;; Some expression types are always singly-valued.
522 ((or ($ <const>)
523 ($ <void>)
524 ($ <lambda>)
525 ($ <lexical-ref>)
526 ($ <toplevel-ref>)
527 ($ <module-ref>)
528 ($ <primitive-ref>)
529 ($ <dynref>)
530 ($ <lexical-set>) ; FIXME: these set! expressions
531 ($ <toplevel-set>) ; could return zero values in
532 ($ <toplevel-define>) ; the future
533 ($ <module-set>) ;
534 ($ <dynset>) ;
535 ($ <primcall> src (? singly-valued-primitive?)))
536 (and (<= nmin 1) (or (not nmax) (>= nmax 1))
537 (make-call src (make-lambda #f '() consumer) (list exp))))
538
539 ;; Statically-known number of values.
540 (($ <primcall> src 'values vals)
541 (and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals)))
542 (make-call src (make-lambda #f '() consumer) vals)))
543
544 ;; Not going to copy code into both branches.
545 (($ <conditional>) #f)
546
547 ;; Bail on other applications.
548 (($ <call>) #f)
549 (($ <primcall>) #f)
550
551 ;; Bail on prompt and abort.
552 (($ <prompt>) #f)
553 (($ <abort>) #f)
554
555 ;; Propagate to tail positions.
556 (($ <let> src names gensyms vals body)
557 (let ((body (loop body)))
558 (and body
559 (make-let src names gensyms vals body))))
560 (($ <letrec> src in-order? names gensyms vals body)
561 (let ((body (loop body)))
562 (and body
563 (make-letrec src in-order? names gensyms vals body))))
564 (($ <fix> src names gensyms vals body)
565 (let ((body (loop body)))
566 (and body
567 (make-fix src names gensyms vals body))))
568 (($ <let-values> src exp
569 ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
570 (let ((body (loop body)))
571 (and body
572 (make-let-values src exp
573 (make-lambda-case src2 req opt rest kw
574 inits gensyms body #f)))))
575 (($ <dynwind> src winder pre body post unwinder)
576 (let ((body (loop body)))
577 (and body
578 (make-dynwind src winder pre body post unwinder))))
579 (($ <dynlet> src fluids vals body)
580 (let ((body (loop body)))
581 (and body
582 (make-dynlet src fluids vals body))))
583 (($ <seq> src head tail)
584 (let ((tail (loop tail)))
585 (and tail (make-seq src head tail)))))))
586
587 (define compute-effects
588 (make-effects-analyzer assigned-lexical?))
589
590 (define (constant-expression? x)
591 ;; Return true if X is constant, for the purposes of copying or
592 ;; elision---i.e., if it is known to have no effects, does not
593 ;; allocate storage for a mutable object, and does not access
594 ;; mutable data (like `car' or toplevel references).
595 (constant? (compute-effects x)))
596
597 (define (prune-bindings ops in-order? body counter ctx build-result)
598 ;; This helper handles both `let' and `letrec'/`fix'. In the latter
599 ;; cases we need to make sure that if referenced binding A needs
600 ;; as-yet-unreferenced binding B, that B is processed for value.
601 ;; Likewise if C, when processed for effect, needs otherwise
602 ;; unreferenced D, then D needs to be processed for value too.
603 ;;
604 (define (referenced? op)
605 ;; When we visit lambdas in operator context, we just copy them,
606 ;; as we will process their body later. However this does have
607 ;; the problem that any free var referenced by the lambda is not
608 ;; marked as needing residualization. Here we hack around this
609 ;; and treat all bindings as referenced if we are in operator
610 ;; context.
611 (or (eq? ctx 'operator)
612 (not (zero? (operand-use-count op)))))
613
614 ;; values := (op ...)
615 ;; effects := (op ...)
616 (define (residualize values effects)
617 ;; Note, values and effects are reversed.
618 (cond
619 (in-order?
620 (let ((values (filter operand-residual-value ops)))
621 (if (null? values)
622 body
623 (build-result (map (compose var-name operand-var) values)
624 (map operand-sym values)
625 (map operand-residual-value values)
626 body))))
627 (else
628 (let ((body
629 (if (null? effects)
630 body
631 (let ((effect-vals (map operand-residual-value effects)))
632 (list->seq #f (reverse (cons body effect-vals)))))))
633 (if (null? values)
634 body
635 (let ((values (reverse values)))
636 (build-result (map (compose var-name operand-var) values)
637 (map operand-sym values)
638 (map operand-residual-value values)
639 body)))))))
640
641 ;; old := (bool ...)
642 ;; values := (op ...)
643 ;; effects := ((op . value) ...)
644 (let prune ((old (map referenced? ops)) (values '()) (effects '()))
645 (let lp ((ops* ops) (values values) (effects effects))
646 (cond
647 ((null? ops*)
648 (let ((new (map referenced? ops)))
649 (if (not (equal? new old))
650 (prune new values '())
651 (residualize values
652 (map (lambda (op val)
653 (set-operand-residual-value! op val)
654 op)
655 (map car effects) (map cdr effects))))))
656 (else
657 (let ((op (car ops*)))
658 (cond
659 ((memq op values)
660 (lp (cdr ops*) values effects))
661 ((operand-residual-value op)
662 (lp (cdr ops*) (cons op values) effects))
663 ((referenced? op)
664 (set-operand-residual-value! op (visit-operand op counter 'value))
665 (lp (cdr ops*) (cons op values) effects))
666 (else
667 (lp (cdr ops*)
668 values
669 (let ((effect (visit-operand op counter 'effect)))
670 (if (void? effect)
671 effects
672 (acons op effect effects))))))))))))
673
674 (define (small-expression? x limit)
675 (let/ec k
676 (tree-il-fold
677 (lambda (x res) ; leaf
678 (1+ res))
679 (lambda (x res) ; down
680 (1+ res))
681 (lambda (x res) ; up
682 (if (< res limit)
683 res
684 (k #f)))
685 0 x)
686 #t))
687
688 (define (extend-env sym op env)
689 (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
690
691 (let loop ((exp exp)
692 (env vlist-null) ; vhash of gensym -> <operand>
693 (counter #f) ; inlined call stack
694 (ctx 'values)) ; effect, value, values, test, operator, or call
695 (define (lookup var)
696 (cond
697 ((vhash-assq var env) => cdr)
698 (else (error "unbound var" var))))
699
700 ;; Find a value referenced a specific number of times. This is a hack
701 ;; that's used for propagating fresh data structures like rest lists and
702 ;; prompt tags. Usually we wouldn't copy consed data, but we can do so in
703 ;; some special cases like `apply' or prompts if we can account
704 ;; for all of its uses.
705 ;;
706 ;; You don't want to use this in general because it introduces a slight
707 ;; nonlinearity by running peval again (though with a small effort and size
708 ;; counter).
709 ;;
710 (define (find-definition x n-aliases)
711 (cond
712 ((lexical-ref? x)
713 (cond
714 ((lookup (lexical-ref-gensym x))
715 => (lambda (op)
716 (let ((y (or (operand-residual-value op)
717 (visit-operand op counter 'value 10 10)
718 (operand-source op))))
719 (cond
720 ((and (lexical-ref? y)
721 (= (lexical-refcount (lexical-ref-gensym x)) 1))
722 ;; X is a simple alias for Y. Recurse, regardless of
723 ;; the number of aliases we were expecting.
724 (find-definition y n-aliases))
725 ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
726 ;; We found a definition that is aliased the right
727 ;; number of times. We still recurse in case it is a
728 ;; lexical.
729 (values (find-definition y 1)
730 op))
731 (else
732 ;; We can't account for our aliases.
733 (values #f #f))))))
734 (else
735 ;; A formal parameter. Can't say anything about that.
736 (values #f #f))))
737 ((= n-aliases 1)
738 ;; Not a lexical: success, but only if we are looking for an
739 ;; unaliased value.
740 (values x #f))
741 (else (values #f #f))))
742
743 (define (visit exp ctx)
744 (loop exp env counter ctx))
745
746 (define (for-value exp) (visit exp 'value))
747 (define (for-values exp) (visit exp 'values))
748 (define (for-test exp) (visit exp 'test))
749 (define (for-effect exp) (visit exp 'effect))
750 (define (for-call exp) (visit exp 'call))
751 (define (for-tail exp) (visit exp ctx))
752
753 (if counter
754 (record-effort! counter))
755
756 (log 'visit ctx (and=> counter effort-counter)
757 (unparse-tree-il exp))
758
759 (match exp
760 (($ <const>)
761 (case ctx
762 ((effect) (make-void #f))
763 (else exp)))
764 (($ <void>)
765 (case ctx
766 ((test) (make-const #f #t))
767 (else exp)))
768 (($ <lexical-ref> _ _ gensym)
769 (log 'begin-copy gensym)
770 (let ((op (lookup gensym)))
771 (cond
772 ((eq? ctx 'effect)
773 (log 'lexical-for-effect gensym)
774 (make-void #f))
775 ((operand-alias-value op)
776 ;; This is an unassigned operand that simply aliases some
777 ;; other operand. Recurse to avoid residualizing the leaf
778 ;; binding.
779 => for-tail)
780 ((eq? ctx 'call)
781 ;; Don't propagate copies if we are residualizing a call.
782 (log 'residualize-lexical-call gensym op)
783 (residualize-lexical op))
784 ((var-set? (operand-var op))
785 ;; Assigned lexicals don't copy-propagate.
786 (log 'assigned-var gensym op)
787 (residualize-lexical op))
788 ((not (operand-copyable? op))
789 ;; We already know that this operand is not copyable.
790 (log 'not-copyable gensym op)
791 (residualize-lexical op))
792 ((and=> (operand-constant-value op)
793 (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
794 ;; A cache hit.
795 (let ((val (operand-constant-value op)))
796 (log 'memoized-constant gensym val)
797 (for-tail val)))
798 ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
799 recursive-effort-limit operand-size-limit)
800 =>
801 ;; If we end up deciding to residualize this value instead of
802 ;; copying it, save that residualized value.
803 (lambda (val)
804 (cond
805 ((not (constant-expression? val))
806 (log 'not-constant gensym op)
807 ;; At this point, ctx is operator, test, or value. A
808 ;; value that is non-constant in one context will be
809 ;; non-constant in the others, so it's safe to record
810 ;; that here, and avoid future visits.
811 (set-operand-copyable?! op #f)
812 (residualize-lexical op ctx val))
813 ((or (const? val)
814 (void? val)
815 (primitive-ref? val))
816 ;; Always propagate simple values that cannot lead to
817 ;; code bloat.
818 (log 'copy-simple gensym val)
819 ;; It could be this constant is the result of folding.
820 ;; If that is the case, cache it. This helps loop
821 ;; unrolling get farther.
822 (if (or (eq? ctx 'value) (eq? ctx 'values))
823 (begin
824 (log 'memoize-constant gensym val)
825 (set-operand-constant-value! op val)))
826 val)
827 ((= 1 (var-refcount (operand-var op)))
828 ;; Always propagate values referenced only once.
829 (log 'copy-single gensym val)
830 val)
831 ;; FIXME: do demand-driven size accounting rather than
832 ;; these heuristics.
833 ((eq? ctx 'operator)
834 ;; A pure expression in the operator position. Inline
835 ;; if it's a lambda that's small enough.
836 (if (and (lambda? val)
837 (small-expression? val operator-size-limit))
838 (begin
839 (log 'copy-operator gensym val)
840 val)
841 (begin
842 (log 'too-big-for-operator gensym val)
843 (residualize-lexical op ctx val))))
844 (else
845 ;; A pure expression, processed for call or for value.
846 ;; Don't inline lambdas, because they will probably won't
847 ;; fold because we don't know the operator.
848 (if (and (small-expression? val value-size-limit)
849 (not (tree-il-any lambda? val)))
850 (begin
851 (log 'copy-value gensym val)
852 val)
853 (begin
854 (log 'too-big-or-has-lambda gensym val)
855 (residualize-lexical op ctx val)))))))
856 (else
857 ;; Visit failed. Either the operand isn't bound, as in
858 ;; lambda formal parameters, or the copy was aborted.
859 (log 'unbound-or-aborted gensym op)
860 (residualize-lexical op)))))
861 (($ <lexical-set> src name gensym exp)
862 (let ((op (lookup gensym)))
863 (if (zero? (var-refcount (operand-var op)))
864 (let ((exp (for-effect exp)))
865 (if (void? exp)
866 exp
867 (make-seq src exp (make-void #f))))
868 (begin
869 (record-operand-use op)
870 (make-lexical-set src name (operand-sym op) (for-value exp))))))
871 (($ <let> src
872 (names ... rest)
873 (gensyms ... rest-sym)
874 (vals ... ($ <primcall> _ 'list rest-args))
875 ($ <primcall> asrc (or 'apply '@apply)
876 (proc args ...
877 ($ <lexical-ref> _
878 (? (cut eq? <> rest))
879 (? (lambda (sym)
880 (and (eq? sym rest-sym)
881 (= (lexical-refcount sym) 1))))))))
882 (let* ((tmps (make-list (length rest-args) 'tmp))
883 (tmp-syms (fresh-temporaries tmps)))
884 (for-tail
885 (make-let src
886 (append names tmps)
887 (append gensyms tmp-syms)
888 (append vals rest-args)
889 (make-call
890 asrc
891 proc
892 (append args
893 (map (cut make-lexical-ref #f <> <>)
894 tmps tmp-syms)))))))
895 (($ <let> src names gensyms vals body)
896 (define (compute-alias exp)
897 ;; It's very common for macros to introduce something like:
898 ;;
899 ;; ((lambda (x y) ...) x-exp y-exp)
900 ;;
901 ;; In that case you might end up trying to inline something like:
902 ;;
903 ;; (let ((x x-exp) (y y-exp)) ...)
904 ;;
905 ;; But if x-exp is itself a lexical-ref that aliases some much
906 ;; larger expression, perhaps it will fail to inline due to
907 ;; size. However we don't want to introduce a useless alias
908 ;; (in this case, x). So if the RHS of a let expression is a
909 ;; lexical-ref, we record that expression. If we end up having
910 ;; to residualize X, then instead we residualize X-EXP, as long
911 ;; as it isn't assigned.
912 ;;
913 (match exp
914 (($ <lexical-ref> _ _ sym)
915 (let ((op (lookup sym)))
916 (and (not (var-set? (operand-var op)))
917 (or (operand-alias-value op)
918 exp))))
919 (_ #f)))
920
921 (let* ((vars (map lookup-var gensyms))
922 (new (fresh-gensyms vars))
923 (ops (make-bound-operands vars new vals
924 (lambda (exp counter ctx)
925 (loop exp env counter ctx))
926 (map compute-alias vals)))
927 (env (fold extend-env env gensyms ops))
928 (body (loop body env counter ctx)))
929 (cond
930 ((const? body)
931 (for-tail (list->seq src (append vals (list body)))))
932 ((and (lexical-ref? body)
933 (memq (lexical-ref-gensym body) new))
934 (let ((sym (lexical-ref-gensym body))
935 (pairs (map cons new vals)))
936 ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
937 (for-tail
938 (list->seq
939 src
940 (append (map cdr (alist-delete sym pairs eq?))
941 (list (assq-ref pairs sym)))))))
942 (else
943 ;; Only include bindings for which lexical references
944 ;; have been residualized.
945 (prune-bindings ops #f body counter ctx
946 (lambda (names gensyms vals body)
947 (if (null? names) (error "what!" names))
948 (make-let src names gensyms vals body)))))))
949 (($ <letrec> src in-order? names gensyms vals body)
950 ;; Note the difference from the `let' case: here we use letrec*
951 ;; so that the `visit' procedure for the new operands closes over
952 ;; an environment that includes the operands. Also we don't try
953 ;; to elide aliases, because we can't sensibly reduce something
954 ;; like (letrec ((a b) (b a)) a).
955 (letrec* ((visit (lambda (exp counter ctx)
956 (loop exp env* counter ctx)))
957 (vars (map lookup-var gensyms))
958 (new (fresh-gensyms vars))
959 (ops (make-bound-operands vars new vals visit))
960 (env* (fold extend-env env gensyms ops))
961 (body* (visit body counter ctx)))
962 (if (and (const? body*) (every constant-expression? vals))
963 ;; We may have folded a loop completely, even though there
964 ;; might be cyclical references between the bound values.
965 ;; Handle this degenerate case specially.
966 body*
967 (prune-bindings ops in-order? body* counter ctx
968 (lambda (names gensyms vals body)
969 (make-letrec src in-order?
970 names gensyms vals body))))))
971 (($ <fix> src names gensyms vals body)
972 (letrec* ((visit (lambda (exp counter ctx)
973 (loop exp env* counter ctx)))
974 (vars (map lookup-var gensyms))
975 (new (fresh-gensyms vars))
976 (ops (make-bound-operands vars new vals visit))
977 (env* (fold extend-env env gensyms ops))
978 (body* (visit body counter ctx)))
979 (if (const? body*)
980 body*
981 (prune-bindings ops #f body* counter ctx
982 (lambda (names gensyms vals body)
983 (make-fix src names gensyms vals body))))))
984 (($ <let-values> lv-src producer consumer)
985 ;; Peval the producer, then try to inline the consumer into
986 ;; the producer. If that succeeds, peval again. Otherwise
987 ;; reconstruct the let-values, pevaling the consumer.
988 (let ((producer (for-values producer)))
989 (or (match consumer
990 (($ <lambda-case> src req opt rest #f inits gensyms body #f)
991 (let* ((nmin (length req))
992 (nmax (and (not rest) (+ nmin (if opt (length opt) 0)))))
993 (cond
994 ((inline-values lv-src producer nmin nmax consumer)
995 => for-tail)
996 (else #f))))
997 (_ #f))
998 (make-let-values lv-src producer (for-tail consumer)))))
999 (($ <dynwind> src winder pre body post unwinder)
1000 (make-dynwind src (for-value winder) (for-effect pre)
1001 (for-tail body)
1002 (for-effect post) (for-value unwinder)))
1003 (($ <dynlet> src fluids vals body)
1004 (make-dynlet src (map for-value fluids) (map for-value vals)
1005 (for-tail body)))
1006 (($ <dynref> src fluid)
1007 (make-dynref src (for-value fluid)))
1008 (($ <dynset> src fluid exp)
1009 (make-dynset src (for-value fluid) (for-value exp)))
1010 (($ <toplevel-ref> src (? effect-free-primitive? name))
1011 exp)
1012 (($ <toplevel-ref>)
1013 ;; todo: open private local bindings.
1014 exp)
1015 (($ <module-ref> src module (? effect-free-primitive? name) #f)
1016 (let ((module (false-if-exception
1017 (resolve-module module #:ensure #f))))
1018 (if (module? module)
1019 (let ((var (module-variable module name)))
1020 (if (eq? var (module-variable the-scm-module name))
1021 (make-primitive-ref src name)
1022 exp))
1023 exp)))
1024 (($ <module-ref>)
1025 exp)
1026 (($ <module-set> src mod name public? exp)
1027 (make-module-set src mod name public? (for-value exp)))
1028 (($ <toplevel-define> src name exp)
1029 (make-toplevel-define src name (for-value exp)))
1030 (($ <toplevel-set> src name exp)
1031 (make-toplevel-set src name (for-value exp)))
1032 (($ <primitive-ref>)
1033 (case ctx
1034 ((effect) (make-void #f))
1035 ((test) (make-const #f #t))
1036 (else exp)))
1037 (($ <conditional> src condition subsequent alternate)
1038 (define (call-with-failure-thunk exp proc)
1039 (match exp
1040 (($ <call> _ _ ()) (proc exp))
1041 (($ <primcall> _ _ ()) (proc exp))
1042 (($ <const>) (proc exp))
1043 (($ <void>) (proc exp))
1044 (($ <lexical-ref>) (proc exp))
1045 (_
1046 (let ((t (gensym "failure-")))
1047 (record-new-temporary! 'failure t 2)
1048 (make-let
1049 src (list 'failure) (list t)
1050 (list
1051 (make-lambda
1052 #f '()
1053 (make-lambda-case #f '() #f #f #f '() '() exp #f)))
1054 (proc (make-call #f (make-lexical-ref #f 'failure t)
1055 '())))))))
1056 (define (simplify-conditional c)
1057 (match c
1058 ;; Swap the arms of (if (not FOO) A B), to simplify.
1059 (($ <conditional> src ($ <primcall> _ 'not (pred))
1060 subsequent alternate)
1061 (simplify-conditional
1062 (make-conditional src pred alternate subsequent)))
1063 ;; Special cases for common tests in the predicates of chains
1064 ;; of if expressions.
1065 (($ <conditional> src
1066 ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
1067 inner-subsequent
1068 alternate)
1069 (let lp ((alternate alternate))
1070 (match alternate
1071 ;; Lift a common repeated test out of a chain of if
1072 ;; expressions.
1073 (($ <conditional> _ (? (cut tree-il=? outer-test <>))
1074 other-subsequent alternate)
1075 (make-conditional
1076 src outer-test
1077 (simplify-conditional
1078 (make-conditional src* inner-test inner-subsequent
1079 other-subsequent))
1080 alternate))
1081 ;; Likewise, but punching through any surrounding
1082 ;; failure continuations.
1083 (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
1084 (make-let
1085 let-src (list name) (list sym) (list thunk)
1086 (lp body)))
1087 ;; Otherwise, rotate AND tests to expose a simple
1088 ;; condition in the front. Although this may result in
1089 ;; lexically binding failure thunks, the thunks will be
1090 ;; compiled to labels allocation, so there's no actual
1091 ;; code growth.
1092 (_
1093 (call-with-failure-thunk
1094 alternate
1095 (lambda (failure)
1096 (make-conditional
1097 src outer-test
1098 (simplify-conditional
1099 (make-conditional src* inner-test inner-subsequent failure))
1100 failure)))))))
1101 (_ c)))
1102 (match (for-test condition)
1103 (($ <const> _ val)
1104 (if val
1105 (for-tail subsequent)
1106 (for-tail alternate)))
1107 (c
1108 (simplify-conditional
1109 (make-conditional src c (for-tail subsequent)
1110 (for-tail alternate))))))
1111 (($ <primcall> src '@call-with-values
1112 (producer
1113 ($ <lambda> _ _
1114 (and consumer
1115 ;; No optional or kwargs.
1116 ($ <lambda-case>
1117 _ req #f rest #f () gensyms body #f)))))
1118 (for-tail (make-let-values src (make-call src producer '())
1119 consumer)))
1120 (($ <primcall> src 'dynamic-wind (w thunk u))
1121 (for-tail
1122 (cond
1123 ((not (constant-expression? w))
1124 (cond
1125 ((not (constant-expression? u))
1126 (let ((w-sym (gensym "w ")) (u-sym (gensym "u ")))
1127 (record-new-temporary! 'w w-sym 2)
1128 (record-new-temporary! 'u u-sym 2)
1129 (make-let src '(w u) (list w-sym u-sym) (list w u)
1130 (make-dynwind
1131 src
1132 (make-lexical-ref #f 'w w-sym)
1133 (make-call #f (make-lexical-ref #f 'w w-sym) '())
1134 (make-call #f thunk '())
1135 (make-call #f (make-lexical-ref #f 'u u-sym) '())
1136 (make-lexical-ref #f 'u u-sym)))))
1137 (else
1138 (let ((w-sym (gensym "w ")))
1139 (record-new-temporary! 'w w-sym 2)
1140 (make-let src '(w) (list w-sym) (list w)
1141 (make-dynwind
1142 src
1143 (make-lexical-ref #f 'w w-sym)
1144 (make-call #f (make-lexical-ref #f 'w w-sym) '())
1145 (make-call #f thunk '())
1146 (make-call #f u '())
1147 u))))))
1148 ((not (constant-expression? u))
1149 (let ((u-sym (gensym "u ")))
1150 (record-new-temporary! 'u u-sym 2)
1151 (make-let src '(u) (list u-sym) (list u)
1152 (make-dynwind
1153 src
1154 w
1155 (make-call #f w '())
1156 (make-call #f thunk '())
1157 (make-call #f (make-lexical-ref #f 'u u-sym) '())
1158 (make-lexical-ref #f 'u u-sym)))))
1159 (else
1160 (make-dynwind src w (make-call #f w '()) (make-call #f thunk '())
1161 (make-call #f u '()) u)))))
1162
1163 (($ <primcall> src 'values exps)
1164 (cond
1165 ((null? exps)
1166 (if (eq? ctx 'effect)
1167 (make-void #f)
1168 exp))
1169 (else
1170 (let ((vals (map for-value exps)))
1171 (if (and (case ctx
1172 ((value test effect) #t)
1173 (else (null? (cdr vals))))
1174 (every singly-valued-expression? vals))
1175 (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
1176 (make-primcall src 'values vals))))))
1177
1178 (($ <primcall> src (or 'apply '@apply) (proc args ... tail))
1179 (let lp ((tail* (find-definition tail 1)) (speculative? #t))
1180 (define (copyable? x)
1181 ;; Inlining a result from find-definition effectively copies it,
1182 ;; relying on the let-pruning to remove its original binding. We
1183 ;; shouldn't copy non-constant expressions.
1184 (or (not speculative?) (constant-expression? x)))
1185 (match tail*
1186 (($ <const> _ (args* ...))
1187 (let ((args* (map (cut make-const #f <>) args*)))
1188 (for-tail (make-call src proc (append args args*)))))
1189 (($ <primcall> _ 'cons
1190 ((and head (? copyable?)) (and tail (? copyable?))))
1191 (for-tail (make-primcall src '@apply
1192 (cons proc
1193 (append args (list head tail))))))
1194 (($ <primcall> _ 'list
1195 (and args* ((? copyable?) ...)))
1196 (for-tail (make-call src proc (append args args*))))
1197 (tail*
1198 (if speculative?
1199 (lp (for-value tail) #f)
1200 (let ((args (append (map for-value args) (list tail*))))
1201 (make-primcall src '@apply
1202 (cons (for-value proc) args))))))))
1203
1204 (($ <primcall> src (? constructor-primitive? name) args)
1205 (cond
1206 ((and (memq ctx '(effect test))
1207 (match (cons name args)
1208 ((or ('cons _ _)
1209 ('list . _)
1210 ('vector . _)
1211 ('make-prompt-tag)
1212 ('make-prompt-tag ($ <const> _ (? string?))))
1213 #t)
1214 (_ #f)))
1215 ;; Some expressions can be folded without visiting the
1216 ;; arguments for value.
1217 (let ((res (if (eq? ctx 'effect)
1218 (make-void #f)
1219 (make-const #f #t))))
1220 (for-tail (list->seq src (append args (list res))))))
1221 (else
1222 (match (cons name (map for-value args))
1223 (('cons x ($ <const> _ (? (cut eq? <> '()))))
1224 (make-primcall src 'list (list x)))
1225 (('cons x ($ <primcall> _ 'list elts))
1226 (make-primcall src 'list (cons x elts)))
1227 ((name . args)
1228 (make-primcall src name args))))))
1229
1230 (($ <primcall> src (? accessor-primitive? name) args)
1231 (match (cons name (map for-value args))
1232 ;; FIXME: these for-tail recursions could take place outside
1233 ;; an effort counter.
1234 (('car ($ <primcall> src 'cons (head tail)))
1235 (for-tail (make-seq src tail head)))
1236 (('cdr ($ <primcall> src 'cons (head tail)))
1237 (for-tail (make-seq src head tail)))
1238 (('car ($ <primcall> src 'list (head . tail)))
1239 (for-tail (list->seq src (append tail (list head)))))
1240 (('cdr ($ <primcall> src 'list (head . tail)))
1241 (for-tail (make-seq src head (make-primcall #f 'list tail))))
1242
1243 (('car ($ <const> src (head . tail)))
1244 (for-tail (make-const src head)))
1245 (('cdr ($ <const> src (head . tail)))
1246 (for-tail (make-const src tail)))
1247 (((or 'memq 'memv) k ($ <const> _ (elts ...)))
1248 ;; FIXME: factor
1249 (case ctx
1250 ((effect)
1251 (for-tail
1252 (make-seq src k (make-void #f))))
1253 ((test)
1254 (cond
1255 ((const? k)
1256 ;; A shortcut. The `else' case would handle it, but
1257 ;; this way is faster.
1258 (let ((member (case name ((memq) memq) ((memv) memv))))
1259 (make-const #f (and (member (const-exp k) elts) #t))))
1260 ((null? elts)
1261 (for-tail
1262 (make-seq src k (make-const #f #f))))
1263 (else
1264 (let ((t (gensym "t "))
1265 (eq (if (eq? name 'memq) 'eq? 'eqv?)))
1266 (record-new-temporary! 't t (length elts))
1267 (for-tail
1268 (make-let
1269 src (list 't) (list t) (list k)
1270 (let lp ((elts elts))
1271 (define test
1272 (make-primcall #f eq
1273 (list (make-lexical-ref #f 't t)
1274 (make-const #f (car elts)))))
1275 (if (null? (cdr elts))
1276 test
1277 (make-conditional src test
1278 (make-const #f #t)
1279 (lp (cdr elts)))))))))))
1280 (else
1281 (cond
1282 ((const? k)
1283 (let ((member (case name ((memq) memq) ((memv) memv))))
1284 (make-const #f (member (const-exp k) elts))))
1285 ((null? elts)
1286 (for-tail (make-seq src k (make-const #f #f))))
1287 (else
1288 (make-primcall src name (list k (make-const #f elts))))))))
1289 ((name . args)
1290 (fold-constants src name args ctx))))
1291
1292 (($ <primcall> src (? equality-primitive? name) (a b))
1293 (let ((val-a (for-value a))
1294 (val-b (for-value b)))
1295 (log 'equality-primitive name val-a val-b)
1296 (cond ((and (lexical-ref? val-a) (lexical-ref? val-b)
1297 (eq? (lexical-ref-gensym val-a)
1298 (lexical-ref-gensym val-b)))
1299 (for-tail (make-const #f #t)))
1300 (else
1301 (fold-constants src name (list val-a val-b) ctx)))))
1302
1303 (($ <primcall> src (? effect-free-primitive? name) args)
1304 (fold-constants src name (map for-value args) ctx))
1305
1306 (($ <primcall> src name args)
1307 (make-primcall src name (map for-value args)))
1308
1309 (($ <call> src orig-proc orig-args)
1310 ;; todo: augment the global env with specialized functions
1311 (let revisit-proc ((proc (visit orig-proc 'operator)))
1312 (match proc
1313 (($ <primitive-ref> _ name)
1314 (for-tail (make-primcall src name orig-args)))
1315 (($ <lambda> _ _
1316 ($ <lambda-case> _ req opt rest #f inits gensyms body #f))
1317 ;; Simple case: no keyword arguments.
1318 ;; todo: handle the more complex cases
1319 (let* ((nargs (length orig-args))
1320 (nreq (length req))
1321 (nopt (if opt (length opt) 0))
1322 (key (source-expression proc)))
1323 (define (inlined-call)
1324 (make-let src
1325 (append req
1326 (or opt '())
1327 (if rest (list rest) '()))
1328 gensyms
1329 (if (> nargs (+ nreq nopt))
1330 (append (list-head orig-args (+ nreq nopt))
1331 (list
1332 (make-primcall
1333 #f 'list
1334 (drop orig-args (+ nreq nopt)))))
1335 (append orig-args
1336 (drop inits (- nargs nreq))
1337 (if rest
1338 (list (make-const #f '()))
1339 '())))
1340 body))
1341
1342 (cond
1343 ((or (< nargs nreq) (and (not rest) (> nargs (+ nreq nopt))))
1344 ;; An error, or effecting arguments.
1345 (make-call src (for-call orig-proc) (map for-value orig-args)))
1346 ((or (and=> (find-counter key counter) counter-recursive?)
1347 (lambda? orig-proc))
1348 ;; A recursive call, or a lambda in the operator
1349 ;; position of the source expression. Process again in
1350 ;; tail context.
1351 ;;
1352 ;; In the recursive case, mark intervening counters as
1353 ;; recursive, so we can handle a toplevel counter that
1354 ;; recurses mutually with some other procedure.
1355 ;; Otherwise, the next time we see the other procedure,
1356 ;; the effort limit would be clamped to 100.
1357 ;;
1358 (let ((found (find-counter key counter)))
1359 (if (and found (counter-recursive? found))
1360 (let lp ((counter counter))
1361 (if (not (eq? counter found))
1362 (begin
1363 (set-counter-recursive?! counter #t)
1364 (lp (counter-prev counter)))))))
1365
1366 (log 'inline-recurse key)
1367 (loop (inlined-call) env counter ctx))
1368 (else
1369 ;; An integration at the top-level, the first
1370 ;; recursion of a recursive procedure, or a nested
1371 ;; integration of a procedure that hasn't been seen
1372 ;; yet.
1373 (log 'inline-begin exp)
1374 (let/ec k
1375 (define (abort)
1376 (log 'inline-abort exp)
1377 (k (make-call src (for-call orig-proc)
1378 (map for-value orig-args))))
1379 (define new-counter
1380 (cond
1381 ;; These first two cases will transfer effort
1382 ;; from the current counter into the new
1383 ;; counter.
1384 ((find-counter key counter)
1385 => (lambda (prev)
1386 (make-recursive-counter recursive-effort-limit
1387 operand-size-limit
1388 prev counter)))
1389 (counter
1390 (make-nested-counter abort key counter))
1391 ;; This case opens a new account, effectively
1392 ;; printing money. It should only do so once
1393 ;; for each call site in the source program.
1394 (else
1395 (make-top-counter effort-limit operand-size-limit
1396 abort key))))
1397 (define result
1398 (loop (inlined-call) env new-counter ctx))
1399
1400 (if counter
1401 ;; The nested inlining attempt succeeded.
1402 ;; Deposit the unspent effort and size back
1403 ;; into the current counter.
1404 (transfer! new-counter counter))
1405
1406 (log 'inline-end result exp)
1407 result)))))
1408 (($ <let> _ _ _ vals _)
1409 ;; Attempt to inline `let' in the operator position.
1410 ;;
1411 ;; We have to re-visit the proc in value mode, since the
1412 ;; `let' bindings might have been introduced or renamed,
1413 ;; whereas the lambda (if any) in operator position has not
1414 ;; been renamed.
1415 (if (or (and-map constant-expression? vals)
1416 (and-map constant-expression? orig-args))
1417 ;; The arguments and the let-bound values commute.
1418 (match (for-value orig-proc)
1419 (($ <let> lsrc names syms vals body)
1420 (log 'inline-let orig-proc)
1421 (for-tail
1422 (make-let lsrc names syms vals
1423 (make-call src body orig-args))))
1424 ;; It's possible for a `let' to go away after the
1425 ;; visit due to the fact that visiting a procedure in
1426 ;; value context will prune unused bindings, whereas
1427 ;; visiting in operator mode can't because it doesn't
1428 ;; traverse through lambdas. In that case re-visit
1429 ;; the procedure.
1430 (proc (revisit-proc proc)))
1431 (make-call src (for-call orig-proc)
1432 (map for-value orig-args))))
1433 (_
1434 (make-call src (for-call orig-proc) (map for-value orig-args))))))
1435 (($ <lambda> src meta body)
1436 (case ctx
1437 ((effect) (make-void #f))
1438 ((test) (make-const #f #t))
1439 ((operator) exp)
1440 (else (record-source-expression!
1441 exp
1442 (make-lambda src meta (and body (for-values body)))))))
1443 (($ <lambda-case> src req opt rest kw inits gensyms body alt)
1444 (define (lift-applied-lambda body gensyms)
1445 (and (not opt) rest (not kw)
1446 (match body
1447 (($ <primcall> _ '@apply
1448 (($ <lambda> _ _ (and lcase ($ <lambda-case>)))
1449 ($ <lexical-ref> _ _ sym)
1450 ...))
1451 (and (equal? sym gensyms)
1452 (not (lambda-case-alternate lcase))
1453 lcase))
1454 (_ #f))))
1455 (let* ((vars (map lookup-var gensyms))
1456 (new (fresh-gensyms vars))
1457 (env (fold extend-env env gensyms
1458 (make-unbound-operands vars new)))
1459 (new-sym (lambda (old)
1460 (operand-sym (cdr (vhash-assq old env)))))
1461 (body (loop body env counter ctx)))
1462 (or
1463 ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
1464 (lift-applied-lambda body new)
1465 (make-lambda-case src req opt rest
1466 (match kw
1467 ((aok? (kw name old) ...)
1468 (cons aok? (map list kw name (map new-sym old))))
1469 (_ #f))
1470 (map (cut loop <> env counter 'value) inits)
1471 new
1472 body
1473 (and alt (for-tail alt))))))
1474 (($ <seq> src head tail)
1475 (let ((head (for-effect head))
1476 (tail (for-tail tail)))
1477 (if (void? head)
1478 tail
1479 (make-seq src
1480 (if (and (seq? head)
1481 (void? (seq-tail head)))
1482 (seq-head head)
1483 head)
1484 tail))))
1485 (($ <prompt> src tag body handler)
1486 (define (make-prompt-tag? x)
1487 (match x
1488 (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
1489 #t)
1490 (_ #f)))
1491
1492 (let ((tag (for-value tag))
1493 (body (for-tail body)))
1494 (cond
1495 ((find-definition tag 1)
1496 (lambda (val op)
1497 (make-prompt-tag? val))
1498 => (lambda (val op)
1499 ;; There is no way that an <abort> could know the tag
1500 ;; for this <prompt>, so we can elide the <prompt>
1501 ;; entirely.
1502 (unrecord-operand-uses op 1)
1503 body))
1504 ((find-definition tag 2)
1505 (lambda (val op)
1506 (and (make-prompt-tag? val)
1507 (abort? body)
1508 (tree-il=? (abort-tag body) tag)))
1509 => (lambda (val op)
1510 ;; (let ((t (make-prompt-tag)))
1511 ;; (call-with-prompt t
1512 ;; (lambda () (abort-to-prompt t val ...))
1513 ;; (lambda (k arg ...) e ...)))
1514 ;; => (let-values (((k arg ...) (values values val ...)))
1515 ;; e ...)
1516 (unrecord-operand-uses op 2)
1517 (for-tail
1518 (make-let-values
1519 src
1520 (make-primcall #f 'apply
1521 `(,(make-primitive-ref #f 'values)
1522 ,(make-primitive-ref #f 'values)
1523 ,@(abort-args body)
1524 ,(abort-tail body)))
1525 (for-value handler)))))
1526 (else
1527 (make-prompt src tag body (for-value handler))))))
1528 (($ <abort> src tag args tail)
1529 (make-abort src (for-value tag) (map for-value args)
1530 (for-value tail))))))