! Copyright (C) 2008, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: namespaces kernel assocs accessors locals sequences math
-math.order fry combinators binary-search
-compiler.cfg.instructions
-compiler.cfg.registers
-compiler.cfg.def-use
-compiler.cfg.liveness
-compiler.cfg.linearization
-compiler.cfg.ssa.destruction
-compiler.cfg
-cpu.architecture ;
+USING: accessors assocs binary-search combinators
+compiler.cfg.def-use compiler.cfg.instructions
+compiler.cfg.linearization compiler.cfg.liveness
+compiler.cfg.registers compiler.cfg.ssa.destruction.leaders cpu.architecture
+fry kernel locals math math.intervals math.order namespaces sequences ;
IN: compiler.cfg.linear-scan.live-intervals
TUPLE: live-range from to ;
: <vreg-use> ( n -- vreg-use ) vreg-use new swap >>n ;
-TUPLE: live-interval
+TUPLE: live-interval-state
vreg
reg spill-to spill-rep reload-from reload-rep
start end ranges uses
M: live-range covers? [ from>> ] [ to>> ] bi between? ;
-M: live-interval covers? ( insn# live-interval -- ? )
+M: live-interval-state covers? ( insn# live-interval -- ? )
ranges>>
dup length 4 <= [
[ covers? ] with any?
[ extend-range ] [ add-new-range ] if ;
: <live-interval> ( vreg reg-class -- live-interval )
- \ live-interval new
+ \ live-interval-state new
V{ } clone >>uses
V{ } clone >>ranges
swap >>reg-class
SYMBOLS: from to ;
-! Mapping from vreg to live-interval
SYMBOL: live-intervals
: live-interval ( vreg -- live-interval )
[ [ temp-vregs ] dip '[ _ record-temp ] each ]
2tri ;
+! Extend lifetime intervals of base pointers, so that their
+! values are available even if the base pointer is never used
+! again.
+
+GENERIC: uses-vregs* ( insn -- seq )
+
+M: gc-map-insn uses-vregs*
+ [ uses-vregs ] [ gc-map>> derived-roots>> values ] bi append ;
+
+M: vreg-insn uses-vregs* uses-vregs ;
+
+M: insn uses-vregs* drop f ;
+
M: clobber-insn compute-live-intervals* ( insn -- )
dup insn#>>
[ [ defs-vregs ] dip '[ _ f record-def ] each ]
- [ [ uses-vregs ] dip '[ _ t record-use ] each ]
+ [ [ uses-vregs* ] dip '[ _ t record-use ] each ]
[ [ temp-vregs ] dip '[ _ record-temp ] each ]
2tri ;
M: hairy-clobber-insn compute-live-intervals* ( insn -- )
dup insn#>>
[ [ defs-vregs ] dip '[ _ t record-def ] each ]
- [ [ uses-vregs ] dip '[ _ t record-use ] each ]
+ [ [ uses-vregs* ] dip '[ _ t record-use ] each ]
[ [ temp-vregs ] dip '[ _ record-temp ] each ]
2tri ;
: handle-live-out ( bb -- )
live-out dup assoc-empty? [ drop ] [
[ from get to get ] dip keys
- [ live-interval add-range ] with with each
+ [ live-interval add-range ] 2with each
] if ;
-! A location where all registers have to be spilled
TUPLE: sync-point n keep-dst? ;
C: <sync-point> sync-point
-! Sequence of sync points
SYMBOL: sync-points
GENERIC: compute-sync-points* ( insn -- )
M: insn compute-sync-points* drop ;
: compute-live-intervals-step ( bb -- )
- dup kill-block?>> [ drop ] [
- {
- [ block-from from set ]
- [ block-to to set ]
- [ handle-live-out ]
- [
- instructions>> <reversed> [
- [ compute-live-intervals* ]
- [ compute-sync-points* ]
- bi
- ] each
- ]
- } cleave
- ] if ;
+ {
+ [ block-from from set ]
+ [ block-to to set ]
+ [ handle-live-out ]
+ [
+ instructions>> <reversed> [
+ [ compute-live-intervals* ]
+ [ compute-sync-points* ]
+ bi
+ ] each
+ ]
+ } cleave ;
: init-live-intervals ( -- )
H{ } clone live-intervals set
dup start>> -1 = [ bad-live-interval ] [ drop ] if ;
: finish-live-intervals ( live-intervals -- seq )
- ! Since live intervals are computed in a backward order, we have
- ! to reverse some sequences, and compute the start and end.
values dup [
{
[ [ { } like reverse! ] change-ranges drop ]
: compute-live-intervals ( cfg -- live-intervals sync-points )
init-live-intervals
- linearization-order <reversed> [ compute-live-intervals-step ] each
+ linearization-order <reversed> [ kill-block?>> ] reject
+ [ compute-live-intervals-step ] each
live-intervals get finish-live-intervals
sync-points get ;