Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / module / language / tree-il / peval.scm
index 8246e47..fe637f0 100644 (file)
@@ -1,6 +1,6 @@
 ;;; Tree-IL partial evaluator
 
-;; Copyright (C) 2011 Free Software Foundation, Inc.
+;; Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
 
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
@@ -19,6 +19,7 @@
 (define-module (language tree-il peval)
   #:use-module (language tree-il)
   #:use-module (language tree-il primitives)
+  #:use-module (language tree-il effects)
   #:use-module (ice-9 vlist)
   #:use-module (ice-9 match)
   #:use-module (srfi srfi-1)
 
 ;; First, some helpers.
 ;;
+(define-syntax *logging* (identifier-syntax #f))
+
 ;; For efficiency we define *logging* to inline to #f, so that the call
-;; to log* gets optimized out.  If you want to log, do:
+;; to log* gets optimized out.  If you want to log, uncomment these
+;; lines:
 ;;
-;;   (define %logging #f)
-;;   (define-syntax *logging* (identifier-syntax %logging)
+;; (define %logging #f)
+;; (define-syntax *logging* (identifier-syntax %logging))
 ;;
 ;; Then you can change %logging at runtime.
-;;
-(define-syntax *logging* (identifier-syntax #f))
 
 (define-syntax log
   (syntax-rules (quote)
            (or (proc (vlist-ref vlist i))
                (lp (1+ i)))))))
 
+(define (singly-valued-expression? exp)
+  (match exp
+    (($ <const>) #t)
+    (($ <lexical-ref>) #t)
+    (($ <void>) #t)
+    (($ <lexical-ref>) #t)
+    (($ <primitive-ref>) #t)
+    (($ <module-ref>) #t)
+    (($ <toplevel-ref>) #t)
+    (($ <primcall> _ (? singly-valued-primitive?)) #t)
+    (($ <primcall> _ 'values (val)) #t)
+    (($ <lambda>) #t)
+    (else #f)))
+
+(define (truncate-values x)
+  "Discard all but the first value of X."
+  (if (singly-valued-expression? x)
+      x
+      (make-primcall (tree-il-src x) 'values (list x))))
+
 ;; Peval will do a one-pass analysis on the source program to determine
 ;; the set of assigned lexicals, and to identify unreferenced and
 ;; singly-referenced lexicals.
 ;;
-;; If peval introduces more code, via copy-propagation, it will need to
-;; run `build-var-table' on the new code to add to make sure it can find
-;; a <var> for each gensym bound in the program.
-;;
 (define-record-type <var>
   (make-var name gensym refcount set?)
   var?
    (lambda (exp res)
      (match exp
        (($ <lexical-ref> src name gensym)
-        (let ((var (vhash-assq gensym res)))
-          (if var
-              (begin
-                (set-var-refcount! (cdr var) (1+ (var-refcount (cdr var))))
-                res)
-              (vhash-consq gensym (make-var name gensym 1 #f) res))))
+        (let ((var (cdr (vhash-assq gensym res))))
+          (set-var-refcount! var (1+ (var-refcount var)))
+          res))
        (_ res)))
    (lambda (exp res)
      (match exp
+       (($ <lambda-case> src req opt rest kw init gensyms body alt)
+        (fold (lambda (name sym res)
+                (vhash-consq sym (make-var name sym 0 #f) res))
+              res
+              (append req (or opt '()) (if rest (list rest) '())
+                      (match kw
+                        ((aok? (kw name sym) ...) name)
+                        (_ '())))
+              gensyms))
+       (($ <let> src names gensyms vals body)
+        (fold (lambda (name sym res)
+                (vhash-consq sym (make-var name sym 0 #f) res))
+              res names gensyms))
+       (($ <letrec> src in-order? names gensyms vals body)
+        (fold (lambda (name sym res)
+                (vhash-consq sym (make-var name sym 0 #f) res))
+              res names gensyms))
+       (($ <fix> src names gensyms vals body)
+        (fold (lambda (name sym res)
+                (vhash-consq sym (make-var name sym 0 #f) res))
+              res names gensyms))
        (($ <lexical-set> src name gensym exp)
-        (let ((var (vhash-assq gensym res)))
-          (if var
-              (begin
-                (set-var-set?! (cdr var) #t)
-                res)
-              (vhash-consq gensym (make-var name gensym 0 #t) res))))
+        (set-var-set?! (cdr (vhash-assq gensym res)) #t)
+        res)
        (_ res)))
    (lambda (exp res) res)
    table exp))
   (effort effort-counter)
   (size size-counter)
   (continuation counter-continuation)
-  (recursive? counter-recursive?)
+  (recursive? counter-recursive? set-counter-recursive?!)
   (data counter-data)
   (prev counter-prev))
 
     (transfer! current c effort-limit size-limit)
     c))
 
+;; Operand structures allow bindings to be processed lazily instead of
+;; eagerly.  By doing so, hopefully we can get process them in a way
+;; appropriate to their use contexts.  Operands also prevent values from
+;; being visited multiple times, wasting effort.
+;;
+;; TODO: Record value size in operand structure?
+;; 
+(define-record-type <operand>
+  (%make-operand var sym visit source visit-count use-count
+                 copyable? residual-value constant-value alias-value)
+  operand?
+  (var operand-var)
+  (sym operand-sym)
+  (visit %operand-visit)
+  (source operand-source)
+  (visit-count operand-visit-count set-operand-visit-count!)
+  (use-count operand-use-count set-operand-use-count!)
+  (copyable? operand-copyable? set-operand-copyable?!)
+  (residual-value operand-residual-value %set-operand-residual-value!)
+  (constant-value operand-constant-value set-operand-constant-value!)
+  (alias-value operand-alias-value set-operand-alias-value!))
+
+(define* (make-operand var sym #:optional source visit alias)
+  ;; Bind SYM to VAR, with value SOURCE.  Unassigned bound operands are
+  ;; considered copyable until we prove otherwise.  If we have a source
+  ;; expression, truncate it to one value.  Copy propagation does not
+  ;; work on multiply-valued expressions.
+  (let ((source (and=> source truncate-values)))
+    (%make-operand var sym visit source 0 0
+                   (and source (not (var-set? var))) #f #f
+                   (and (not (var-set? var)) alias))))
+
+(define* (make-bound-operands vars syms sources visit #:optional aliases)
+  (if aliases
+      (map (lambda (name sym source alias)
+             (make-operand name sym source visit alias))
+           vars syms sources aliases)
+      (map (lambda (name sym source)
+             (make-operand name sym source visit #f))
+           vars syms sources)))
+
+(define (make-unbound-operands vars syms)
+  (map make-operand vars syms))
+
+(define (set-operand-residual-value! op val)
+  (%set-operand-residual-value!
+   op
+   (match val
+    (($ <primcall> src 'values (first))
+     ;; The continuation of a residualized binding does not need the
+     ;; introduced `values' node, so undo the effects of truncation.
+     first)
+    (else
+     val))))
+
+(define* (visit-operand op counter ctx #:optional effort-limit size-limit)
+  ;; Peval is O(N) in call sites of the source program.  However,
+  ;; visiting an operand can introduce new call sites.  If we visit an
+  ;; operand outside a counter -- i.e., outside an inlining attempt --
+  ;; this can lead to divergence.  So, if we are visiting an operand to
+  ;; try to copy it, and there is no counter, make a new one.
+  ;;
+  ;; This will only happen at most as many times as there are lexical
+  ;; references in the source program.
+  (and (zero? (operand-visit-count op))
+       (dynamic-wind
+         (lambda ()
+           (set-operand-visit-count! op (1+ (operand-visit-count op))))
+         (lambda ()
+           (and (operand-source op)
+                (if (or counter (and (not effort-limit) (not size-limit)))
+                    ((%operand-visit op) (operand-source op) counter ctx)
+                    (let/ec k
+                      (define (abort)
+                        ;; If we abort when visiting the value in a
+                        ;; fresh context, we won't succeed in any future
+                        ;; attempt, so don't try to copy it again.
+                        (set-operand-copyable?! op #f)
+                        (k #f))
+                      ((%operand-visit op)
+                       (operand-source op) 
+                       (make-top-counter effort-limit size-limit abort op)
+                       ctx)))))
+         (lambda ()
+           (set-operand-visit-count! op (1- (operand-visit-count op)))))))
+
+;; A helper for constant folding.
+;;
 (define (types-check? primitive-name args)
   (case primitive-name
     ((values) #t)
     ;; FIXME: add more cases?
     (else #f)))
 
-(define (fresh-gensyms syms)
-  (map (lambda (x) (gensym (string-append (symbol->string x) " ")))
-       syms))
-
-;; Copy propagation of terms that bind variables, like `lambda' terms,
-;; will need to bind fresh variables.  This procedure renames all the
-;; lexicals in a term.
-;;
-(define (alpha-rename exp)
-  "Alpha-rename EXP.  For any lambda in EXP, generate new symbols and
-replace all lexical references to the former symbols with lexical
-references to the new symbols."
-  ;; XXX: This should be factorized somehow.
-  (let loop ((exp     exp)
-             (mapping vlist-null))             ; maps old to new gensyms
-    (match exp
-      (($ <lambda-case> src req opt rest kw inits gensyms body alt)
-       ;; Create new symbols to replace GENSYMS and propagate them down
-       ;; in BODY and ALT.
-       (let* ((new     (fresh-gensyms
-                        (append req
-                                (or opt '())
-                                (if rest (list rest) '())
-                                (match kw
-                                  ((aok? (_ name _) ...) name)
-                                  (_ '())))))
-              (mapping (fold vhash-consq mapping gensyms new)))
-         (make-lambda-case src req opt rest
-                           (match kw
-                             ((aok? (kw name old) ...)
-                              (cons aok? (map list
-                                              kw
-                                              name
-                                              (take-right new (length old)))))
-                             (_ #f))
-                           (map (cut loop <> mapping) inits)
-                           new
-                           (loop body mapping)
-                           (and alt (loop alt mapping)))))
-      (($ <lexical-ref> src name gensym)
-       ;; Possibly replace GENSYM by the new gensym defined in MAPPING.
-       (let ((val (vhash-assq gensym mapping)))
-         (if val
-             (make-lexical-ref src name (cdr val))
-             exp)))
-      (($ <lexical-set> src name gensym exp)
-       (let ((val (vhash-assq gensym mapping)))
-         (make-lexical-set src name (if val (cdr val) gensym)
-                           (loop exp mapping))))
-      (($ <lambda> src meta body)
-       (make-lambda src meta (loop body mapping)))
-      (($ <let> src names gensyms vals body)
-       ;; As for `lambda-case' rename GENSYMS to avoid any collision.
-       (let* ((new     (fresh-gensyms names))
-              (mapping (fold vhash-consq mapping gensyms new))
-              (vals    (map (cut loop <> mapping) vals))
-              (body    (loop body mapping)))
-         (make-let src names new vals body)))
-      (($ <letrec> src in-order? names gensyms vals body)
-       ;; Likewise.
-       (let* ((new     (fresh-gensyms names))
-              (mapping (fold vhash-consq mapping gensyms new))
-              (vals    (map (cut loop <> mapping) vals))
-              (body    (loop body mapping)))
-         (make-letrec src in-order? names new vals body)))
-      (($ <fix> src names gensyms vals body)
-       ;; Likewise.
-       (let* ((new     (fresh-gensyms names))
-              (mapping (fold vhash-consq mapping gensyms new))
-              (vals    (map (cut loop <> mapping) vals))
-              (body    (loop body mapping)))
-         (make-fix src names new vals body)))
-      (($ <let-values> src exp body)
-       (make-let-values src (loop exp mapping) (loop body mapping)))
-      (($ <const>)
-       exp)
-      (($ <void>)
-       exp)
-      (($ <toplevel-ref>)
-       exp)
-      (($ <module-ref>)
-       exp)
-      (($ <primitive-ref>)
-       exp)
-      (($ <toplevel-set> src name exp)
-       (make-toplevel-set src name (loop exp mapping)))
-      (($ <toplevel-define> src name exp)
-       (make-toplevel-define src name (loop exp mapping)))
-      (($ <module-set> src mod name public? exp)
-       (make-module-set src mod name public? (loop exp mapping)))
-      (($ <dynlet> src fluids vals body)
-       (make-dynlet src
-                    (map (cut loop <> mapping) fluids)
-                    (map (cut loop <> mapping) vals)
-                    (loop body mapping)))
-      (($ <dynwind> src winder body unwinder)
-       (make-dynwind src
-                     (loop winder mapping)
-                     (loop body mapping)
-                     (loop unwinder mapping)))
-      (($ <dynref> src fluid)
-       (make-dynref src (loop fluid mapping)))
-      (($ <dynset> src fluid exp)
-       (make-dynset src (loop fluid mapping) (loop exp mapping)))
-      (($ <conditional> src condition subsequent alternate)
-       (make-conditional src
-                         (loop condition mapping)
-                         (loop subsequent mapping)
-                         (loop alternate mapping)))
-      (($ <application> src proc args)
-       (make-application src (loop proc mapping)
-                         (map (cut loop <> mapping) args)))
-      (($ <sequence> src exps)
-       (make-sequence src (map (cut loop <> mapping) exps)))
-      (($ <prompt> src tag body handler)
-       (make-prompt src (loop tag mapping) (loop body mapping)
-                    (loop handler mapping)))
-      (($ <abort> src tag args tail)
-       (make-abort src (loop tag mapping) (map (cut loop <> mapping) args)
-                   (loop tail mapping))))))
-
 (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
                 #:key
                 (operator-size-limit 40)
@@ -399,18 +398,15 @@ top-level bindings from ENV and return the resulting expression."
 
   (define local-toplevel-env
     ;; The top-level environment of the module being compiled.
-    (match exp
-      (($ <toplevel-define> _ name)
-       (vhash-consq name #t env))
-      (($ <sequence> _ exps)
-       (fold (lambda (x r)
-               (match x
-                 (($ <toplevel-define> _ name)
-                  (vhash-consq name #t r))
-                 (_ r)))
-             env
-             exps))
-      (_ env)))
+    (let ()
+      (define (env-folder x env)
+        (match x
+          (($ <toplevel-define> _ name)
+           (vhash-consq name #t env))
+          (($ <seq> _ head tail)
+           (env-folder tail (env-folder head env)))
+          (_ env)))
+      (env-folder exp vlist-null)))
 
   (define (local-toplevel? name)
     (vhash-assq name local-toplevel-env))
@@ -420,21 +416,39 @@ top-level bindings from ENV and return the resulting expression."
   ;;
   (define store (build-var-table exp))
 
-  (define (assigned-lexical? sym)
+  (define (record-new-temporary! name sym refcount)
+    (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
+
+  (define (lookup-var sym)
     (let ((v (vhash-assq sym store)))
-      (and v (var-set? (cdr v)))))
+      (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
+
+  (define (fresh-gensyms vars)
+    (map (lambda (var)
+           (let ((new (gensym (string-append (symbol->string (var-name var))
+                                             " "))))
+             (set! store (vhash-consq new var store))
+             new))
+         vars))
+
+  (define (fresh-temporaries ls)
+    (map (lambda (elt)
+           (let ((new (gensym "tmp ")))
+             (record-new-temporary! 'tmp new 1)
+             new))
+         ls))
+
+  (define (assigned-lexical? sym)
+    (var-set? (lookup-var sym)))
 
   (define (lexical-refcount sym)
-    (let ((v (vhash-assq sym store)))
-      (if v (var-refcount (cdr v)) 0)))
+    (var-refcount (lookup-var sym)))
 
   ;; ORIG has been alpha-renamed to NEW.  Analyze NEW and record a link
   ;; from it to ORIG.
   ;;
   (define (record-source-expression! orig new)
-    (set! store (vhash-consq new
-                             (source-expression orig)
-                             (build-var-table new store)))
+    (set! store (vhash-consq new (source-expression orig) store))
     new)
 
   ;; Find the source expression corresponding to NEW.  Used to detect
@@ -444,24 +458,64 @@ top-level bindings from ENV and return the resulting expression."
     (let ((x (vhash-assq new store)))
       (if x (cdr x) new)))
 
-  (define residual-lexical-references (make-hash-table))
+  (define (record-operand-use op)
+    (set-operand-use-count! op (1+ (operand-use-count op))))
 
-  (define (record-residual-lexical-reference! sym)
-    (hashq-set! residual-lexical-references sym #t))
+  (define (unrecord-operand-uses op n)
+    (let ((count (- (operand-use-count op) n)))
+      (when (zero? count)
+        (set-operand-residual-value! op #f))
+      (set-operand-use-count! op count)))
 
-  (define (apply-primitive name args)
-    ;; todo: further optimize commutative primitives
-    (catch #t
-      (lambda ()
-        (call-with-values
-            (lambda ()
-              (apply (module-ref the-scm-module name) args))
-          (lambda results
-            (values #t results))))
-      (lambda _
-        (values #f '()))))
+  (define* (residualize-lexical op #:optional ctx val)
+    (log 'residualize op)
+    (record-operand-use op)
+    (if (memq ctx '(value values))
+        (set-operand-residual-value! op val))
+    (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
+
+  (define (fold-constants src name args ctx)
+    (define (apply-primitive name args)
+      ;; todo: further optimize commutative primitives
+      (catch #t
+        (lambda ()
+          (call-with-values
+              (lambda ()
+                (apply (module-ref the-scm-module name) args))
+            (lambda results
+              (values #t results))))
+        (lambda _
+          (values #f '()))))
+    (define (make-values src values)
+      (match values
+        ((single) single)               ; 1 value
+        ((_ ...)                        ; 0, or 2 or more values
+         (make-primcall src 'values values))))
+    (define (residualize-call)
+      (make-primcall src name args))
+    (cond
+     ((every const? args)
+      (let-values (((success? values)
+                    (apply-primitive name (map const-exp args))))
+        (log 'fold success? values name args)
+        (if success?
+            (case ctx
+              ((effect) (make-void src))
+              ((test)
+               ;; Values truncation: only take the first
+               ;; value.
+               (if (pair? values)
+                   (make-const src (car values))
+                   (make-values src '())))
+              (else
+               (make-values src (map (cut make-const src <>) values))))
+            (residualize-call))))
+     ((and (eq? ctx 'effect) (types-check? name args))
+      (make-void #f))
+     (else
+      (residualize-call))))
 
-  (define (inline-values exp src names gensyms body)
+  (define (inline-values src exp nmin nmax consumer)
     (let loop ((exp exp))
       (match exp
         ;; Some expression types are always singly-valued.
@@ -477,24 +531,22 @@ top-level bindings from ENV and return the resulting expression."
              ($ <toplevel-set>)         ; could return zero values in
              ($ <toplevel-define>)      ; the future
              ($ <module-set>)           ;
-             ($ <dynset>))              ; 
-         (and (= (length names) 1)
-              (make-let src names gensyms (list exp) body)))
-        (($ <application> src
-            ($ <primitive-ref> _ (? singly-valued-primitive? name)))
-         (and (= (length names) 1)
-              (make-let src names gensyms (list exp) body)))
+             ($ <dynset>)               ;
+             ($ <primcall> src (? singly-valued-primitive?)))
+         (and (<= nmin 1) (or (not nmax) (>= nmax 1))
+              (make-call src (make-lambda #f '() consumer) (list exp))))
 
         ;; Statically-known number of values.
-        (($ <application> src ($ <primitive-ref> _ 'values) vals)
-         (and (= (length names) (length vals))
-              (make-let src names gensyms vals body)))
+        (($ <primcall> src 'values vals)
+         (and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals)))
+              (make-call src (make-lambda #f '() consumer) vals)))
 
         ;; Not going to copy code into both branches.
         (($ <conditional>) #f)
 
         ;; Bail on other applications.
-        (($ <application>) #f)
+        (($ <call>) #f)
+        (($ <primcall>) #f)
 
         ;; Bail on prompt and abort.
         (($ <prompt>) #f)
@@ -520,93 +572,104 @@ top-level bindings from ENV and return the resulting expression."
                 (make-let-values src exp
                                  (make-lambda-case src2 req opt rest kw
                                                    inits gensyms body #f)))))
-        (($ <dynwind> src winder body unwinder)
+        (($ <dynwind> src winder pre body post unwinder)
          (let ((body (loop body)))
            (and body
-                (make-dynwind src winder body unwinder))))
+                (make-dynwind src winder pre body post unwinder))))
         (($ <dynlet> src fluids vals body)
          (let ((body (loop body)))
            (and body
                 (make-dynlet src fluids vals body))))
-        (($ <sequence> src exps)
-         (match exps
-           ((head ... tail)
-            (let ((tail (loop tail)))
-              (and tail
-                   (make-sequence src (append head (list tail)))))))))))
-
-  (define (make-values src values)
-    (match values
-      ((single) single)                 ; 1 value
-      ((_ ...)                          ; 0, or 2 or more values
-       (make-application src (make-primitive-ref src 'values)
-                         values))))
+        (($ <seq> src head tail)
+         (let ((tail (loop tail)))
+           (and tail (make-seq src head tail)))))))
+
+  (define compute-effects
+    (make-effects-analyzer assigned-lexical?))
 
   (define (constant-expression? x)
-    ;; Return true if X is constant---i.e., if it is known to have no
-    ;; effects, does not allocate storage for a mutable object, and does
-    ;; not access mutable data (like `car' or toplevel references).
-    (let loop ((x x))
-      (match x
-        (($ <void>) #t)
-        (($ <const>) #t)
-        (($ <lambda>) #t)
-        (($ <lambda-case> _ req opt rest kw inits _ body alternate)
-         (and (every loop inits) (loop body)
-              (or (not alternate) (loop alternate))))
-        (($ <lexical-ref> _ _ gensym)
-         (not (assigned-lexical? gensym)))
-        (($ <primitive-ref>) #t)
-        (($ <conditional> _ condition subsequent alternate)
-         (and (loop condition) (loop subsequent) (loop alternate)))
-        (($ <application> _ ($ <primitive-ref> _ name) args)
-         (and (effect-free-primitive? name)
-              (not (constructor-primitive? name))
-              (types-check? name args)
-              (every loop args)))
-        (($ <application> _ ($ <lambda> _ _ body) args)
-         (and (loop body) (every loop args)))
-        (($ <sequence> _ exps)
-         (every loop exps))
-        (($ <let> _ _ _ vals body)
-         (and (every loop vals) (loop body)))
-        (($ <letrec> _ _ _ _ vals body)
-         (and (every loop vals) (loop body)))
-        (($ <fix> _ _ _ vals body)
-         (and (every loop vals) (loop body)))
-        (($ <let-values> _ exp body)
-         (and (loop exp) (loop body)))
-        (($ <prompt> _ tag body handler)
-         (and (loop tag) (loop body) (loop handler)))
-        (_ #f))))
-
-  (define (prune-bindings names syms vals body for-effect
-                          build-result)
-    (let lp ((names names) (syms syms) (vals vals)
-             (names* '()) (syms* '()) (vals* '())
-             (effects '()))
-      (match (list names syms vals)
-       ((() () ())
-        (let ((body (if (null? effects)
-                        body
-                        (make-sequence #f (reverse (cons body effects))))))
-          (if (null? names*)
+    ;; Return true if X is constant, for the purposes of copying or
+    ;; elision---i.e., if it is known to have no effects, does not
+    ;; allocate storage for a mutable object, and does not access
+    ;; mutable data (like `car' or toplevel references).
+    (constant? (compute-effects x)))
+
+  (define (prune-bindings ops in-order? body counter ctx build-result)
+    ;; This helper handles both `let' and `letrec'/`fix'.  In the latter
+    ;; cases we need to make sure that if referenced binding A needs
+    ;; as-yet-unreferenced binding B, that B is processed for value.
+    ;; Likewise if C, when processed for effect, needs otherwise
+    ;; unreferenced D, then D needs to be processed for value too.
+    ;;
+    (define (referenced? op)
+      ;; When we visit lambdas in operator context, we just copy them,
+      ;; as we will process their body later.  However this does have
+      ;; the problem that any free var referenced by the lambda is not
+      ;; marked as needing residualization.  Here we hack around this
+      ;; and treat all bindings as referenced if we are in operator
+      ;; context.
+      (or (eq? ctx 'operator)
+          (not (zero? (operand-use-count op)))))
+    
+    ;; values := (op ...)
+    ;; effects := (op ...)
+    (define (residualize values effects)
+      ;; Note, values and effects are reversed.
+      (cond
+       (in-order?
+        (let ((values (filter operand-residual-value ops)))
+          (if (null? values)
+              body
+              (build-result (map (compose var-name operand-var) values)
+                            (map operand-sym values)
+                            (map operand-residual-value values)
+                            body))))
+       (else
+        (let ((body
+               (if (null? effects)
+                   body
+                   (let ((effect-vals (map operand-residual-value effects)))
+                     (list->seq #f (reverse (cons body effect-vals)))))))
+          (if (null? values)
               body
-              (build-result (reverse names*) (reverse syms*)
-                            (reverse vals*) body))))
-       (((name . names) (sym . syms) (val . vals))
-        (if (hashq-ref residual-lexical-references sym)
-            (lp names syms vals
-                (cons name names*) (cons sym syms*) (cons val vals*)
-                effects)
-            (let ((effect (for-effect val)))
-              (lp names syms vals
-                  names* syms* vals*
-                  (if (void? effect)
-                      (begin
-                        (log 'prune sym)
-                        effects)
-                      (cons effect effects)))))))))
+              (let ((values (reverse values)))
+                (build-result (map (compose var-name operand-var) values)
+                              (map operand-sym values)
+                              (map operand-residual-value values)
+                              body)))))))
+
+    ;; old := (bool ...)
+    ;; values := (op ...)
+    ;; effects := ((op . value) ...)
+    (let prune ((old (map referenced? ops)) (values '()) (effects '()))
+      (let lp ((ops* ops) (values values) (effects effects))
+        (cond
+         ((null? ops*)
+          (let ((new (map referenced? ops)))
+            (if (not (equal? new old))
+                (prune new values '())
+                (residualize values
+                             (map (lambda (op val)
+                                    (set-operand-residual-value! op val)
+                                    op)
+                                  (map car effects) (map cdr effects))))))
+         (else
+          (let ((op (car ops*)))
+            (cond
+             ((memq op values)
+              (lp (cdr ops*) values effects))
+             ((operand-residual-value op)
+              (lp (cdr ops*) (cons op values) effects))
+             ((referenced? op)
+              (set-operand-residual-value! op (visit-operand op counter 'value))
+              (lp (cdr ops*) (cons op values) effects))
+             (else
+              (lp (cdr ops*)
+                  values
+                  (let ((effect (visit-operand op counter 'effect)))
+                    (if (void? effect)
+                        effects
+                        (acons op effect effects))))))))))))
   
   (define (small-expression? x limit)
     (let/ec k
@@ -622,20 +685,69 @@ top-level bindings from ENV and return the resulting expression."
        0 x)
       #t))
   
+  (define (extend-env sym op env)
+    (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
+      
   (let loop ((exp   exp)
-             (env   vlist-null)         ; static environment
+             (env   vlist-null)         ; vhash of gensym -> <operand>
              (counter #f)               ; inlined call stack
-             (ctx 'value))   ; effect, value, test, operator, or operand
+             (ctx 'values))   ; effect, value, values, test, operator, or call
     (define (lookup var)
-      (and=> (vhash-assq var env) cdr))
+      (cond 
+       ((vhash-assq var env) => cdr)
+       (else (error "unbound var" var))))
+
+    ;; Find a value referenced a specific number of times.  This is a hack
+    ;; that's used for propagating fresh data structures like rest lists and
+    ;; prompt tags.  Usually we wouldn't copy consed data, but we can do so in
+    ;; some special cases like `apply' or prompts if we can account
+    ;; for all of its uses.
+    ;;
+    ;; You don't want to use this in general because it introduces a slight
+    ;; nonlinearity by running peval again (though with a small effort and size
+    ;; counter).
+    ;;
+    (define (find-definition x n-aliases)
+      (cond
+       ((lexical-ref? x)
+        (cond
+         ((lookup (lexical-ref-gensym x))
+          => (lambda (op)
+               (let ((y (or (operand-residual-value op)
+                            (visit-operand op counter 'value 10 10)
+                            (operand-source op))))
+                 (cond
+                  ((and (lexical-ref? y)
+                        (= (lexical-refcount (lexical-ref-gensym x)) 1))
+                   ;; X is a simple alias for Y.  Recurse, regardless of
+                   ;; the number of aliases we were expecting.
+                   (find-definition y n-aliases))
+                  ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
+                   ;; We found a definition that is aliased the right
+                   ;; number of times.  We still recurse in case it is a
+                   ;; lexical.
+                   (values (find-definition y 1)
+                           op))
+                  (else
+                   ;; We can't account for our aliases.
+                   (values #f #f))))))
+         (else
+          ;; A formal parameter.  Can't say anything about that.
+          (values #f #f))))
+       ((= n-aliases 1)
+        ;; Not a lexical: success, but only if we are looking for an
+        ;; unaliased value.
+        (values x #f))
+       (else (values #f #f))))
 
     (define (visit exp ctx)
       (loop exp env counter ctx))
 
     (define (for-value exp)    (visit exp 'value))
-    (define (for-operand exp)  (visit exp 'operand))
+    (define (for-values exp)   (visit exp 'values))
     (define (for-test exp)     (visit exp 'test))
     (define (for-effect exp)   (visit exp 'effect))
+    (define (for-call exp)     (visit exp 'call))
     (define (for-tail exp)     (visit exp ctx))
 
     (if counter
@@ -654,158 +766,240 @@ top-level bindings from ENV and return the resulting expression."
          ((test) (make-const #f #t))
          (else exp)))
       (($ <lexical-ref> _ _ gensym)
-       (case ctx
-         ((effect) (make-void #f))
-         (else
-          (log 'begin-copy gensym)
-          (let ((val (lookup gensym)))
-            (cond
-             ((or (not val)
-                  (assigned-lexical? gensym)
-                  (not (constant-expression? val)))
-              ;; Don't copy-propagate through assigned variables,
-              ;; and don't reorder effects.
-              (log 'unbound-or-not-constant gensym val)
-              (record-residual-lexical-reference! gensym)
-              exp)
-             ((lexical-ref? val)
-              (for-tail val))
-             ((or (const? val)
-                  (void? val)
-                  (primitive-ref? val))
-              ;; Always propagate simple values that cannot lead to
-              ;; code bloat.
-              (log 'copy-simple gensym val)
-              (for-tail val))
-             ((= 1 (lexical-refcount gensym))
-              ;; Always propagate values referenced only once.
-              ;; There is no need to rename the bindings, as they
-              ;; are only being moved, not copied.  However in
-              ;; operator context we do rename it, as that
-              ;; effectively clears out the residualized-lexical
-              ;; flags that may have been set when this value was
-              ;; visited previously as an operand.
-              (log 'copy-single gensym val)
-              (case ctx
-                ((test) (for-test val))
-                ((operator) (record-source-expression! val (alpha-rename val)))
-                (else val)))
-             ;; FIXME: do demand-driven size accounting rather than
-             ;; these heuristics.
-             ((eq? ctx 'operator)
-              ;; A pure expression in the operator position.  Inline
-              ;; if it's a lambda that's small enough.
-              (if (and (lambda? val)
-                       (small-expression? val operator-size-limit))
-                  (begin
-                    (log 'copy-operator gensym val)
-                    (record-source-expression! val (alpha-rename val)))
-                  (begin
-                    (log 'too-big-for-operator gensym val)
-                    (record-residual-lexical-reference! gensym)
-                    exp)))
-             ((eq? ctx 'operand)
-              ;; A pure expression in the operand position.  Inline
-              ;; if it's small enough.
-              (if (small-expression? val operand-size-limit)
-                  (begin
-                    (log 'copy-operand gensym val)
-                    (record-source-expression! val (alpha-rename val)))
-                  (begin
-                    (log 'too-big-for-operand gensym val)
-                    (record-residual-lexical-reference! gensym)
-                    exp)))
-             (else
-              ;; A pure expression, processed for value.  Don't
-              ;; inline lambdas, because they will probably won't
-              ;; fold because we don't know the operator.
-              (if (and (small-expression? val value-size-limit)
-                       (not (tree-il-any lambda? val)))
-                  (begin
-                    (log 'copy-value gensym val)
-                    (record-source-expression! val (alpha-rename val)))
-                  (begin
-                    (log 'too-big-or-has-lambda gensym val)
-                    (record-residual-lexical-reference! gensym)
-                    exp))))))))
+       (log 'begin-copy gensym)
+       (let ((op (lookup gensym)))
+         (cond
+          ((eq? ctx 'effect)
+           (log 'lexical-for-effect gensym)
+           (make-void #f))
+          ((operand-alias-value op)
+           ;; This is an unassigned operand that simply aliases some
+           ;; other operand.  Recurse to avoid residualizing the leaf
+           ;; binding.
+           => for-tail)
+          ((eq? ctx 'call)
+           ;; Don't propagate copies if we are residualizing a call.
+           (log 'residualize-lexical-call gensym op)
+           (residualize-lexical op))
+          ((var-set? (operand-var op))
+           ;; Assigned lexicals don't copy-propagate.
+           (log 'assigned-var gensym op)
+           (residualize-lexical op))
+          ((not (operand-copyable? op))
+           ;; We already know that this operand is not copyable.
+           (log 'not-copyable gensym op)
+           (residualize-lexical op))
+          ((and=> (operand-constant-value op)
+                  (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
+           ;; A cache hit.
+           (let ((val (operand-constant-value op)))
+             (log 'memoized-constant gensym val)
+             (for-tail val)))
+          ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
+                          recursive-effort-limit operand-size-limit)
+           =>
+           ;; If we end up deciding to residualize this value instead of
+           ;; copying it, save that residualized value.
+           (lambda (val)
+             (cond
+              ((not (constant-expression? val))
+               (log 'not-constant gensym op)
+               ;; At this point, ctx is operator, test, or value.  A
+               ;; value that is non-constant in one context will be
+               ;; non-constant in the others, so it's safe to record
+               ;; that here, and avoid future visits.
+               (set-operand-copyable?! op #f)
+               (residualize-lexical op ctx val))
+              ((or (const? val)
+                   (void? val)
+                   (primitive-ref? val))
+               ;; Always propagate simple values that cannot lead to
+               ;; code bloat.
+               (log 'copy-simple gensym val)
+               ;; It could be this constant is the result of folding.
+               ;; If that is the case, cache it.  This helps loop
+               ;; unrolling get farther.
+               (if (or (eq? ctx 'value) (eq? ctx 'values))
+                   (begin
+                     (log 'memoize-constant gensym val)
+                     (set-operand-constant-value! op val)))
+               val)
+              ((= 1 (var-refcount (operand-var op)))
+               ;; Always propagate values referenced only once.
+               (log 'copy-single gensym val)
+               val)
+              ;; FIXME: do demand-driven size accounting rather than
+              ;; these heuristics.
+              ((eq? ctx 'operator)
+               ;; A pure expression in the operator position.  Inline
+               ;; if it's a lambda that's small enough.
+               (if (and (lambda? val)
+                        (small-expression? val operator-size-limit))
+                   (begin
+                     (log 'copy-operator gensym val)
+                     val)
+                   (begin
+                     (log 'too-big-for-operator gensym val)
+                     (residualize-lexical op ctx val))))
+              (else
+               ;; A pure expression, processed for call or for value.
+               ;; Don't inline lambdas, because they will probably won't
+               ;; fold because we don't know the operator.
+               (if (and (small-expression? val value-size-limit)
+                        (not (tree-il-any lambda? val)))
+                   (begin
+                     (log 'copy-value gensym val)
+                     val)
+                   (begin
+                     (log 'too-big-or-has-lambda gensym val)
+                     (residualize-lexical op ctx val)))))))
+          (else
+           ;; Visit failed.  Either the operand isn't bound, as in
+           ;; lambda formal parameters, or the copy was aborted.
+           (log 'unbound-or-aborted gensym op)
+           (residualize-lexical op)))))
       (($ <lexical-set> src name gensym exp)
-       (if (zero? (lexical-refcount gensym))
-           (let ((exp (for-effect exp)))
-             (if (void? exp)
-                 exp
-                 (make-sequence src (list exp (make-void #f)))))
-           (begin
-             (record-residual-lexical-reference! gensym)
-             (make-lexical-set src name gensym (for-value exp)))))
+       (let ((op (lookup gensym)))
+         (if (zero? (var-refcount (operand-var op)))
+             (let ((exp (for-effect exp)))
+               (if (void? exp)
+                   exp
+                   (make-seq src exp (make-void #f))))
+             (begin
+               (record-operand-use op)
+               (make-lexical-set src name (operand-sym op) (for-value exp))))))
+      (($ <let> src
+          (names ... rest)
+          (gensyms ... rest-sym)
+          (vals ... ($ <primcall> _ 'list rest-args))
+          ($ <primcall> asrc (or 'apply '@apply)
+             (proc args ...
+                   ($ <lexical-ref> _
+                      (? (cut eq? <> rest))
+                      (? (lambda (sym)
+                           (and (eq? sym rest-sym)
+                                (= (lexical-refcount sym) 1))))))))
+       (let* ((tmps (make-list (length rest-args) 'tmp))
+              (tmp-syms (fresh-temporaries tmps)))
+         (for-tail
+          (make-let src
+                    (append names tmps)
+                    (append gensyms tmp-syms)
+                    (append vals rest-args)
+                    (make-call
+                     asrc
+                     proc
+                     (append args
+                             (map (cut make-lexical-ref #f <> <>)
+                                  tmps tmp-syms)))))))
       (($ <let> src names gensyms vals body)
-       (let* ((vals (map for-operand vals))
-              (body (loop body
-                      (fold vhash-consq env gensyms vals)
-                      counter
-                      ctx)))
+       (define (compute-alias exp)
+         ;; It's very common for macros to introduce something like:
+         ;;
+         ;;   ((lambda (x y) ...) x-exp y-exp)
+         ;;
+         ;; In that case you might end up trying to inline something like:
+         ;;
+         ;;   (let ((x x-exp) (y y-exp)) ...)
+         ;;
+         ;; But if x-exp is itself a lexical-ref that aliases some much
+         ;; larger expression, perhaps it will fail to inline due to
+         ;; size.  However we don't want to introduce a useless alias
+         ;; (in this case, x).  So if the RHS of a let expression is a
+         ;; lexical-ref, we record that expression.  If we end up having
+         ;; to residualize X, then instead we residualize X-EXP, as long
+         ;; as it isn't assigned.
+         ;;
+         (match exp
+           (($ <lexical-ref> _ _ sym)
+            (let ((op (lookup sym)))
+              (and (not (var-set? (operand-var op)))
+                   (or (operand-alias-value op)
+                       exp))))
+           (_ #f)))
+
+       (let* ((vars (map lookup-var gensyms))
+              (new (fresh-gensyms vars))
+              (ops (make-bound-operands vars new vals
+                                        (lambda (exp counter ctx)
+                                          (loop exp env counter ctx))
+                                        (map compute-alias vals)))
+              (env (fold extend-env env gensyms ops))
+              (body (loop body env counter ctx)))
          (cond
           ((const? body)
-           (for-tail (make-sequence src (append vals (list body)))))
+           (for-tail (list->seq src (append vals (list body)))))
           ((and (lexical-ref? body)
-                (memq (lexical-ref-gensym body) gensyms))
+                (memq (lexical-ref-gensym body) new))
            (let ((sym (lexical-ref-gensym body))
-                 (pairs (map cons gensyms vals)))
+                 (pairs (map cons new vals)))
              ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
              (for-tail
-              (make-sequence
+              (list->seq
                src
                (append (map cdr (alist-delete sym pairs eq?))
                        (list (assq-ref pairs sym)))))))
           (else
            ;; Only include bindings for which lexical references
            ;; have been residualized.
-           (prune-bindings names gensyms vals body for-effect
+           (prune-bindings ops #f body counter ctx
                            (lambda (names gensyms vals body)
                              (if (null? names) (error "what!" names))
                              (make-let src names gensyms vals body)))))))
       (($ <letrec> src in-order? names gensyms vals body)
-       ;; Things could be done more precisely when IN-ORDER? but
-       ;; it's OK not to do it---at worst we lost an optimization
-       ;; opportunity.
-       (let* ((vals (map for-operand vals))
-              (body (loop body
-                      (fold vhash-consq env gensyms vals)
-                      counter
-                      ctx)))
-         (if (and (const? body)
-                  (every constant-expression? vals))
-             body
-             (prune-bindings names gensyms vals body for-effect
+       ;; Note the difference from the `let' case: here we use letrec*
+       ;; so that the `visit' procedure for the new operands closes over
+       ;; an environment that includes the operands.  Also we don't try
+       ;; to elide aliases, because we can't sensibly reduce something
+       ;; like (letrec ((a b) (b a)) a).
+       (letrec* ((visit (lambda (exp counter ctx)
+                          (loop exp env* counter ctx)))
+                 (vars (map lookup-var gensyms))
+                 (new (fresh-gensyms vars))
+                 (ops (make-bound-operands vars new vals visit))
+                 (env* (fold extend-env env gensyms ops))
+                 (body* (visit body counter ctx)))
+         (if (and (const? body*) (every constant-expression? vals))
+             ;; We may have folded a loop completely, even though there
+             ;; might be cyclical references between the bound values.
+             ;; Handle this degenerate case specially.
+             body*
+             (prune-bindings ops in-order? body* counter ctx
                              (lambda (names gensyms vals body)
                                (make-letrec src in-order?
                                             names gensyms vals body))))))
       (($ <fix> src names gensyms vals body)
-       (let* ((vals (map for-operand vals))
-              (body (loop body
-                      (fold vhash-consq env gensyms vals)
-                      counter
-                      ctx)))
-         (if (const? body)
-             body
-             (prune-bindings names gensyms vals body for-effect
+       (letrec* ((visit (lambda (exp counter ctx)
+                          (loop exp env* counter ctx)))
+                 (vars (map lookup-var gensyms))
+                 (new (fresh-gensyms vars))
+                 (ops (make-bound-operands vars new vals visit))
+                 (env* (fold extend-env env gensyms ops))
+                 (body* (visit body counter ctx)))
+         (if (const? body*)
+             body*
+             (prune-bindings ops #f body* counter ctx
                              (lambda (names gensyms vals body)
                                (make-fix src names gensyms vals body))))))
       (($ <let-values> lv-src producer consumer)
        ;; Peval the producer, then try to inline the consumer into
        ;; the producer.  If that succeeds, peval again.  Otherwise
        ;; reconstruct the let-values, pevaling the consumer.
-       (let ((producer (for-value producer)))
+       (let ((producer (for-values producer)))
          (or (match consumer
-               (($ <lambda-case> src req #f #f #f () gensyms body #f)
-                (cond
-                 ((inline-values producer src req gensyms body)
-                  => for-tail)
-                 (else #f)))
+               (($ <lambda-case> src req opt rest #f inits gensyms body #f)
+                (let* ((nmin (length req))
+                       (nmax (and (not rest) (+ nmin (if opt (length opt) 0)))))
+                  (cond
+                   ((inline-values lv-src producer nmin nmax consumer)
+                    => for-tail)
+                   (else #f))))
                (_ #f))
              (make-let-values lv-src producer (for-tail consumer)))))
-      (($ <dynwind> src winder body unwinder)
-       (make-dynwind src (for-value winder) (for-tail body)
-                     (for-value unwinder)))
+      (($ <dynwind> src winder pre body post unwinder)
+       (make-dynwind src (for-value winder) (for-effect pre)
+                     (for-tail body)
+                     (for-effect post) (for-value unwinder)))
       (($ <dynlet> src fluids vals body)
        (make-dynlet src (map for-value fluids) (map for-value vals)
                     (for-tail body)))
@@ -814,9 +1008,7 @@ top-level bindings from ENV and return the resulting expression."
       (($ <dynset> src fluid exp)
        (make-dynset src (for-value fluid) (for-value exp)))
       (($ <toplevel-ref> src (? effect-free-primitive? name))
-       (if (local-toplevel? name)
-           exp
-           (resolve-primitives! exp cenv)))
+       exp)
       (($ <toplevel-ref>)
        ;; todo: open private local bindings.
        exp)
@@ -843,136 +1035,336 @@ top-level bindings from ENV and return the resulting expression."
          ((test) (make-const #f #t))
          (else exp)))
       (($ <conditional> src condition subsequent alternate)
-       (let ((condition (for-test condition)))
-         (if (const? condition)
-             (if (const-exp condition)
-                 (for-tail subsequent)
-                 (for-tail alternate))
-             (make-conditional src condition
-                               (for-tail subsequent)
-                               (for-tail alternate)))))
-      (($ <application> src
-          ($ <primitive-ref> _ '@call-with-values)
+       (define (call-with-failure-thunk exp proc)
+         (match exp
+           (($ <call> _ _ ()) (proc exp))
+           (($ <primcall> _ _ ()) (proc exp))
+           (($ <const>) (proc exp))
+           (($ <void>) (proc exp))
+           (($ <lexical-ref>) (proc exp))
+           (_
+            (let ((t (gensym "failure-")))
+              (record-new-temporary! 'failure t 2)
+              (make-let
+               src (list 'failure) (list t)
+               (list
+                (make-lambda
+                 #f '()
+                 (make-lambda-case #f '() #f #f #f '() '() exp #f)))
+               (proc (make-call #f (make-lexical-ref #f 'failure t)
+                                '())))))))
+       (define (simplify-conditional c)
+         (match c
+           ;; Swap the arms of (if (not FOO) A B), to simplify.
+           (($ <conditional> src ($ <primcall> _ 'not (pred))
+               subsequent alternate)
+            (simplify-conditional
+             (make-conditional src pred alternate subsequent)))
+           ;; Special cases for common tests in the predicates of chains
+           ;; of if expressions.
+           (($ <conditional> src
+               ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
+               inner-subsequent
+               alternate)
+            (let lp ((alternate alternate))
+              (match alternate
+                ;; Lift a common repeated test out of a chain of if
+                ;; expressions.
+                (($ <conditional> _ (? (cut tree-il=? outer-test <>))
+                    other-subsequent alternate)
+                 (make-conditional
+                  src outer-test
+                  (simplify-conditional
+                   (make-conditional src* inner-test inner-subsequent
+                                     other-subsequent))
+                  alternate))
+                ;; Likewise, but punching through any surrounding
+                ;; failure continuations.
+                (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
+                 (make-let
+                  let-src (list name) (list sym) (list thunk)
+                  (lp body)))
+                ;; Otherwise, rotate AND tests to expose a simple
+                ;; condition in the front.  Although this may result in
+                ;; lexically binding failure thunks, the thunks will be
+                ;; compiled to labels allocation, so there's no actual
+                ;; code growth.
+                (_
+                 (call-with-failure-thunk
+                  alternate
+                  (lambda (failure)
+                    (make-conditional
+                     src outer-test
+                     (simplify-conditional
+                      (make-conditional src* inner-test inner-subsequent failure))
+                     failure)))))))
+           (_ c)))
+       (match (for-test condition)
+         (($ <const> _ val)
+          (if val
+              (for-tail subsequent)
+              (for-tail alternate)))
+         (c
+          (simplify-conditional
+           (make-conditional src c (for-tail subsequent)
+                             (for-tail alternate))))))
+      (($ <primcall> src '@call-with-values
           (producer
            ($ <lambda> _ _
               (and consumer
                    ;; No optional or kwargs.
                    ($ <lambda-case>
                       _ req #f rest #f () gensyms body #f)))))
-       (for-tail (make-let-values src (make-application src producer '())
+       (for-tail (make-let-values src (make-call src producer '())
                                   consumer)))
+      (($ <primcall> src 'dynamic-wind (w thunk u))
+       (for-tail
+        (cond
+         ((not (constant-expression? w))
+          (cond
+           ((not (constant-expression? u))
+            (let ((w-sym (gensym "w ")) (u-sym (gensym "u ")))
+              (record-new-temporary! 'w w-sym 2)
+              (record-new-temporary! 'u u-sym 2)
+              (make-let src '(w u) (list w-sym u-sym) (list w u)
+                        (make-dynwind
+                         src
+                         (make-lexical-ref #f 'w w-sym)
+                         (make-call #f (make-lexical-ref #f 'w w-sym) '())
+                         (make-call #f thunk '())
+                         (make-call #f (make-lexical-ref #f 'u u-sym) '())
+                         (make-lexical-ref #f 'u u-sym)))))
+           (else
+            (let ((w-sym (gensym "w ")))
+              (record-new-temporary! 'w w-sym 2)
+              (make-let src '(w) (list w-sym) (list w)
+                        (make-dynwind
+                         src
+                         (make-lexical-ref #f 'w w-sym)
+                         (make-call #f (make-lexical-ref #f 'w w-sym) '())
+                         (make-call #f thunk '())
+                         (make-call #f u '())
+                         u))))))
+         ((not (constant-expression? u))
+          (let ((u-sym (gensym "u ")))
+            (record-new-temporary! 'u u-sym 2)
+            (make-let src '(u) (list u-sym) (list u)
+                      (make-dynwind
+                       src
+                       w
+                       (make-call #f w '())
+                       (make-call #f thunk '())
+                       (make-call #f (make-lexical-ref #f 'u u-sym) '())
+                       (make-lexical-ref #f 'u u-sym)))))
+         (else
+          (make-dynwind src w (make-call #f w '()) (make-call #f thunk '())
+                        (make-call #f u '()) u)))))
+
+      (($ <primcall> src 'values exps)
+       (cond
+        ((null? exps)
+         (if (eq? ctx 'effect)
+             (make-void #f)
+             exp))
+        (else
+         (let ((vals (map for-value exps)))
+           (if (and (case ctx
+                      ((value test effect) #t)
+                      (else (null? (cdr vals))))
+                    (every singly-valued-expression? vals))
+               (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
+               (make-primcall src 'values vals))))))
+
+      (($ <primcall> src (or 'apply '@apply) (proc args ... tail))
+       (let lp ((tail* (find-definition tail 1)) (speculative? #t))
+         (define (copyable? x)
+           ;; Inlining a result from find-definition effectively copies it,
+           ;; relying on the let-pruning to remove its original binding.  We
+           ;; shouldn't copy non-constant expressions.
+           (or (not speculative?) (constant-expression? x)))
+         (match tail*
+           (($ <const> _ (args* ...))
+            (let ((args* (map (cut make-const #f <>) args*)))
+              (for-tail (make-call src proc (append args args*)))))
+           (($ <primcall> _ 'cons
+               ((and head (? copyable?)) (and tail (? copyable?))))
+            (for-tail (make-primcall src '@apply
+                                     (cons proc
+                                           (append args (list head tail))))))
+           (($ <primcall> _ 'list
+               (and args* ((? copyable?) ...)))
+            (for-tail (make-call src proc (append args args*))))
+           (tail*
+            (if speculative?
+                (lp (for-value tail) #f)
+                (let ((args (append (map for-value args) (list tail*))))
+                  (make-primcall src '@apply
+                                 (cons (for-value proc) args))))))))
+
+      (($ <primcall> src (? constructor-primitive? name) args)
+       (cond
+        ((and (memq ctx '(effect test))
+              (match (cons name args)
+                ((or ('cons _ _)
+                     ('list . _)
+                     ('vector . _)
+                     ('make-prompt-tag)
+                     ('make-prompt-tag ($ <const> _ (? string?))))
+                 #t)
+                (_ #f)))
+         ;; Some expressions can be folded without visiting the
+         ;; arguments for value.
+         (let ((res (if (eq? ctx 'effect)
+                        (make-void #f)
+                        (make-const #f #t))))
+           (for-tail (list->seq src (append args (list res))))))
+        (else
+         (match (cons name (map for-value args))
+           (('cons x ($ <const> _ (? (cut eq? <> '()))))
+            (make-primcall src 'list (list x)))
+           (('cons x ($ <primcall> _ 'list elts))
+            (make-primcall src 'list (cons x elts)))
+           ((name . args)
+            (make-primcall src name args))))))
+
+      (($ <primcall> src (? accessor-primitive? name) args)
+       (match (cons name (map for-value args))
+         ;; FIXME: these for-tail recursions could take place outside
+         ;; an effort counter.
+         (('car ($ <primcall> src 'cons (head tail)))
+          (for-tail (make-seq src tail head)))
+         (('cdr ($ <primcall> src 'cons (head tail)))
+          (for-tail (make-seq src head tail)))
+         (('car ($ <primcall> src 'list (head . tail)))
+          (for-tail (list->seq src (append tail (list head)))))
+         (('cdr ($ <primcall> src 'list (head . tail)))
+          (for-tail (make-seq src head (make-primcall #f 'list tail))))
+                  
+         (('car ($ <const> src (head . tail)))
+          (for-tail (make-const src head)))
+         (('cdr ($ <const> src (head . tail)))
+          (for-tail (make-const src tail)))
+         (((or 'memq 'memv) k ($ <const> _ (elts ...)))
+          ;; FIXME: factor 
+          (case ctx
+            ((effect)
+             (for-tail
+              (make-seq src k (make-void #f))))
+            ((test)
+             (cond
+              ((const? k)
+               ;; A shortcut.  The `else' case would handle it, but
+               ;; this way is faster.
+               (let ((member (case name ((memq) memq) ((memv) memv))))
+                 (make-const #f (and (member (const-exp k) elts) #t))))
+              ((null? elts)
+               (for-tail
+                (make-seq src k (make-const #f #f))))
+              (else
+               (let ((t (gensym "t "))
+                     (eq (if (eq? name 'memq) 'eq? 'eqv?)))
+                 (record-new-temporary! 't t (length elts))
+                 (for-tail
+                  (make-let
+                   src (list 't) (list t) (list k)
+                   (let lp ((elts elts))
+                     (define test
+                       (make-primcall #f eq
+                                      (list (make-lexical-ref #f 't t)
+                                            (make-const #f (car elts)))))
+                     (if (null? (cdr elts))
+                         test
+                         (make-conditional src test
+                                           (make-const #f #t)
+                                           (lp (cdr elts)))))))))))
+            (else
+             (cond
+              ((const? k)
+               (let ((member (case name ((memq) memq) ((memv) memv))))
+                 (make-const #f (member (const-exp k) elts))))
+              ((null? elts)
+               (for-tail (make-seq src k (make-const #f #f))))
+              (else
+               (make-primcall src name (list k (make-const #f elts))))))))
+         ((name . args)
+          (fold-constants src name args ctx))))
+
+      (($ <primcall> src (? equality-primitive? name) (a b))
+       (let ((val-a (for-value a))
+             (val-b (for-value b)))
+         (log 'equality-primitive name val-a val-b)
+         (cond ((and (lexical-ref? val-a) (lexical-ref? val-b)
+                     (eq? (lexical-ref-gensym val-a)
+                          (lexical-ref-gensym val-b)))
+                (for-tail (make-const #f #t)))
+               (else
+                (fold-constants src name (list val-a val-b) ctx)))))
+      
+      (($ <primcall> src (? effect-free-primitive? name) args)
+       (fold-constants src name (map for-value args) ctx))
 
-      (($ <application> src orig-proc orig-args)
+      (($ <primcall> src name args)
+       (make-primcall src name (map for-value args)))
+
+      (($ <call> src orig-proc orig-args)
        ;; todo: augment the global env with specialized functions
-       (let ((proc (loop orig-proc env counter 'operator)))
+       (let revisit-proc ((proc (visit orig-proc 'operator)))
          (match proc
-           (($ <primitive-ref> _ (? constructor-primitive? name))
-            (case ctx
-              ((effect test)
-               (let ((res (if (eq? ctx 'effect)
-                              (make-void #f)
-                              (make-const #f #t))))
-                 (match (for-value exp)
-                   (($ <application> _ ($ <primitive-ref> _ 'cons) (x xs))
-                    (for-tail
-                     (make-sequence src (list x xs res))))
-                   (($ <application> _ ($ <primitive-ref> _ 'list) elts)
-                    (for-tail
-                     (make-sequence src (append elts (list res)))))
-                   (($ <application> _ ($ <primitive-ref> _ 'vector) elts)
-                    (for-tail
-                     (make-sequence src (append elts (list res)))))
-                   (($ <application> _ ($ <primitive-ref> _ 'make-prompt-tag) ())
-                    res)
-                   (($ <application> _ ($ <primitive-ref> _ 'make-prompt-tag)
-                       (($ <const> _ (? string?))))
-                    res)
-                   (exp exp))))
-              (else
-               (match (cons name (map for-value orig-args))
-                 (('cons head tail)
-                  (match tail
-                    (($ <const> src ())
-                     (make-application src (make-primitive-ref #f 'list)
-                                       (list head)))
-                    (($ <application> src ($ <primitive-ref> _ 'list) elts)
-                     (make-application src (make-primitive-ref #f 'list)
-                                       (cons head elts)))
-                    (_ (make-application src proc
-                                         (list head tail)))))
-
-                 ;; FIXME: these for-tail recursions could take
-                 ;; place outside an effort counter.
-                 (('car ($ <application> src ($ <primitive-ref> _ 'cons) (head tail)))
-                  (for-tail (make-sequence src (list tail head))))
-                 (('cdr ($ <application> src ($ <primitive-ref> _ 'cons) (head tail)))
-                  (for-tail (make-sequence src (list head tail))))
-                 (('car ($ <application> src ($ <primitive-ref> _ 'list) (head . tail)))
-                  (for-tail (make-sequence src (append tail (list head)))))
-                 (('cdr ($ <application> src ($ <primitive-ref> _ 'list) (head . tail)))
-                  (for-tail (make-sequence
-                             src
-                             (list head
-                                   (make-application
-                                    src (make-primitive-ref #f 'list) tail)))))
-                  
-                 (('car ($ <const> src (head . tail)))
-                  (for-tail (make-const src head)))
-                 (('cdr ($ <const> src (head . tail)))
-                  (for-tail (make-const src tail)))
-
-                 ((_ . args)
-                  (make-application src proc args))))))
-           (($ <primitive-ref> _ (? effect-free-primitive? name))
-            (let ((args (map for-value orig-args)))
-              (if (every const? args)   ; only simple constants
-                  (let-values (((success? values)
-                                (apply-primitive name
-                                                 (map const-exp args))))
-                    (log 'fold success? values exp)
-                    (if success?
-                        (case ctx
-                          ((effect) (make-void #f))
-                          ((test)
-                           ;; Values truncation: only take the first
-                           ;; value.
-                           (if (pair? values)
-                               (make-const #f (car values))
-                               (make-values src '())))
-                          (else
-                           (make-values src (map (cut make-const src <>)
-                                                 values))))
-                        (make-application src proc args)))
-                  (cond
-                   ((and (eq? ctx 'effect) (types-check? name args))
-                    (make-void #f))
-                   (else
-                    (make-application src proc args))))))
+           (($ <primitive-ref> _ name)
+            (for-tail (make-primcall src name orig-args)))
            (($ <lambda> _ _
-               ($ <lambda-case> _ req opt #f #f inits gensyms body #f))
-            ;; Simple case: no rest, no keyword arguments.
+               ($ <lambda-case> _ req opt rest #f inits gensyms body #f))
+            ;; Simple case: no keyword arguments.
             ;; todo: handle the more complex cases
             (let* ((nargs (length orig-args))
                    (nreq (length req))
                    (nopt (if opt (length opt) 0))
                    (key (source-expression proc)))
+              (define (inlined-call)
+                (make-let src
+                          (append req
+                                  (or opt '())
+                                  (if rest (list rest) '()))
+                          gensyms
+                          (if (> nargs (+ nreq nopt))
+                              (append (list-head orig-args (+ nreq nopt))
+                                      (list
+                                       (make-primcall
+                                        #f 'list
+                                        (drop orig-args (+ nreq nopt)))))
+                              (append orig-args
+                                      (drop inits (- nargs nreq))
+                                      (if rest
+                                          (list (make-const #f '()))
+                                          '())))
+                          body))
+
               (cond
-               ((or (< nargs nreq) (> nargs (+ nreq nopt)))
+               ((or (< nargs nreq) (and (not rest) (> nargs (+ nreq nopt))))
                 ;; An error, or effecting arguments.
-                (make-application src (for-value orig-proc)
-                                  (map for-value orig-args)))
+                (make-call src (for-call orig-proc) (map for-value orig-args)))
                ((or (and=> (find-counter key counter) counter-recursive?)
                     (lambda? orig-proc))
                 ;; A recursive call, or a lambda in the operator
                 ;; position of the source expression.  Process again in
                 ;; tail context.
+                ;;
+                ;; In the recursive case, mark intervening counters as
+                ;; recursive, so we can handle a toplevel counter that
+                ;; recurses mutually with some other procedure.
+                ;; Otherwise, the next time we see the other procedure,
+                ;; the effort limit would be clamped to 100.
+                ;;
+                (let ((found (find-counter key counter)))
+                  (if (and found (counter-recursive? found))
+                      (let lp ((counter counter))
+                        (if (not (eq? counter found))
+                            (begin
+                              (set-counter-recursive?! counter #t)
+                              (lp (counter-prev counter)))))))
+
                 (log 'inline-recurse key)
-                (loop (make-let src (append req (or opt '()))
-                                gensyms
-                                (append orig-args
-                                        (drop inits (- nargs nreq)))
-                                body)
-                  env counter ctx))
+                (loop (inlined-call) env counter ctx))
                (else
                 ;; An integration at the top-level, the first
                 ;; recursion of a recursive procedure, or a nested
@@ -982,9 +1374,8 @@ top-level bindings from ENV and return the resulting expression."
                 (let/ec k
                   (define (abort)
                     (log 'inline-abort exp)
-                    (k (make-application src
-                                         (for-value orig-proc)
-                                         (map for-value orig-args))))
+                    (k (make-call src (for-call orig-proc)
+                                  (map for-value orig-args))))
                   (define new-counter
                     (cond
                      ;; These first two cases will transfer effort
@@ -1004,12 +1395,7 @@ top-level bindings from ENV and return the resulting expression."
                       (make-top-counter effort-limit operand-size-limit
                                         abort key))))
                   (define result
-                    (loop (make-let src (append req (or opt '()))
-                                    gensyms
-                                    (append orig-args
-                                            (drop inits (- nargs nreq)))
-                                    body)
-                      env new-counter ctx))
+                    (loop (inlined-call) env new-counter ctx))
                       
                   (if counter
                       ;; The nested inlining attempt succeeded.
@@ -1019,59 +1405,126 @@ top-level bindings from ENV and return the resulting expression."
 
                   (log 'inline-end result exp)
                   result)))))
+           (($ <let> _ _ _ vals _)
+            ;; Attempt to inline `let' in the operator position.
+            ;;
+            ;; We have to re-visit the proc in value mode, since the
+            ;; `let' bindings might have been introduced or renamed,
+            ;; whereas the lambda (if any) in operator position has not
+            ;; been renamed.
+            (if (or (and-map constant-expression? vals)
+                    (and-map constant-expression? orig-args))
+                ;; The arguments and the let-bound values commute.
+                (match (for-value orig-proc)
+                  (($ <let> lsrc names syms vals body)
+                   (log 'inline-let orig-proc)
+                   (for-tail
+                    (make-let lsrc names syms vals
+                              (make-call src body orig-args))))
+                  ;; It's possible for a `let' to go away after the
+                  ;; visit due to the fact that visiting a procedure in
+                  ;; value context will prune unused bindings, whereas
+                  ;; visiting in operator mode can't because it doesn't
+                  ;; traverse through lambdas.  In that case re-visit
+                  ;; the procedure.
+                  (proc (revisit-proc proc)))
+                (make-call src (for-call orig-proc)
+                           (map for-value orig-args))))
            (_
-            (make-application src proc
-                              (map for-value orig-args))))))
+            (make-call src (for-call orig-proc) (map for-value orig-args))))))
       (($ <lambda> src meta body)
        (case ctx
          ((effect) (make-void #f))
          ((test) (make-const #f #t))
          ((operator) exp)
-         (else
-          (make-lambda src meta (for-value body)))))
+         (else (record-source-expression!
+                exp
+                (make-lambda src meta (and body (for-values body)))))))
       (($ <lambda-case> src req opt rest kw inits gensyms body alt)
-       (make-lambda-case src req opt rest kw
-                         (map for-value inits)
-                         gensyms
-                         (for-tail body)
-                         (and alt (for-tail alt))))
-      (($ <sequence> src exps)
-       (let lp ((exps exps) (effects '()))
-         (match exps
-           ((last)
-            (if (null? effects)
-                (for-tail last)
-                (make-sequence
-                 src
-                 (reverse (cons (for-tail last) effects)))))
-           ((head . rest)
-            (let ((head (for-effect head)))
-              (cond
-               ((sequence? head)
-                (lp (append (sequence-exps head) rest) effects))
-               ((void? head)
-                (lp rest effects))
-               (else
-                (lp rest (cons head effects)))))))))
+       (define (lift-applied-lambda body gensyms)
+         (and (not opt) rest (not kw)
+              (match body
+                (($ <primcall> _ '@apply
+                    (($ <lambda> _ _ (and lcase ($ <lambda-case>)))
+                     ($ <lexical-ref> _ _ sym)
+                     ...))
+                 (and (equal? sym gensyms)
+                      (not (lambda-case-alternate lcase))
+                      lcase))
+                (_ #f))))
+       (let* ((vars (map lookup-var gensyms))
+              (new (fresh-gensyms vars))
+              (env (fold extend-env env gensyms
+                         (make-unbound-operands vars new)))
+              (new-sym (lambda (old)
+                         (operand-sym (cdr (vhash-assq old env)))))
+              (body (loop body env counter ctx)))
+         (or
+          ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
+          (lift-applied-lambda body new)
+          (make-lambda-case src req opt rest
+                            (match kw
+                              ((aok? (kw name old) ...)
+                               (cons aok? (map list kw name (map new-sym old))))
+                              (_ #f))
+                            (map (cut loop <> env counter 'value) inits)
+                            new
+                            body
+                            (and alt (for-tail alt))))))
+      (($ <seq> src head tail)
+       (let ((head (for-effect head))
+             (tail (for-tail tail)))
+         (if (void? head)
+             tail
+             (make-seq src
+                       (if (and (seq? head)
+                                (void? (seq-tail head)))
+                           (seq-head head)
+                           head)
+                       tail))))
       (($ <prompt> src tag body handler)
-       (define (singly-used-definition x)
+       (define (make-prompt-tag? x)
+         (match x
+           (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
+            #t)
+           (_ #f)))
+
+       (let ((tag (for-value tag))
+             (body (for-tail body)))
          (cond
-          ((and (lexical-ref? x)
-                ;; Only fetch definitions with single uses.
-                (= (lexical-refcount (lexical-ref-gensym x)) 1)
-                (lookup (lexical-ref-gensym x)))
-           => singly-used-definition)
-          (else x)))
-       (match (singly-used-definition tag)
-         (($ <application> _ ($ <primitive-ref> _ 'make-prompt-tag)
-             (or () ((? constant-expression?))))
-          ;; There is no way that an <abort> could know the tag
-          ;; for this <prompt>, so we can elide the <prompt>
-          ;; entirely.
-          (for-tail body))
-         (_
-          (make-prompt src (for-value tag) (for-tail body)
-                       (for-value handler)))))
+          ((find-definition tag 1)
+           (lambda (val op)
+             (make-prompt-tag? val))
+           => (lambda (val op)
+                ;; There is no way that an <abort> could know the tag
+                ;; for this <prompt>, so we can elide the <prompt>
+                ;; entirely.
+                (unrecord-operand-uses op 1)
+                body))
+          ((find-definition tag 2)
+           (lambda (val op)
+             (and (make-prompt-tag? val)
+                  (abort? body)
+                  (tree-il=? (abort-tag body) tag)))
+           => (lambda (val op)
+                ;; (let ((t (make-prompt-tag)))
+                ;;   (call-with-prompt t
+                ;;     (lambda () (abort-to-prompt t val ...))
+                ;;     (lambda (k arg ...) e ...)))
+                ;; => (let-values (((k arg ...) (values values val ...)))
+                ;;      e ...)
+                (unrecord-operand-uses op 2)
+                (for-tail
+                 (make-let-values
+                  src
+                  (make-primcall #f 'apply
+                                 `(,(make-primitive-ref #f 'values)
+                                   ,(make-primitive-ref #f 'values)
+                                   ,@(abort-args body)
+                                   ,(abort-tail body)))
+                  (for-value handler)))))
+          (else
+           (make-prompt src tag body (for-value handler))))))
       (($ <abort> src tag args tail)
        (make-abort src (for-value tag) (map for-value args)
                    (for-value tail))))))