1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-10 05:50:26 +02:00

Minor CSE run-time optimization

* module/language/cps/cse.scm (compute-equivalent-subexpressions): Minor
  optimization to reduce the size of equivalent expression keys, and to
  avoid some work if an expression has no key.
This commit is contained in:
Andy Wingo 2017-11-30 10:41:45 +01:00
parent bfe70b129c
commit 1575c863fe

View file

@ -250,9 +250,9 @@ false. It could be that both true and false proofs are available."
(($ $call proc args) #f) (($ $call proc args) #f)
(($ $callk k proc args) #f) (($ $callk k proc args) #f)
(($ $primcall name param args) (($ $primcall name param args)
(cons* 'primcall name param (subst-vars var-substs args))) (cons* name param (subst-vars var-substs args)))
(($ $branch _ ($ $primcall name param args)) (($ $branch _ ($ $primcall name param args))
(cons* 'primcall name param (subst-vars var-substs args))) (cons* name param (subst-vars var-substs args)))
(($ $values args) #f) (($ $values args) #f)
(($ $prompt escape? tag handler) #f))) (($ $prompt escape? tag handler) #f)))
@ -271,16 +271,16 @@ false. It could be that both true and false proofs are available."
((def <- op arg ...) (aux <- op* arg* ...) ...) ((def <- op arg ...) (aux <- op* arg* ...) ...)
. clauses) . clauses)
(match exp-key (match exp-key
(('primcall 'op arg ...) (('op arg ...)
(match defs (match defs
((def) (add-def! (list 'primcall 'op* arg* ...) aux) ...))) ((def) (add-def! (list 'op* arg* ...) aux) ...)))
(_ (add-definitions . clauses)))) (_ (add-definitions . clauses))))
((add-definitions ((add-definitions
((op arg ...) (aux <- op* arg* ...) ...) ((op arg ...) (aux <- op* arg* ...) ...)
. clauses) . clauses)
(match exp-key (match exp-key
(('primcall 'op arg ...) (('op arg ...)
(add-def! (list 'primcall 'op* arg* ...) aux) ...) (add-def! (list 'op* arg* ...) aux) ...)
(_ (add-definitions . clauses)))))) (_ (add-definitions . clauses))))))
(add-definitions (add-definitions
((b <- box #f o) (o <- box-ref #f b)) ((b <- box #f o) (o <- box-ref #f b))
@ -319,55 +319,56 @@ false. It could be that both true and false proofs are available."
(define (visit-label label equiv-labels var-substs) (define (visit-label label equiv-labels var-substs)
(match (intmap-ref conts label) (match (intmap-ref conts label)
(($ $kargs names vars ($ $continue k src exp)) (($ $kargs names vars ($ $continue k src exp))
(let* ((exp-key (compute-exp-key var-substs exp)) (match (compute-exp-key var-substs exp)
(equiv (hash-ref equiv-set exp-key '())) (#f (values equiv-labels var-substs))
(fx (intmap-ref effects label)) (exp-key
(avail (intmap-ref avail label))) (let* ((equiv (hash-ref equiv-set exp-key '()))
(define (finish equiv-labels var-substs) (fx (intmap-ref effects label))
;; If this expression defines auxiliary definitions, (avail (intmap-ref avail label)))
;; as `cons' does for the results of `car' and `cdr', (define (finish equiv-labels var-substs)
;; define those. Do so after finding equivalent ;; If this expression defines auxiliary definitions,
;; expressions, so that we can take advantage of ;; as `cons' does for the results of `car' and `cdr',
;; subst'd output vars. ;; define those. Do so after finding equivalent
(add-auxiliary-definitions! label var-substs exp-key) ;; expressions, so that we can take advantage of
(values equiv-labels var-substs)) ;; subst'd output vars.
(let lp ((candidates equiv)) (add-auxiliary-definitions! label var-substs exp-key)
(match candidates (values equiv-labels var-substs))
(() (let lp ((candidates equiv))
;; No matching expressions. Add our expression (match candidates
;; to the equivalence set, if appropriate. Note (()
;; that expressions that allocate a fresh object ;; No matching expressions. Add our expression
;; or change the current fluid environment can't ;; to the equivalence set, if appropriate. Note
;; be eliminated by CSE (though DCE might do it ;; that expressions that allocate a fresh object
;; if the value proves to be unused, in the ;; or change the current fluid environment can't
;; allocation case). ;; be eliminated by CSE (though DCE might do it
(when (and exp-key ;; if the value proves to be unused, in the
(not (causes-effect? fx &allocation)) ;; allocation case).
(not (effect-clobbers? fx (&read-object &fluid)))) (when (and (not (causes-effect? fx &allocation))
(let ((defs (and (intset-ref singly-referenced k) (not (effect-clobbers? fx (&read-object &fluid))))
(intmap-ref defs label)))) (let ((defs (and (intset-ref singly-referenced k)
(when defs (intmap-ref defs label))))
(hash-set! equiv-set exp-key (when defs
(acons label defs equiv))))) (hash-set! equiv-set exp-key
(finish equiv-labels var-substs)) (acons label defs equiv)))))
(((and head (candidate . vars)) . candidates) (finish equiv-labels var-substs))
(cond (((and head (candidate . vars)) . candidates)
((not (intset-ref avail candidate)) (cond
;; This expression isn't available here; try ((not (intset-ref avail candidate))
;; the next one. ;; This expression isn't available here; try
(lp candidates)) ;; the next one.
(else (lp candidates))
;; Yay, a match. Mark expression as equivalent. If (else
;; we provide the definitions for the successor, mark ;; Yay, a match. Mark expression as equivalent. If
;; the vars for substitution. ;; we provide the definitions for the successor, mark
(finish (intmap-add equiv-labels label head) ;; the vars for substitution.
(let ((defs (and (intset-ref singly-referenced k) (finish (intmap-add equiv-labels label head)
(intmap-ref defs label)))) (let ((defs (and (intset-ref singly-referenced k)
(if defs (intmap-ref defs label))))
(fold (lambda (def var var-substs) (if defs
(intmap-add var-substs def var)) (fold (lambda (def var var-substs)
var-substs defs vars) (intmap-add var-substs def var))
var-substs)))))))))) var-substs defs vars)
var-substs))))))))))))
(_ (values equiv-labels var-substs)))) (_ (values equiv-labels var-substs))))
;; Traverse the labels in fun in reverse post-order, which will ;; Traverse the labels in fun in reverse post-order, which will