factor/basis/compiler/cfg/linear-scan/live-intervals/live-intervals.factor

130 lines
3.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2009 Slava Pestov.
2008-09-10 23:11:03 -04:00
! See http://factorcode.org/license.txt for BSD license.
2009-06-03 04:22:27 -04:00
USING: namespaces kernel assocs accessors sequences math math.order fry
binary-search compiler.cfg.instructions compiler.cfg.registers
2009-06-03 04:22:27 -04:00
compiler.cfg.def-use compiler.cfg.liveness compiler.cfg ;
2008-09-11 03:05:22 -04:00
IN: compiler.cfg.linear-scan.live-intervals
2008-09-10 23:11:03 -04:00
2009-06-03 04:22:27 -04:00
TUPLE: live-range from to ;
C: <live-range> live-range
2008-10-19 02:10:21 -04:00
TUPLE: live-interval
2008-09-15 02:54:48 -04:00
vreg
reg spill-to reload-from split-before split-after
2009-06-03 04:22:27 -04:00
start end ranges uses
2008-11-02 02:49:57 -05:00
copy-from ;
2008-09-10 23:11:03 -04:00
2009-06-03 04:22:27 -04:00
ERROR: dead-value-error vreg ;
: shorten-range ( n live-interval -- )
dup ranges>> empty?
[ vreg>> dead-value-error ] [ ranges>> last (>>from) ] if ;
: extend-range ( from to live-range -- )
ranges>> last
[ max ] change-to
[ min ] change-from
drop ;
: add-new-range ( from to live-interval -- )
[ <live-range> ] dip ranges>> push ;
: extend-range? ( to live-interval -- ? )
ranges>> [ drop f ] [ last from>> >= ] if-empty ;
: add-range ( from to live-interval -- )
2dup extend-range?
[ extend-range ] [ add-new-range ] if ;
2008-10-19 02:10:21 -04:00
: add-use ( n live-interval -- )
2009-06-03 04:22:27 -04:00
uses>> push ;
2008-10-19 02:10:21 -04:00
2009-06-03 04:22:27 -04:00
: <live-interval> ( vreg -- live-interval )
\ live-interval new
2008-10-19 02:10:21 -04:00
V{ } clone >>uses
2009-06-03 04:22:27 -04:00
V{ } clone >>ranges
swap >>vreg ;
: block-from ( -- n )
basic-block get instructions>> first insn#>> ;
: block-to ( -- n )
basic-block get instructions>> last insn#>> ;
2008-09-15 02:54:48 -04:00
M: live-interval hashcode*
nip [ start>> ] [ end>> 1000 * ] bi + ;
M: live-interval clone
call-next-method [ clone ] change-uses ;
2008-09-10 23:11:03 -04:00
! Mapping from vreg to live-interval
SYMBOL: live-intervals
2009-06-03 04:22:27 -04:00
: live-interval ( vreg live-intervals -- live-interval )
[ <live-interval> ] cache ;
2008-09-15 02:54:48 -04:00
GENERIC: compute-live-intervals* ( insn -- )
2008-11-02 02:49:57 -05:00
M: insn compute-live-intervals* drop ;
2008-11-02 04:58:32 -05:00
2009-06-03 04:22:27 -04:00
: handle-output ( n vreg live-intervals -- )
live-interval
[ add-use ] [ shorten-range ] 2bi ;
: handle-input ( n vreg live-intervals -- )
live-interval
[ [ block-from ] 2dip add-range ] [ add-use ] 2bi ;
: handle-temp ( n vreg live-intervals -- )
live-interval
[ dupd add-range ] [ add-use ] 2bi ;
2008-11-02 04:58:32 -05:00
M: vreg-insn compute-live-intervals*
dup insn#>>
2008-09-15 02:54:48 -04:00
live-intervals get
2009-06-03 04:22:27 -04:00
[ [ defs-vregs ] 2dip '[ [ _ ] dip _ handle-output ] each ]
[ [ uses-vregs ] 2dip '[ [ _ ] dip _ handle-input ] each ]
[ [ temp-vregs ] 2dip '[ [ _ ] dip _ handle-temp ] each ]
3tri ;
2008-09-10 23:11:03 -04:00
2008-11-02 02:49:57 -05:00
: record-copy ( insn -- )
[ dst>> live-intervals get at ] [ src>> ] bi >>copy-from drop ;
M: ##copy compute-live-intervals*
[ call-next-method ] [ record-copy ] bi ;
2008-11-02 02:49:57 -05:00
M: ##copy-float compute-live-intervals*
[ call-next-method ] [ record-copy ] bi ;
2008-11-02 02:49:57 -05:00
2009-06-03 04:22:27 -04:00
: handle-live-out ( bb -- )
live-out keys block-from block-to live-intervals get '[
[ _ _ ] dip _ live-interval add-range
] each ;
: compute-live-intervals-step ( bb -- )
[ basic-block set ]
[ handle-live-out ]
[ instructions>> <reversed> [ compute-live-intervals* ] each ] tri ;
: compute-start/end ( live-interval -- )
dup ranges>> [ first from>> ] [ last to>> ] bi
2dup > [ "BUG: start > end" throw ] when
2009-06-03 04:22:27 -04:00
[ >>start ] [ >>end ] bi* drop ;
: finish-live-intervals ( live-intervals -- )
! Since live intervals are computed in a backward order, we have
! to reverse some sequences, and compute the start and end.
[
[ ranges>> reverse-here ]
[ uses>> reverse-here ]
[ compute-start/end ]
tri
] each ;
: compute-live-intervals ( rpo -- live-intervals )
2008-09-15 05:22:12 -04:00
H{ } clone [
live-intervals set
2009-06-03 04:22:27 -04:00
<reversed> [ compute-live-intervals-step ] each
] keep values dup finish-live-intervals ;