factor/basis/compiler/cfg/ssa/destruction/destruction.factor

146 lines
3.8 KiB
Factor
Raw Normal View History

2011-01-17 18:16:17 -05:00
! Copyright (C) 2009, 2011 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2014-12-13 19:10:21 -05:00
USING: accessors arrays assocs combinators compiler.cfg.def-use
compiler.cfg.dominance compiler.cfg.instructions
compiler.cfg.liveness compiler.cfg.parallel-copy
compiler.cfg.registers compiler.cfg.rpo compiler.cfg.ssa.cssa
compiler.cfg.ssa.destruction.leaders
compiler.cfg.ssa.interference
compiler.cfg.ssa.interference.live-ranges compiler.cfg.utilities
cpu.architecture kernel locals make namespaces sequences sets ;
2010-02-26 16:01:01 -05:00
FROM: namespaces => set ;
IN: compiler.cfg.ssa.destruction
SYMBOL: class-element-map
: class-elements ( vreg -- elts ) class-element-map get at ;
<PRIVATE
! Sequence of vreg pairs
SYMBOL: copies
: value-of ( vreg -- value )
dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
: init-coalescing ( -- )
defs get
[ keys unique leader-map set ]
[
[ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map
class-element-map set
] bi
V{ } clone copies set ;
: coalesce-elements ( merged vreg1 vreg2 -- )
! delete leader1's class, and set leader2's class to merged.
class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
: coalesce-vregs ( merged leader1 leader2 -- )
2dup swap leader-map get set-at coalesce-elements ;
GENERIC: prepare-insn ( insn -- )
: maybe-eliminate-copy-later ( dst src -- )
2array copies get push ;
2010-05-14 18:18:29 -04:00
M: insn prepare-insn drop ;
M: alien-call-insn prepare-insn drop ;
2010-05-14 18:18:29 -04:00
M: vreg-insn prepare-insn
[ temp-vregs [ leader-map get conjoin ] each ]
[
[ defs-vregs ] [ uses-vregs ] bi
2dup [ empty? not ] both? [
[ first ] bi@
2dup [ rep-of reg-class-of ] bi@ eq?
[ maybe-eliminate-copy-later ] [ 2drop ] if
] [ 2drop ] if
] bi ;
M: ##copy prepare-insn
[ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
2011-01-17 18:16:17 -05:00
M: ##parallel-copy prepare-insn
values>> [ first2 maybe-eliminate-copy-later ] each ;
: leaders ( vreg1 vreg2 -- vreg1' vreg2' )
[ leader ] bi@ ;
: vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
[ class-elements ] bi@ sets-interfere? ;
ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
:: must-eliminate-copy ( vreg1 vreg2 -- )
! Eliminate a copy.
vreg1 vreg2 = [
2011-01-17 18:16:17 -05:00
vreg1 vreg2 vregs-interfere?
[ vreg1 vreg2 vregs-shouldn't-interfere ]
[ vreg1 vreg2 coalesce-vregs ]
if
] unless ;
M: ##tagged>integer prepare-insn
2011-01-17 18:16:17 -05:00
[ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
M: ##phi prepare-insn
[ dst>> ] [ inputs>> values ] bi
2011-01-17 18:16:17 -05:00
[ leaders must-eliminate-copy ] with each ;
: prepare-coalescing ( cfg -- )
init-coalescing
[ [ prepare-insn ] each ] simple-analysis ;
2011-01-17 18:16:17 -05:00
:: maybe-eliminate-copy ( vreg1 vreg2 -- )
! Eliminate a copy if possible.
vreg1 vreg2 = [
2011-01-17 18:16:17 -05:00
vreg1 vreg2 vregs-interfere?
[ drop ] [ vreg1 vreg2 coalesce-vregs ] if
] unless ;
: process-copies ( copies -- )
[ leaders maybe-eliminate-copy ] assoc-each ;
: perform-coalescing ( cfg -- )
prepare-coalescing copies get process-copies ;
2011-01-17 18:16:17 -05:00
GENERIC: cleanup-insn ( insn -- )
2010-04-27 10:51:00 -04:00
: useful-copy? ( insn -- ? )
[ dst>> ] [ src>> ] bi leaders = not ; inline
2011-01-17 18:16:17 -05:00
M: ##copy cleanup-insn
dup useful-copy? [ , ] [ drop ] if ;
2010-04-27 10:51:00 -04:00
2011-01-17 18:16:17 -05:00
M: ##parallel-copy cleanup-insn
values>> [ leaders ] assoc-map [ first2 = not ] filter
parallel-copy-rep % ;
2010-04-27 10:51:00 -04:00
2011-01-17 18:16:17 -05:00
M: ##tagged>integer cleanup-insn
dup useful-copy? [ , ] [ drop ] if ;
2011-01-17 18:16:17 -05:00
M: ##phi cleanup-insn drop ;
2010-04-27 10:51:00 -04:00
2011-01-17 18:16:17 -05:00
M: insn cleanup-insn , ;
2010-04-27 10:51:00 -04:00
: cleanup-cfg ( cfg -- )
2011-01-17 18:16:17 -05:00
[ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
PRIVATE>
: destruct-ssa ( cfg -- )
H{ } clone leader-map set
{
needs-dominance
construct-cssa
compute-defs
compute-insns
compute-live-sets
compute-live-ranges
perform-coalescing
cleanup-cfg
compute-live-sets
} apply-passes ;