mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-09 21:40:33 +02:00
Simplification pass prunes all unreachable continuations
* module/language/cps/simplify.scm (prune-continuations): Prune continuations as a post-pass with a fresh DFG. Using a pre-eta-conversion DFG as we were doing before missed some cases.
This commit is contained in:
parent
546efe2514
commit
4b3d7a2b7c
1 changed files with 44 additions and 42 deletions
|
@ -40,6 +40,46 @@
|
|||
;; aren't used), making it useful for this pass to include its own
|
||||
;; little pruner.
|
||||
|
||||
(define* (prune-continuations fun #:optional (dfg (compute-dfg fun)))
|
||||
(let ((cfa (analyze-control-flow fun dfg)))
|
||||
(define (must-visit-cont cont)
|
||||
(or (visit-cont cont)
|
||||
(error "cont must be reachable" cont)))
|
||||
(define (visit-cont cont)
|
||||
(match cont
|
||||
(($ $cont sym cont)
|
||||
(and (cfa-k-idx cfa sym #:default (lambda (k) #f))
|
||||
(rewrite-cps-cont cont
|
||||
(($ $kargs names syms body)
|
||||
(sym ($kargs names syms ,(visit-term body))))
|
||||
(($ $kentry self tail clauses)
|
||||
(sym ($kentry self ,tail ,(visit-conts clauses))))
|
||||
(($ $kclause arity body)
|
||||
(sym ($kclause ,arity ,(must-visit-cont body))))
|
||||
((or ($ $kreceive) ($ $kif))
|
||||
(sym ,cont)))))))
|
||||
(define (visit-conts conts)
|
||||
(filter-map visit-cont conts))
|
||||
(define (visit-term term)
|
||||
(match term
|
||||
(($ $letk conts body)
|
||||
(let ((body (visit-term body)))
|
||||
(match (visit-conts conts)
|
||||
(() body)
|
||||
(conts (build-cps-term ($letk ,conts ,body))))))
|
||||
(($ $letrec names syms funs body)
|
||||
(build-cps-term
|
||||
($letrec names syms (map (cut prune-continuations <> dfg) funs)
|
||||
,(visit-term body))))
|
||||
(($ $continue k src (and fun ($ $fun)))
|
||||
(build-cps-term
|
||||
($continue k src ,(prune-continuations fun dfg))))
|
||||
(($ $continue k src exp)
|
||||
term)))
|
||||
(rewrite-cps-exp fun
|
||||
(($ $fun src meta free body)
|
||||
($fun src meta free ,(must-visit-cont body))))))
|
||||
|
||||
(define (compute-eta-reductions fun)
|
||||
(let ((table (make-hash-table)))
|
||||
(define (visit-cont cont)
|
||||
|
@ -73,42 +113,6 @@
|
|||
(visit-fun fun)
|
||||
table))
|
||||
|
||||
(define (locally-prune-continuations fun dfg)
|
||||
(let ((cfa (analyze-control-flow fun dfg)))
|
||||
(define (must-visit-cont cont)
|
||||
(or (visit-cont cont)
|
||||
(error "cont must be reachable" cont)))
|
||||
(define (visit-cont cont)
|
||||
(match cont
|
||||
(($ $cont sym cont)
|
||||
(and (cfa-k-idx cfa sym #:default (lambda (k) #f))
|
||||
(rewrite-cps-cont cont
|
||||
(($ $kargs names syms body)
|
||||
(sym ($kargs names syms ,(visit-term body))))
|
||||
(($ $kentry self tail clauses)
|
||||
(sym ($kentry self ,tail ,(visit-conts clauses))))
|
||||
(($ $kclause arity body)
|
||||
(sym ($kclause ,arity ,(must-visit-cont body))))
|
||||
((or ($ $kreceive) ($ $kif))
|
||||
(sym ,cont)))))))
|
||||
(define (visit-conts conts)
|
||||
(filter-map visit-cont conts))
|
||||
(define (visit-term term)
|
||||
(match term
|
||||
(($ $letk conts body)
|
||||
(let ((body (visit-term body)))
|
||||
(match (visit-conts conts)
|
||||
(() body)
|
||||
(conts (build-cps-term ($letk ,conts ,body))))))
|
||||
(($ $letrec names syms funs body)
|
||||
(build-cps-term
|
||||
($letrec names syms funs ,(visit-term body))))
|
||||
(($ $continue k src exp)
|
||||
term)))
|
||||
(rewrite-cps-exp fun
|
||||
(($ $fun src meta free body)
|
||||
($fun src meta free ,(must-visit-cont body))))))
|
||||
|
||||
(define (eta-reduce fun)
|
||||
(let ((table (compute-eta-reductions fun))
|
||||
(dfg (compute-dfg fun)))
|
||||
|
@ -154,11 +158,9 @@
|
|||
(($ $continue k src exp)
|
||||
($continue (reduce k scope) src ,exp))))
|
||||
(define (visit-fun fun)
|
||||
(locally-prune-continuations
|
||||
(rewrite-cps-exp fun
|
||||
(($ $fun src meta free body)
|
||||
($fun src meta free ,(visit-cont body #f))))
|
||||
dfg))
|
||||
(rewrite-cps-exp fun
|
||||
(($ $fun src meta free body)
|
||||
($fun src meta free ,(visit-cont body #f)))))
|
||||
(visit-fun fun)))
|
||||
|
||||
(define (compute-beta-reductions fun)
|
||||
|
@ -273,4 +275,4 @@
|
|||
(visit-fun fun)))
|
||||
|
||||
(define (simplify fun)
|
||||
(eta-reduce (beta-reduce fun)))
|
||||
(prune-continuations (eta-reduce (beta-reduce fun))))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue