1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-30 06:50:31 +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 committed by Andy Wingo
parent 8e2314c46d
commit d4883307ca

View file

@ -1,6 +1,6 @@
;;; Continuation-passing style (CPS) intermediate language (IL) ;;; Continuation-passing style (CPS) intermediate language (IL)
;; Copyright (C) 2013, 2014, 2015 Free Software Foundation, Inc. ;; Copyright (C) 2013, 2014, 2015, 2017 Free Software Foundation, Inc.
;;;; This library is free software; you can redistribute it and/or ;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public ;;;; modify it under the terms of the GNU Lesser General Public
@ -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 args) (($ $primcall name args)
(cons* 'primcall name (subst-vars var-substs args))) (cons* name (subst-vars var-substs args)))
(($ $branch _ ($ $primcall name args)) (($ $branch _ ($ $primcall name args))
(cons* 'primcall name (subst-vars var-substs args))) (cons* name (subst-vars var-substs args)))
(($ $branch) #f) (($ $branch) #f)
(($ $values args) #f) (($ $values args) #f)
(($ $prompt escape? tag handler) #f))) (($ $prompt escape? tag handler) #f)))
@ -266,61 +266,61 @@ false. It could be that both true and false proofs are available."
(hash-set! equiv-set aux-key (hash-set! equiv-set aux-key
(acons label (list var) equiv)))) (acons label (list var) equiv))))
(match exp-key (match exp-key
(('primcall 'box val) (('box val)
(match defs (match defs
((box) ((box)
(add-def! `(primcall box-ref ,(subst box)) val)))) (add-def! `(primcall box-ref ,(subst box)) val))))
(('primcall 'box-set! box val) (('box-set! box val)
(add-def! `(primcall box-ref ,box) val)) (add-def! `(primcall box-ref ,box) val))
(('primcall 'cons car cdr) (('cons car cdr)
(match defs (match defs
((pair) ((pair)
(add-def! `(primcall car ,(subst pair)) car) (add-def! `(primcall car ,(subst pair)) car)
(add-def! `(primcall cdr ,(subst pair)) cdr)))) (add-def! `(primcall cdr ,(subst pair)) cdr))))
(('primcall 'set-car! pair car) (('set-car! pair car)
(add-def! `(primcall car ,pair) car)) (add-def! `(primcall car ,pair) car))
(('primcall 'set-cdr! pair cdr) (('set-cdr! pair cdr)
(add-def! `(primcall cdr ,pair) cdr)) (add-def! `(primcall cdr ,pair) cdr))
(('primcall (or 'make-vector 'make-vector/immediate) len fill) (((or 'make-vector 'make-vector/immediate) len fill)
(match defs (match defs
((vec) ((vec)
(add-def! `(primcall vector-length ,(subst vec)) len)))) (add-def! `(primcall vector-length ,(subst vec)) len))))
(('primcall 'vector-set! vec idx val) (('vector-set! vec idx val)
(add-def! `(primcall vector-ref ,vec ,idx) val)) (add-def! `(primcall vector-ref ,vec ,idx) val))
(('primcall 'vector-set!/immediate vec idx val) (('vector-set!/immediate vec idx val)
(add-def! `(primcall vector-ref/immediate ,vec ,idx) val)) (add-def! `(primcall vector-ref/immediate ,vec ,idx) val))
(('primcall (or 'allocate-struct 'allocate-struct/immediate) (((or 'allocate-struct 'allocate-struct/immediate)
vtable size) vtable size)
(match defs (match defs
((struct) ((struct)
(add-def! `(primcall struct-vtable ,(subst struct)) (add-def! `(primcall struct-vtable ,(subst struct))
vtable)))) vtable))))
(('primcall 'struct-set! struct n val) (('struct-set! struct n val)
(add-def! `(primcall struct-ref ,struct ,n) val)) (add-def! `(primcall struct-ref ,struct ,n) val))
(('primcall 'struct-set!/immediate struct n val) (('struct-set!/immediate struct n val)
(add-def! `(primcall struct-ref/immediate ,struct ,n) val)) (add-def! `(primcall struct-ref/immediate ,struct ,n) val))
(('primcall 'scm->f64 scm) (('scm->f64 scm)
(match defs (match defs
((f64) ((f64)
(add-def! `(primcall f64->scm ,f64) scm)))) (add-def! `(primcall f64->scm ,f64) scm))))
(('primcall 'f64->scm f64) (('f64->scm f64)
(match defs (match defs
((scm) ((scm)
(add-def! `(primcall scm->f64 ,scm) f64)))) (add-def! `(primcall scm->f64 ,scm) f64))))
(('primcall 'scm->u64 scm) (('scm->u64 scm)
(match defs (match defs
((u64) ((u64)
(add-def! `(primcall u64->scm ,u64) scm)))) (add-def! `(primcall u64->scm ,u64) scm))))
(('primcall 'u64->scm u64) (('u64->scm u64)
(match defs (match defs
((scm) ((scm)
(add-def! `(primcall scm->u64 ,scm) u64) (add-def! `(primcall scm->u64 ,scm) u64)
(add-def! `(primcall scm->u64/truncate ,scm) u64)))) (add-def! `(primcall scm->u64/truncate ,scm) u64))))
(('primcall 'scm->s64 scm) (('scm->s64 scm)
(match defs (match defs
((s64) ((s64)
(add-def! `(primcall s64->scm ,s64) scm)))) (add-def! `(primcall s64->scm ,s64) scm))))
(('primcall 's64->scm s64) (('s64->scm s64)
(match defs (match defs
((scm) ((scm)
(add-def! `(primcall scm->s64 ,scm) s64)))) (add-def! `(primcall scm->s64 ,scm) s64))))
@ -329,55 +329,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