1 ;;; Tree-IL partial evaluator
3 ;; Copyright (C) 2011, 2012 Free Software Foundation, Inc.
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.
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.
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
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)
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.
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].
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.
45 ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
48 ;; First, some helpers.
50 (define-syntax *logging* (identifier-syntax #f))
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
56 ;; (define %logging #f)
57 ;; (define-syntax *logging* (identifier-syntax %logging))
59 ;; Then you can change %logging at runtime.
65 (or (eq? *logging* #t)
66 (memq 'event *logging*)))
67 (log* 'event arg ...)))))
69 (define (log* event . args)
70 (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
72 (pp `(log ,event . ,args))
76 (define-syntax-rule (let/ec k e e* ...)
77 (let ((tag (make-prompt-tag)))
81 (let ((k (lambda args (apply abort-to-prompt tag args))))
83 (lambda (_ res) res))))
85 (define (tree-il-any proc exp)
87 (tree-il-fold (lambda (exp res)
88 (let ((res (proc exp)))
91 (let ((res (proc exp)))
96 (define (vlist-any proc vlist)
97 (let ((len (vlist-length vlist)))
100 (or (proc (vlist-ref vlist i))
103 (define (singly-valued-expression? exp)
106 (($ <lexical-ref>) #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)
117 (define (truncate-values x)
118 "Discard all but the first value of X."
119 (if (singly-valued-expression? x)
121 (make-primcall (tree-il-src x) 'values (list x))))
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.
127 (define-record-type <var>
128 (make-var name gensym refcount set?)
132 (refcount var-refcount set-var-refcount!)
133 (set? var-set? set-var-set?!))
135 (define* (build-var-table exp #:optional (table vlist-null))
139 (($ <lexical-ref> src name gensym)
140 (let ((var (cdr (vhash-assq gensym res))))
141 (set-var-refcount! var (1+ (var-refcount var)))
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))
150 (append req (or opt '()) (if rest (list rest) '())
152 ((aok? (kw name sym) ...) name)
155 (($ <let> src names gensyms vals body)
156 (fold (lambda (name sym res)
157 (vhash-consq sym (make-var name sym 0 #f) res))
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))
163 (($ <fix> src names gensyms vals body)
164 (fold (lambda (name sym res)
165 (vhash-consq sym (make-var name sym 0 #f) res))
167 (($ <lexical-set> src name gensym exp)
168 (set-var-set?! (cdr (vhash-assq gensym res)) #t)
171 (lambda (exp res) res)
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.
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.
183 ;; Counters should limit the size of the residual program as well, but
184 ;; currently this is not implemented.
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.
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.
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.
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.
208 (define-record-type <counter>
209 (%make-counter effort size continuation recursive? data prev)
211 (effort effort-counter)
213 (continuation counter-continuation)
214 (recursive? counter-recursive? set-counter-recursive?!)
218 (define (abort-counter c)
219 ((counter-continuation c)))
221 (define (record-effort! c)
222 (let ((e (effort-counter c)))
223 (if (zero? (variable-ref e))
225 (variable-set! e (1- (variable-ref e))))))
227 (define (record-size! c)
228 (let ((s (size-counter c)))
229 (if (zero? (variable-ref s))
231 (variable-set! s (1- (variable-ref s))))))
233 (define (find-counter data counter)
235 (if (eq? data (counter-data counter))
237 (find-counter data (counter-prev counter)))))
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))))
249 (transfer-counter! (effort-counter from) (effort-counter to) effort)
250 (transfer-counter! (size-counter from) (size-counter to) size))
252 (define (make-top-counter effort-limit size-limit continuation data)
253 (%make-counter (make-variable effort-limit)
254 (make-variable size-limit)
260 (define (make-nested-counter continuation data current)
261 (let ((c (%make-counter (make-variable 0)
267 (transfer! current c)
270 (define (make-recursive-counter effort-limit size-limit orig current)
271 (let ((c (%make-counter (make-variable 0)
273 (counter-continuation orig)
277 (transfer! current c effort-limit size-limit)
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.
285 ;; TODO: Record value size in operand structure?
287 (define-record-type <operand>
288 (%make-operand var sym visit source visit-count use-count
289 copyable? residual-value constant-value alias-value)
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!))
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))))
312 (define* (make-bound-operands vars syms sources visit #:optional 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))
321 (define (make-unbound-operands vars syms)
322 (map make-operand vars syms))
324 (define (set-operand-residual-value! op val)
325 (%set-operand-residual-value!
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.
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.
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))
347 (set-operand-visit-count! op (1+ (operand-visit-count op))))
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)
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)
361 (make-top-counter effort-limit size-limit abort op)
364 (set-operand-visit-count! op (1- (operand-visit-count op)))))))
366 ;; A helper for constant folding.
368 (define (types-check? primitive-name args)
371 ((not pair? null? list? symbol? vector? struct?)
375 ;; FIXME: add more cases?
378 (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
380 (operator-size-limit 40)
381 (operand-size-limit 20)
382 (value-size-limit 10)
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."
388 ;; This is a simple partial evaluator. It effectively performs
389 ;; constant folding, copy propagation, dead code elimination, and
394 ;; Propagate copies across toplevel bindings, if we can prove the
395 ;; bindings to be immutable.
397 ;; Specialize lambda expressions with invariant arguments.
399 (define local-toplevel-env
400 ;; The top-level environment of the module being compiled.
402 (define (env-folder x env)
404 (($ <toplevel-define> _ name)
405 (vhash-consq name #t env))
406 (($ <seq> _ head tail)
407 (env-folder tail (env-folder head env)))
409 (env-folder exp vlist-null)))
411 (define (local-toplevel? name)
412 (vhash-assq name local-toplevel-env))
415 ;; renamed-term -> original-term
417 (define store (build-var-table exp))
419 (define (record-new-temporary! name sym refcount)
420 (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
422 (define (lookup-var sym)
423 (let ((v (vhash-assq sym store)))
424 (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
426 (define (fresh-gensyms vars)
428 (let ((new (gensym (string-append (symbol->string (var-name var))
430 (set! store (vhash-consq new var store))
434 (define (assigned-lexical? sym)
435 (var-set? (lookup-var sym)))
437 (define (lexical-refcount sym)
438 (var-refcount (lookup-var sym)))
440 ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
443 (define (record-source-expression! orig new)
444 (set! store (vhash-consq new (source-expression orig) store))
447 ;; Find the source expression corresponding to NEW. Used to detect
448 ;; recursive inlining attempts.
450 (define (source-expression new)
451 (let ((x (vhash-assq new store)))
454 (define (record-operand-use op)
455 (set-operand-use-count! op (1+ (operand-use-count op))))
457 (define (unrecord-operand-uses op n)
458 (let ((count (- (operand-use-count op) n)))
460 (set-operand-residual-value! op #f))
461 (set-operand-use-count! op count)))
463 (define* (residualize-lexical op #:optional ctx val)
464 (log 'residualize op)
465 (record-operand-use op)
466 (if (memq ctx '(value values))
467 (set-operand-residual-value! op val))
468 (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
470 (define (fold-constants src name args ctx)
471 (define (apply-primitive name args)
472 ;; todo: further optimize commutative primitives
477 (apply (module-ref the-scm-module name) args))
479 (values #t results))))
482 (define (make-values src values)
484 ((single) single) ; 1 value
485 ((_ ...) ; 0, or 2 or more values
486 (make-primcall src 'values values))))
487 (define (residualize-call)
488 (make-primcall src name args))
491 (let-values (((success? values)
492 (apply-primitive name (map const-exp args))))
493 (log 'fold success? values name args)
496 ((effect) (make-void src))
498 ;; Values truncation: only take the first
501 (make-const src (car values))
502 (make-values src '())))
504 (make-values src (map (cut make-const src <>) values))))
505 (residualize-call))))
506 ((and (eq? ctx 'effect) (types-check? name args))
509 (residualize-call))))
511 (define (inline-values exp src names gensyms body)
512 (let loop ((exp exp))
514 ;; Some expression types are always singly-valued.
523 ($ <lexical-set>) ; FIXME: these set! expressions
524 ($ <toplevel-set>) ; could return zero values in
525 ($ <toplevel-define>) ; the future
528 (and (= (length names) 1)
529 (make-let src names gensyms (list exp) body)))
530 (($ <primcall> src (? singly-valued-primitive? name))
531 (and (= (length names) 1)
532 (make-let src names gensyms (list exp) body)))
534 ;; Statically-known number of values.
535 (($ <primcall> src 'values vals)
536 (and (= (length names) (length vals))
537 (make-let src names gensyms vals body)))
539 ;; Not going to copy code into both branches.
540 (($ <conditional>) #f)
542 ;; Bail on other applications.
546 ;; Bail on prompt and abort.
550 ;; Propagate to tail positions.
551 (($ <let> src names gensyms vals body)
552 (let ((body (loop body)))
554 (make-let src names gensyms vals body))))
555 (($ <letrec> src in-order? names gensyms vals body)
556 (let ((body (loop body)))
558 (make-letrec src in-order? names gensyms vals body))))
559 (($ <fix> src names gensyms vals body)
560 (let ((body (loop body)))
562 (make-fix src names gensyms vals body))))
563 (($ <let-values> src exp
564 ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
565 (let ((body (loop body)))
567 (make-let-values src exp
568 (make-lambda-case src2 req opt rest kw
569 inits gensyms body #f)))))
570 (($ <dynwind> src winder pre body post unwinder)
571 (let ((body (loop body)))
573 (make-dynwind src winder pre body post unwinder))))
574 (($ <dynlet> src fluids vals body)
575 (let ((body (loop body)))
577 (make-dynlet src fluids vals body))))
578 (($ <seq> src head tail)
579 (let ((tail (loop tail)))
580 (and tail (make-seq src head tail)))))))
582 (define compute-effects
583 (make-effects-analyzer assigned-lexical?))
585 (define (constant-expression? x)
586 ;; Return true if X is constant, for the purposes of copying or
587 ;; elision---i.e., if it is known to have no effects, does not
588 ;; allocate storage for a mutable object, and does not access
589 ;; mutable data (like `car' or toplevel references).
590 (constant? (compute-effects x)))
592 (define (prune-bindings ops in-order? body counter ctx build-result)
593 ;; This helper handles both `let' and `letrec'/`fix'. In the latter
594 ;; cases we need to make sure that if referenced binding A needs
595 ;; as-yet-unreferenced binding B, that B is processed for value.
596 ;; Likewise if C, when processed for effect, needs otherwise
597 ;; unreferenced D, then D needs to be processed for value too.
599 (define (referenced? op)
600 ;; When we visit lambdas in operator context, we just copy them,
601 ;; as we will process their body later. However this does have
602 ;; the problem that any free var referenced by the lambda is not
603 ;; marked as needing residualization. Here we hack around this
604 ;; and treat all bindings as referenced if we are in operator
606 (or (eq? ctx 'operator)
607 (not (zero? (operand-use-count op)))))
609 ;; values := (op ...)
610 ;; effects := (op ...)
611 (define (residualize values effects)
612 ;; Note, values and effects are reversed.
615 (let ((values (filter operand-residual-value ops)))
618 (build-result (map (compose var-name operand-var) values)
619 (map operand-sym values)
620 (map operand-residual-value values)
626 (let ((effect-vals (map operand-residual-value effects)))
627 (list->seq #f (reverse (cons body effect-vals)))))))
630 (let ((values (reverse values)))
631 (build-result (map (compose var-name operand-var) values)
632 (map operand-sym values)
633 (map operand-residual-value values)
637 ;; values := (op ...)
638 ;; effects := ((op . value) ...)
639 (let prune ((old (map referenced? ops)) (values '()) (effects '()))
640 (let lp ((ops* ops) (values values) (effects effects))
643 (let ((new (map referenced? ops)))
644 (if (not (equal? new old))
645 (prune new values '())
647 (map (lambda (op val)
648 (set-operand-residual-value! op val)
650 (map car effects) (map cdr effects))))))
652 (let ((op (car ops*)))
655 (lp (cdr ops*) values effects))
656 ((operand-residual-value op)
657 (lp (cdr ops*) (cons op values) effects))
659 (set-operand-residual-value! op (visit-operand op counter 'value))
660 (lp (cdr ops*) (cons op values) effects))
664 (let ((effect (visit-operand op counter 'effect)))
667 (acons op effect effects))))))))))))
669 (define (small-expression? x limit)
672 (lambda (x res) ; leaf
674 (lambda (x res) ; down
683 (define (extend-env sym op env)
684 (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
687 (env vlist-null) ; vhash of gensym -> <operand>
688 (counter #f) ; inlined call stack
689 (ctx 'values)) ; effect, value, values, test, operator, or call
692 ((vhash-assq var env) => cdr)
693 (else (error "unbound var" var))))
695 (define (visit exp ctx)
696 (loop exp env counter ctx))
698 (define (for-value exp) (visit exp 'value))
699 (define (for-values exp) (visit exp 'values))
700 (define (for-test exp) (visit exp 'test))
701 (define (for-effect exp) (visit exp 'effect))
702 (define (for-call exp) (visit exp 'call))
703 (define (for-tail exp) (visit exp ctx))
706 (record-effort! counter))
708 (log 'visit ctx (and=> counter effort-counter)
709 (unparse-tree-il exp))
714 ((effect) (make-void #f))
718 ((test) (make-const #f #t))
720 (($ <lexical-ref> _ _ gensym)
721 (log 'begin-copy gensym)
722 (let ((op (lookup gensym)))
725 (log 'lexical-for-effect gensym)
727 ((operand-alias-value op)
728 ;; This is an unassigned operand that simply aliases some
729 ;; other operand. Recurse to avoid residualizing the leaf
733 ;; Don't propagate copies if we are residualizing a call.
734 (log 'residualize-lexical-call gensym op)
735 (residualize-lexical op))
736 ((var-set? (operand-var op))
737 ;; Assigned lexicals don't copy-propagate.
738 (log 'assigned-var gensym op)
739 (residualize-lexical op))
740 ((not (operand-copyable? op))
741 ;; We already know that this operand is not copyable.
742 (log 'not-copyable gensym op)
743 (residualize-lexical op))
744 ((and=> (operand-constant-value op)
745 (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
747 (let ((val (operand-constant-value op)))
748 (log 'memoized-constant gensym val)
750 ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
751 recursive-effort-limit operand-size-limit)
753 ;; If we end up deciding to residualize this value instead of
754 ;; copying it, save that residualized value.
757 ((not (constant-expression? val))
758 (log 'not-constant gensym op)
759 ;; At this point, ctx is operator, test, or value. A
760 ;; value that is non-constant in one context will be
761 ;; non-constant in the others, so it's safe to record
762 ;; that here, and avoid future visits.
763 (set-operand-copyable?! op #f)
764 (residualize-lexical op ctx val))
767 (primitive-ref? val))
768 ;; Always propagate simple values that cannot lead to
770 (log 'copy-simple gensym val)
771 ;; It could be this constant is the result of folding.
772 ;; If that is the case, cache it. This helps loop
773 ;; unrolling get farther.
774 (if (or (eq? ctx 'value) (eq? ctx 'values))
776 (log 'memoize-constant gensym val)
777 (set-operand-constant-value! op val)))
779 ((= 1 (var-refcount (operand-var op)))
780 ;; Always propagate values referenced only once.
781 (log 'copy-single gensym val)
783 ;; FIXME: do demand-driven size accounting rather than
786 ;; A pure expression in the operator position. Inline
787 ;; if it's a lambda that's small enough.
788 (if (and (lambda? val)
789 (small-expression? val operator-size-limit))
791 (log 'copy-operator gensym val)
794 (log 'too-big-for-operator gensym val)
795 (residualize-lexical op ctx val))))
797 ;; A pure expression, processed for call or for value.
798 ;; Don't inline lambdas, because they will probably won't
799 ;; fold because we don't know the operator.
800 (if (and (small-expression? val value-size-limit)
801 (not (tree-il-any lambda? val)))
803 (log 'copy-value gensym val)
806 (log 'too-big-or-has-lambda gensym val)
807 (residualize-lexical op ctx val)))))))
809 ;; Visit failed. Either the operand isn't bound, as in
810 ;; lambda formal parameters, or the copy was aborted.
811 (log 'unbound-or-aborted gensym op)
812 (residualize-lexical op)))))
813 (($ <lexical-set> src name gensym exp)
814 (let ((op (lookup gensym)))
815 (if (zero? (var-refcount (operand-var op)))
816 (let ((exp (for-effect exp)))
819 (make-seq src exp (make-void #f))))
821 (record-operand-use op)
822 (make-lexical-set src name (operand-sym op) (for-value exp))))))
823 (($ <let> src names gensyms vals body)
824 (define (compute-alias exp)
825 ;; It's very common for macros to introduce something like:
827 ;; ((lambda (x y) ...) x-exp y-exp)
829 ;; In that case you might end up trying to inline something like:
831 ;; (let ((x x-exp) (y y-exp)) ...)
833 ;; But if x-exp is itself a lexical-ref that aliases some much
834 ;; larger expression, perhaps it will fail to inline due to
835 ;; size. However we don't want to introduce a useless alias
836 ;; (in this case, x). So if the RHS of a let expression is a
837 ;; lexical-ref, we record that expression. If we end up having
838 ;; to residualize X, then instead we residualize X-EXP, as long
839 ;; as it isn't assigned.
842 (($ <lexical-ref> _ _ sym)
843 (let ((op (lookup sym)))
844 (and (not (var-set? (operand-var op)))
845 (or (operand-alias-value op)
849 (let* ((vars (map lookup-var gensyms))
850 (new (fresh-gensyms vars))
851 (ops (make-bound-operands vars new vals
852 (lambda (exp counter ctx)
853 (loop exp env counter ctx))
854 (map compute-alias vals)))
855 (env (fold extend-env env gensyms ops))
856 (body (loop body env counter ctx)))
859 (for-tail (list->seq src (append vals (list body)))))
860 ((and (lexical-ref? body)
861 (memq (lexical-ref-gensym body) new))
862 (let ((sym (lexical-ref-gensym body))
863 (pairs (map cons new vals)))
864 ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
868 (append (map cdr (alist-delete sym pairs eq?))
869 (list (assq-ref pairs sym)))))))
871 ;; Only include bindings for which lexical references
872 ;; have been residualized.
873 (prune-bindings ops #f body counter ctx
874 (lambda (names gensyms vals body)
875 (if (null? names) (error "what!" names))
876 (make-let src names gensyms vals body)))))))
877 (($ <letrec> src in-order? names gensyms vals body)
878 ;; Note the difference from the `let' case: here we use letrec*
879 ;; so that the `visit' procedure for the new operands closes over
880 ;; an environment that includes the operands. Also we don't try
881 ;; to elide aliases, because we can't sensibly reduce something
882 ;; like (letrec ((a b) (b a)) a).
883 (letrec* ((visit (lambda (exp counter ctx)
884 (loop exp env* counter ctx)))
885 (vars (map lookup-var gensyms))
886 (new (fresh-gensyms vars))
887 (ops (make-bound-operands vars new vals visit))
888 (env* (fold extend-env env gensyms ops))
889 (body* (visit body counter ctx)))
890 (if (and (const? body*) (every constant-expression? vals))
891 ;; We may have folded a loop completely, even though there
892 ;; might be cyclical references between the bound values.
893 ;; Handle this degenerate case specially.
895 (prune-bindings ops in-order? body* counter ctx
896 (lambda (names gensyms vals body)
897 (make-letrec src in-order?
898 names gensyms vals body))))))
899 (($ <fix> src names gensyms vals body)
900 (letrec* ((visit (lambda (exp counter ctx)
901 (loop exp env* counter ctx)))
902 (vars (map lookup-var gensyms))
903 (new (fresh-gensyms vars))
904 (ops (make-bound-operands vars new vals visit))
905 (env* (fold extend-env env gensyms ops))
906 (body* (visit body counter ctx)))
909 (prune-bindings ops #f body* counter ctx
910 (lambda (names gensyms vals body)
911 (make-fix src names gensyms vals body))))))
912 (($ <let-values> lv-src producer consumer)
913 ;; Peval the producer, then try to inline the consumer into
914 ;; the producer. If that succeeds, peval again. Otherwise
915 ;; reconstruct the let-values, pevaling the consumer.
916 (let ((producer (for-values producer)))
918 (($ <lambda-case> src req #f #f #f () gensyms body #f)
920 ((inline-values producer src req gensyms body)
924 (make-let-values lv-src producer (for-tail consumer)))))
925 (($ <dynwind> src winder pre body post unwinder)
926 (make-dynwind src (for-value winder) (for-effect pre)
928 (for-effect post) (for-value unwinder)))
929 (($ <dynlet> src fluids vals body)
930 (make-dynlet src (map for-value fluids) (map for-value vals)
932 (($ <dynref> src fluid)
933 (make-dynref src (for-value fluid)))
934 (($ <dynset> src fluid exp)
935 (make-dynset src (for-value fluid) (for-value exp)))
936 (($ <toplevel-ref> src (? effect-free-primitive? name))
939 ;; todo: open private local bindings.
941 (($ <module-ref> src module (? effect-free-primitive? name) #f)
942 (let ((module (false-if-exception
943 (resolve-module module #:ensure #f))))
945 (let ((var (module-variable module name)))
946 (if (eq? var (module-variable the-scm-module name))
947 (make-primitive-ref src name)
952 (($ <module-set> src mod name public? exp)
953 (make-module-set src mod name public? (for-value exp)))
954 (($ <toplevel-define> src name exp)
955 (make-toplevel-define src name (for-value exp)))
956 (($ <toplevel-set> src name exp)
957 (make-toplevel-set src name (for-value exp)))
960 ((effect) (make-void #f))
961 ((test) (make-const #f #t))
963 (($ <conditional> src condition subsequent alternate)
964 (define (call-with-failure-thunk exp proc)
966 (($ <call> _ _ ()) (proc exp))
967 (($ <primcall> _ _ ()) (proc exp))
968 (($ <const>) (proc exp))
969 (($ <void>) (proc exp))
970 (($ <lexical-ref>) (proc exp))
972 (let ((t (gensym "failure-")))
973 (record-new-temporary! 'failure t 2)
975 src (list 'failure) (list t)
979 (make-lambda-case #f '() #f #f #f '() '() exp #f)))
980 (proc (make-call #f (make-lexical-ref #f 'failure t)
982 (define (simplify-conditional c)
984 ;; Swap the arms of (if (not FOO) A B), to simplify.
985 (($ <conditional> src ($ <primcall> _ 'not (pred))
986 subsequent alternate)
987 (simplify-conditional
988 (make-conditional src pred alternate subsequent)))
989 ;; Special cases for common tests in the predicates of chains
990 ;; of if expressions.
991 (($ <conditional> src
992 ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
995 (let lp ((alternate alternate))
997 ;; Lift a common repeated test out of a chain of if
999 (($ <conditional> _ (? (cut tree-il=? outer-test <>))
1000 other-subsequent alternate)
1003 (simplify-conditional
1004 (make-conditional src* inner-test inner-subsequent
1007 ;; Likewise, but punching through any surrounding
1008 ;; failure continuations.
1009 (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
1011 let-src (list name) (list sym) (list thunk)
1013 ;; Otherwise, rotate AND tests to expose a simple
1014 ;; condition in the front. Although this may result in
1015 ;; lexically binding failure thunks, the thunks will be
1016 ;; compiled to labels allocation, so there's no actual
1019 (call-with-failure-thunk
1024 (simplify-conditional
1025 (make-conditional src* inner-test inner-subsequent failure))
1028 (match (for-test condition)
1031 (for-tail subsequent)
1032 (for-tail alternate)))
1034 (simplify-conditional
1035 (make-conditional src c (for-tail subsequent)
1036 (for-tail alternate))))))
1037 (($ <primcall> src '@call-with-values
1041 ;; No optional or kwargs.
1043 _ req #f rest #f () gensyms body #f)))))
1044 (for-tail (make-let-values src (make-call src producer '())
1046 (($ <primcall> src 'dynamic-wind (w thunk u))
1049 ((not (constant-expression? w))
1051 ((not (constant-expression? u))
1052 (let ((w-sym (gensym "w ")) (u-sym (gensym "u ")))
1053 (record-new-temporary! 'w w-sym 2)
1054 (record-new-temporary! 'u u-sym 2)
1055 (make-let src '(w u) (list w-sym u-sym) (list w u)
1058 (make-lexical-ref #f 'w w-sym)
1059 (make-call #f (make-lexical-ref #f 'w w-sym) '())
1060 (make-call #f thunk '())
1061 (make-call #f (make-lexical-ref #f 'u u-sym) '())
1062 (make-lexical-ref #f 'u u-sym)))))
1064 (let ((w-sym (gensym "w ")))
1065 (record-new-temporary! 'w w-sym 2)
1066 (make-let src '(w) (list w-sym) (list w)
1069 (make-lexical-ref #f 'w w-sym)
1070 (make-call #f (make-lexical-ref #f 'w w-sym) '())
1071 (make-call #f thunk '())
1072 (make-call #f u '())
1074 ((not (constant-expression? u))
1075 (let ((u-sym (gensym "u ")))
1076 (record-new-temporary! 'u u-sym 2)
1077 (make-let src '(u) (list u-sym) (list u)
1081 (make-call #f w '())
1082 (make-call #f thunk '())
1083 (make-call #f (make-lexical-ref #f 'u u-sym) '())
1084 (make-lexical-ref #f 'u u-sym)))))
1086 (make-dynwind src w (make-call #f w '()) (make-call #f thunk '())
1087 (make-call #f u '()) u)))))
1089 (($ <primcall> src 'values exps)
1092 (if (eq? ctx 'effect)
1096 (let ((vals (map for-value exps)))
1098 ((value test effect) #t)
1099 (else (null? (cdr vals))))
1100 (every singly-valued-expression? vals))
1101 (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
1102 (make-primcall src 'values vals))))))
1104 (($ <primcall> src (or 'apply '@apply) (proc args ... tail))
1105 (match (for-value tail)
1106 (($ <const> _ (args* ...))
1107 (let ((args* (map (lambda (x) (make-const #f x)) args*)))
1108 (for-tail (make-call src proc (append args args*)))))
1109 (($ <primcall> _ 'list args*)
1110 (for-tail (make-call src proc (append args args*))))
1112 (let ((args (append (map for-value args) (list tail))))
1113 (make-primcall src '@apply (cons (for-value proc) args))))))
1115 (($ <primcall> src (? constructor-primitive? name) args)
1117 ((and (memq ctx '(effect test))
1118 (match (cons name args)
1123 ('make-prompt-tag ($ <const> _ (? string?))))
1126 ;; Some expressions can be folded without visiting the
1127 ;; arguments for value.
1128 (let ((res (if (eq? ctx 'effect)
1130 (make-const #f #t))))
1131 (for-tail (list->seq src (append args (list res))))))
1133 (match (cons name (map for-value args))
1134 (('cons x ($ <const> _ (? (cut eq? <> '()))))
1135 (make-primcall src 'list (list x)))
1136 (('cons x ($ <primcall> _ 'list elts))
1137 (make-primcall src 'list (cons x elts)))
1139 (make-primcall src name args))))))
1141 (($ <primcall> src (? accessor-primitive? name) args)
1142 (match (cons name (map for-value args))
1143 ;; FIXME: these for-tail recursions could take place outside
1144 ;; an effort counter.
1145 (('car ($ <primcall> src 'cons (head tail)))
1146 (for-tail (make-seq src tail head)))
1147 (('cdr ($ <primcall> src 'cons (head tail)))
1148 (for-tail (make-seq src head tail)))
1149 (('car ($ <primcall> src 'list (head . tail)))
1150 (for-tail (list->seq src (append tail (list head)))))
1151 (('cdr ($ <primcall> src 'list (head . tail)))
1152 (for-tail (make-seq src head (make-primcall #f 'list tail))))
1154 (('car ($ <const> src (head . tail)))
1155 (for-tail (make-const src head)))
1156 (('cdr ($ <const> src (head . tail)))
1157 (for-tail (make-const src tail)))
1158 (((or 'memq 'memv) k ($ <const> _ (elts ...)))
1163 (make-seq src k (make-void #f))))
1167 ;; A shortcut. The `else' case would handle it, but
1168 ;; this way is faster.
1169 (let ((member (case name ((memq) memq) ((memv) memv))))
1170 (make-const #f (and (member (const-exp k) elts) #t))))
1173 (make-seq src k (make-const #f #f))))
1175 (let ((t (gensym "t "))
1176 (eq (if (eq? name 'memq) 'eq? 'eqv?)))
1177 (record-new-temporary! 't t (length elts))
1180 src (list 't) (list t) (list k)
1181 (let lp ((elts elts))
1183 (make-primcall #f eq
1184 (list (make-lexical-ref #f 't t)
1185 (make-const #f (car elts)))))
1186 (if (null? (cdr elts))
1188 (make-conditional src test
1190 (lp (cdr elts)))))))))))
1194 (let ((member (case name ((memq) memq) ((memv) memv))))
1195 (make-const #f (member (const-exp k) elts))))
1197 (for-tail (make-seq src k (make-const #f #f))))
1199 (make-primcall src name (list k (make-const #f elts))))))))
1201 (fold-constants src name args ctx))))
1203 (($ <primcall> src (? equality-primitive? name) (a b))
1204 (let ((val-a (for-value a))
1205 (val-b (for-value b)))
1206 (log 'equality-primitive name val-a val-b)
1207 (cond ((and (lexical-ref? val-a) (lexical-ref? val-b)
1208 (eq? (lexical-ref-gensym val-a)
1209 (lexical-ref-gensym val-b)))
1210 (for-tail (make-const #f #t)))
1212 (fold-constants src name (list val-a val-b) ctx)))))
1214 (($ <primcall> src (? effect-free-primitive? name) args)
1215 (fold-constants src name (map for-value args) ctx))
1217 (($ <primcall> src name args)
1218 (make-primcall src name (map for-value args)))
1220 (($ <call> src orig-proc orig-args)
1221 ;; todo: augment the global env with specialized functions
1222 (let ((proc (visit orig-proc 'operator)))
1224 (($ <primitive-ref> _ name)
1225 (for-tail (make-primcall src name orig-args)))
1227 ($ <lambda-case> _ req opt #f #f inits gensyms body #f))
1228 ;; Simple case: no rest, no keyword arguments.
1229 ;; todo: handle the more complex cases
1230 (let* ((nargs (length orig-args))
1232 (nopt (if opt (length opt) 0))
1233 (key (source-expression proc)))
1235 ((or (< nargs nreq) (> nargs (+ nreq nopt)))
1236 ;; An error, or effecting arguments.
1237 (make-call src (for-call orig-proc) (map for-value orig-args)))
1238 ((or (and=> (find-counter key counter) counter-recursive?)
1239 (lambda? orig-proc))
1240 ;; A recursive call, or a lambda in the operator
1241 ;; position of the source expression. Process again in
1244 ;; In the recursive case, mark intervening counters as
1245 ;; recursive, so we can handle a toplevel counter that
1246 ;; recurses mutually with some other procedure.
1247 ;; Otherwise, the next time we see the other procedure,
1248 ;; the effort limit would be clamped to 100.
1250 (let ((found (find-counter key counter)))
1251 (if (and found (counter-recursive? found))
1252 (let lp ((counter counter))
1253 (if (not (eq? counter found))
1255 (set-counter-recursive?! counter #t)
1256 (lp (counter-prev counter)))))))
1258 (log 'inline-recurse key)
1259 (loop (make-let src (append req (or opt '()))
1262 (drop inits (- nargs nreq)))
1266 ;; An integration at the top-level, the first
1267 ;; recursion of a recursive procedure, or a nested
1268 ;; integration of a procedure that hasn't been seen
1270 (log 'inline-begin exp)
1273 (log 'inline-abort exp)
1274 (k (make-call src (for-call orig-proc)
1275 (map for-value orig-args))))
1278 ;; These first two cases will transfer effort
1279 ;; from the current counter into the new
1281 ((find-counter key counter)
1283 (make-recursive-counter recursive-effort-limit
1287 (make-nested-counter abort key counter))
1288 ;; This case opens a new account, effectively
1289 ;; printing money. It should only do so once
1290 ;; for each call site in the source program.
1292 (make-top-counter effort-limit operand-size-limit
1295 (loop (make-let src (append req (or opt '()))
1298 (drop inits (- nargs nreq)))
1300 env new-counter ctx))
1303 ;; The nested inlining attempt succeeded.
1304 ;; Deposit the unspent effort and size back
1305 ;; into the current counter.
1306 (transfer! new-counter counter))
1308 (log 'inline-end result exp)
1311 (make-call src (for-call orig-proc) (map for-value orig-args))))))
1312 (($ <lambda> src meta body)
1314 ((effect) (make-void #f))
1315 ((test) (make-const #f #t))
1317 (else (record-source-expression!
1319 (make-lambda src meta (for-values body))))))
1320 (($ <lambda-case> src req opt rest kw inits gensyms body alt)
1321 (define (lift-applied-lambda body gensyms)
1322 (and (not opt) rest (not kw)
1324 (($ <primcall> _ '@apply
1325 (($ <lambda> _ _ lcase)
1326 ($ <lexical-ref> _ _ sym)
1328 (and (equal? sym gensyms)
1329 (not (lambda-case-alternate lcase))
1332 (let* ((vars (map lookup-var gensyms))
1333 (new (fresh-gensyms vars))
1334 (env (fold extend-env env gensyms
1335 (make-unbound-operands vars new)))
1336 (new-sym (lambda (old)
1337 (operand-sym (cdr (vhash-assq old env)))))
1338 (body (loop body env counter ctx)))
1340 ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
1341 (lift-applied-lambda body new)
1342 (make-lambda-case src req opt rest
1344 ((aok? (kw name old) ...)
1345 (cons aok? (map list kw name (map new-sym old))))
1347 (map (cut loop <> env counter 'value) inits)
1350 (and alt (for-tail alt))))))
1351 (($ <seq> src head tail)
1352 (let ((head (for-effect head))
1353 (tail (for-tail tail)))
1357 (if (and (seq? head)
1358 (void? (seq-tail head)))
1362 (($ <prompt> src tag body handler)
1363 (define (make-prompt-tag? x)
1365 (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
1368 (define (find-definition x n-aliases)
1372 ((lookup (lexical-ref-gensym x))
1374 (let ((y (or (operand-residual-value op)
1375 (visit-operand op counter 'value 10 10))))
1377 ((and (lexical-ref? y)
1378 (= (lexical-refcount (lexical-ref-gensym x)) 1))
1379 ;; X is a simple alias for Y. Recurse, regardless of
1380 ;; the number of aliases we were expecting.
1381 (find-definition y n-aliases))
1382 ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
1383 ;; We found a definition that is aliased the right
1384 ;; number of times. We still recurse in case it is a
1386 (values (find-definition y 1)
1389 ;; We can't account for our aliases.
1392 ;; A formal parameter. Can't say anything about that.
1395 ;; Not a lexical: success, but only if we are looking for an
1398 (else (values #f #f))))
1400 (let ((tag (for-value tag))
1401 (body (for-tail body)))
1403 ((find-definition tag 1)
1405 (make-prompt-tag? val))
1407 ;; There is no way that an <abort> could know the tag
1408 ;; for this <prompt>, so we can elide the <prompt>
1410 (unrecord-operand-uses op 1)
1412 ((find-definition tag 2)
1414 (and (make-prompt-tag? val)
1416 (tree-il=? (abort-tag body) tag)))
1418 ;; (let ((t (make-prompt-tag)))
1419 ;; (call-with-prompt t
1420 ;; (lambda () (abort-to-prompt t val ...))
1421 ;; (lambda (k arg ...) e ...)))
1422 ;; => (let-values (((k arg ...) (values values val ...)))
1424 (unrecord-operand-uses op 2)
1428 (make-primcall #f 'apply
1429 `(,(make-primitive-ref #f 'values)
1430 ,(make-primitive-ref #f 'values)
1432 ,(abort-tail body)))
1433 (for-value handler)))))
1435 (make-prompt src tag body (for-value handler))))))
1436 (($ <abort> src tag args tail)
1437 (make-abort src (for-value tag) (map for-value args)
1438 (for-value tail))))))