2009-06-05 19:06:47 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-09-15 03:59:24 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: accessors kernel math assocs namespaces sequences heaps
|
2009-06-05 19:06:47 -04:00
|
|
|
fry make combinators sets
|
2008-10-07 21:00:38 -04:00
|
|
|
cpu.architecture
|
2008-10-20 02:56:28 -04:00
|
|
|
compiler.cfg.def-use
|
2008-09-15 03:59:24 -04:00
|
|
|
compiler.cfg.registers
|
|
|
|
compiler.cfg.instructions
|
2009-06-05 19:06:47 -04:00
|
|
|
compiler.cfg.linear-scan.allocation
|
2008-09-15 03:59:24 -04:00
|
|
|
compiler.cfg.linear-scan.live-intervals ;
|
2008-09-15 05:22:12 -04:00
|
|
|
IN: compiler.cfg.linear-scan.assignment
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
! A vector of live intervals. There is linear searching involved
|
|
|
|
! but since we never have too many machine registers (around 30
|
|
|
|
! at most) and we probably won't have that many live at any one
|
|
|
|
! time anyway, it is not a problem to check each element.
|
2009-05-31 19:21:23 -04:00
|
|
|
TUPLE: active-intervals seq ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: add-active ( live-interval -- )
|
2009-05-31 19:21:23 -04:00
|
|
|
active-intervals get seq>> push ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: lookup-register ( vreg -- reg )
|
2009-05-31 19:21:23 -04:00
|
|
|
active-intervals get seq>> [ vreg>> = ] with find nip reg>> ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
! Minheap of live intervals which still need a register allocation
|
|
|
|
SYMBOL: unhandled-intervals
|
|
|
|
|
|
|
|
: add-unhandled ( live-interval -- )
|
2009-06-04 19:53:02 -04:00
|
|
|
dup start>> unhandled-intervals get heap-push ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: init-unhandled ( live-intervals -- )
|
|
|
|
[ add-unhandled ] each ;
|
|
|
|
|
2009-06-05 19:06:47 -04:00
|
|
|
! Mapping spill slots to vregs
|
|
|
|
SYMBOL: spill-slots
|
|
|
|
|
|
|
|
: spill-slots-for ( vreg -- assoc )
|
|
|
|
reg-class>> spill-slots get at ;
|
|
|
|
|
|
|
|
: record-spill ( live-interval -- )
|
|
|
|
[ dup spill-to>> ] [ vreg>> spill-slots-for ] bi
|
|
|
|
2dup key? [ "BUG: Already spilled" throw ] [ set-at ] if ;
|
|
|
|
|
2008-09-15 03:59:24 -04:00
|
|
|
: insert-spill ( live-interval -- )
|
2009-06-05 19:06:47 -04:00
|
|
|
[ reg>> ] [ vreg>> reg-class>> ] [ spill-to>> ] tri _spill ;
|
|
|
|
|
|
|
|
: handle-spill ( live-interval -- )
|
|
|
|
dup spill-to>> [ [ record-spill ] [ insert-spill ] bi ] [ drop ] if ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: expire-old-intervals ( n -- )
|
|
|
|
active-intervals get
|
2009-05-31 19:21:23 -04:00
|
|
|
[ swap '[ end>> _ = ] partition ] change-seq drop
|
2009-06-05 19:06:47 -04:00
|
|
|
[ handle-spill ] each ;
|
|
|
|
|
|
|
|
: record-reload ( live-interval -- )
|
|
|
|
[ reload-from>> ] [ vreg>> spill-slots-for ] bi
|
|
|
|
2dup key? [ delete-at ] [ "BUG: Already reloaded" throw ] if ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: insert-reload ( live-interval -- )
|
2009-06-05 19:06:47 -04:00
|
|
|
[ reg>> ] [ vreg>> reg-class>> ] [ reload-from>> ] tri _reload ;
|
|
|
|
|
|
|
|
: handle-reload ( live-interval -- )
|
|
|
|
dup reload-from>> [ [ record-reload ] [ insert-reload ] bi ] [ drop ] if ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: activate-new-intervals ( n -- )
|
|
|
|
#! Any live intervals which start on the current instruction
|
|
|
|
#! are added to the active set.
|
|
|
|
unhandled-intervals get dup heap-empty? [ 2drop ] [
|
|
|
|
2dup heap-peek drop start>> = [
|
2009-06-05 19:06:47 -04:00
|
|
|
heap-pop drop
|
|
|
|
[ add-active ] [ handle-reload ] bi
|
2008-09-15 03:59:24 -04:00
|
|
|
activate-new-intervals
|
|
|
|
] [ 2drop ] if
|
|
|
|
] if ;
|
|
|
|
|
2009-06-02 19:23:47 -04:00
|
|
|
GENERIC: assign-before ( insn -- )
|
|
|
|
|
|
|
|
GENERIC: assign-after ( insn -- )
|
2008-11-02 04:58:32 -05:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
: all-vregs ( insn -- vregs )
|
|
|
|
[ defs-vregs ] [ temp-vregs ] [ uses-vregs ] tri 3append ;
|
|
|
|
|
2009-06-02 19:23:47 -04:00
|
|
|
M: vreg-insn assign-before
|
2009-05-31 19:21:23 -04:00
|
|
|
active-intervals get seq>> over all-vregs '[ vreg>> _ member? ] filter
|
2008-09-15 05:22:12 -04:00
|
|
|
[ [ vreg>> ] [ reg>> ] bi ] { } map>assoc
|
|
|
|
>>regs drop ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-06-02 19:23:47 -04:00
|
|
|
M: insn assign-before drop ;
|
|
|
|
|
|
|
|
: compute-live-registers ( -- regs )
|
|
|
|
active-intervals get seq>> [ [ vreg>> ] [ reg>> ] bi ] { } map>assoc ;
|
|
|
|
|
|
|
|
: compute-live-spill-slots ( -- spill-slots )
|
2009-06-05 19:06:47 -04:00
|
|
|
spill-slots get values [ values ] map concat
|
2009-06-02 19:23:47 -04:00
|
|
|
[ [ vreg>> ] [ reload-from>> ] bi ] { } map>assoc ;
|
|
|
|
|
|
|
|
M: ##gc assign-after
|
|
|
|
compute-live-registers >>live-registers
|
|
|
|
compute-live-spill-slots >>live-spill-slots
|
|
|
|
drop ;
|
|
|
|
|
|
|
|
M: insn assign-after drop ;
|
2008-11-02 04:58:32 -05:00
|
|
|
|
2009-05-31 19:21:23 -04:00
|
|
|
: <active-intervals> ( -- obj )
|
|
|
|
V{ } clone active-intervals boa ;
|
|
|
|
|
2008-09-15 05:22:12 -04:00
|
|
|
: init-assignment ( live-intervals -- )
|
2009-05-31 19:21:23 -04:00
|
|
|
<active-intervals> active-intervals set
|
2008-09-15 05:22:12 -04:00
|
|
|
<min-heap> unhandled-intervals set
|
2009-06-05 19:06:47 -04:00
|
|
|
[ H{ } clone ] reg-class-assoc spill-slots set
|
2008-09-15 05:22:12 -04:00
|
|
|
init-unhandled ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
: assign-registers-in-block ( bb -- )
|
2008-09-15 03:59:24 -04:00
|
|
|
[
|
|
|
|
[
|
2009-05-29 14:11:34 -04:00
|
|
|
[
|
2009-06-02 19:23:47 -04:00
|
|
|
{
|
|
|
|
[ insn#>> activate-new-intervals ]
|
|
|
|
[ assign-before ]
|
|
|
|
[ , ]
|
|
|
|
[ insn#>> expire-old-intervals ]
|
|
|
|
[ assign-after ]
|
|
|
|
} cleave
|
2009-05-29 14:11:34 -04:00
|
|
|
] each
|
|
|
|
] V{ } make
|
|
|
|
] change-instructions drop ;
|
|
|
|
|
|
|
|
: assign-registers ( rpo live-intervals -- )
|
|
|
|
init-assignment
|
|
|
|
[ assign-registers-in-block ] each ;
|