2011-05-22 20:15:56 -04:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2011-06-23 20:39:46 -04:00
|
|
|
USING: namespaces arrays assocs hashtables kernel accessors fry
|
|
|
|
grouping sorting sets sequences locals
|
2011-05-22 20:15:56 -04:00
|
|
|
cpu.architecture
|
|
|
|
sequences.deep
|
|
|
|
compiler.cfg
|
|
|
|
compiler.cfg.rpo
|
|
|
|
compiler.cfg.def-use
|
|
|
|
compiler.cfg.utilities
|
|
|
|
compiler.cfg.instructions
|
2011-06-25 19:50:05 -04:00
|
|
|
compiler.cfg.predecessors
|
2011-05-22 20:15:56 -04:00
|
|
|
compiler.cfg.gvn.alien
|
2011-06-25 19:50:05 -04:00
|
|
|
compiler.cfg.gvn.avail
|
2011-05-22 20:15:56 -04:00
|
|
|
compiler.cfg.gvn.comparisons
|
|
|
|
compiler.cfg.gvn.graph
|
|
|
|
compiler.cfg.gvn.math
|
|
|
|
compiler.cfg.gvn.rewrite
|
|
|
|
compiler.cfg.gvn.slots
|
|
|
|
compiler.cfg.gvn.misc
|
2011-06-27 18:19:57 -04:00
|
|
|
compiler.cfg.gvn.expressions ;
|
2011-05-22 20:15:56 -04:00
|
|
|
IN: compiler.cfg.gvn
|
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
GENERIC: simplify ( insn -- insn' )
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
M: insn simplify dup rewrite [ simplify ] [ ] ?if ;
|
|
|
|
M: array simplify [ simplify ] map ;
|
|
|
|
M: ##copy simplify ;
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-07-02 16:41:25 -04:00
|
|
|
! ! ! Global value numbering
|
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
GENERIC: value-number ( insn -- )
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-06-28 15:13:02 -04:00
|
|
|
M: array value-number [ value-number ] each ;
|
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
M: alien-call-insn value-number drop ;
|
|
|
|
M: ##callback-inputs value-number drop ;
|
2011-06-28 15:13:02 -04:00
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
M: ##copy value-number [ src>> vreg>vn ] [ dst>> ] bi set-vn ;
|
2011-06-27 18:19:57 -04:00
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
: redundant-instruction ( insn vn -- )
|
|
|
|
swap dst>> set-vn ;
|
2011-06-27 18:19:57 -04:00
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
:: useful-instruction ( insn expr -- )
|
|
|
|
insn dst>> :> vn
|
|
|
|
vn vn set-vn
|
|
|
|
vn expr exprs>vns get set-at
|
|
|
|
insn vn vns>insns get set-at ;
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-06-28 15:13:02 -04:00
|
|
|
: check-redundancy ( insn -- )
|
|
|
|
dup >expr dup exprs>vns get at
|
|
|
|
[ redundant-instruction ] [ useful-instruction ] ?if ;
|
|
|
|
|
2011-07-02 16:41:25 -04:00
|
|
|
! M: ##phi value-number
|
|
|
|
! dup inputs>> values [ vreg>vn ] map sift
|
|
|
|
! dup all-equal? [
|
|
|
|
! [ drop ] [ first redundant-instruction ] if-empty
|
|
|
|
! ] [ drop check-redundancy ] if ;
|
2011-06-28 15:13:02 -04:00
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
M: insn value-number
|
2011-06-28 15:13:02 -04:00
|
|
|
dup defs-vregs length 1 = [ check-redundancy ] [ drop ] if ;
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-06-27 20:58:28 -04:00
|
|
|
: value-numbering-step ( insns -- )
|
2011-06-28 15:13:02 -04:00
|
|
|
[ simplify value-number ] each ;
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-06-09 15:08:25 -04:00
|
|
|
: value-numbering-iteration ( cfg -- )
|
2011-06-27 20:58:28 -04:00
|
|
|
clear-exprs [ value-numbering-step ] simple-analysis ;
|
2011-06-09 15:08:25 -04:00
|
|
|
|
2011-06-27 19:21:23 -04:00
|
|
|
: determine-value-numbers ( cfg -- )
|
2011-06-18 19:16:09 -04:00
|
|
|
final-iteration? off
|
2011-06-09 20:08:41 -04:00
|
|
|
init-value-graph
|
2011-05-22 20:15:56 -04:00
|
|
|
'[
|
|
|
|
changed? off
|
2011-06-09 15:08:25 -04:00
|
|
|
_ value-numbering-iteration
|
2011-05-22 20:15:56 -04:00
|
|
|
changed? get
|
2011-06-09 20:08:41 -04:00
|
|
|
] loop ;
|
2011-05-22 20:15:56 -04:00
|
|
|
|
2011-07-02 16:41:25 -04:00
|
|
|
! ! ! Global common subexpression elimination
|
|
|
|
|
|
|
|
GENERIC: gcse ( insn -- insn' )
|
|
|
|
|
|
|
|
M: array gcse [ gcse ] map ;
|
|
|
|
|
|
|
|
M: alien-call-insn gcse ;
|
|
|
|
M: ##callback-inputs gcse ;
|
|
|
|
M: ##copy gcse ;
|
|
|
|
|
|
|
|
: ?eliminate ( insn vn -- insn' )
|
|
|
|
dup available? [
|
|
|
|
[ dst>> ] dip <copy>
|
|
|
|
] [ drop make-available ] if ;
|
|
|
|
|
|
|
|
: eliminate-redundancy ( insn -- insn' )
|
|
|
|
dup >expr exprs>vns get at
|
|
|
|
[ ?eliminate ] [ make-available ] if* ;
|
|
|
|
|
|
|
|
! M: ##phi gcse
|
|
|
|
! dup inputs>> values [ vreg>vn ] map sift
|
|
|
|
! dup all-equal? [
|
|
|
|
! [ first ?eliminate ] unless-empty
|
|
|
|
! ] [ drop eliminate-redundancy ] if ;
|
|
|
|
|
|
|
|
M: insn gcse
|
|
|
|
dup defs-vregs length 1 = [ eliminate-redundancy ] when ;
|
|
|
|
|
|
|
|
: gcse-step ( insns -- insns' )
|
|
|
|
! [ simplify gcse ] map flatten ;
|
|
|
|
[ gcse ] map flatten ;
|
|
|
|
|
|
|
|
: eliminate-common-subexpressions ( cfg -- )
|
|
|
|
final-iteration? on
|
|
|
|
dup compute-avail-sets
|
|
|
|
[ gcse-step ] simple-optimization ;
|
|
|
|
|
2011-06-09 20:08:41 -04:00
|
|
|
: value-numbering ( cfg -- cfg )
|
2011-06-25 19:50:05 -04:00
|
|
|
needs-predecessors
|
2011-06-27 19:21:23 -04:00
|
|
|
dup determine-value-numbers
|
2011-07-02 16:41:25 -04:00
|
|
|
dup eliminate-common-subexpressions
|
2011-06-25 19:50:05 -04:00
|
|
|
|
2011-05-22 20:15:56 -04:00
|
|
|
cfg-changed predecessors-changed ;
|