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