factor/basis/compiler/cfg/linear-scan/assignment/assignment.factor

131 lines
3.9 KiB
Factor
Raw Normal View History

! 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
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
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.
TUPLE: active-intervals seq ;
2008-09-15 03:59:24 -04:00
: add-active ( live-interval -- )
active-intervals get seq>> push ;
2008-09-15 03:59:24 -04:00
: lookup-register ( vreg -- reg )
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 -- )
dup start>> unhandled-intervals get heap-push ;
2008-09-15 03:59:24 -04:00
: init-unhandled ( live-intervals -- )
[ add-unhandled ] each ;
! 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 -- )
[ 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
[ swap '[ end>> _ = ] partition ] change-seq drop
[ 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 -- )
[ 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>> = [
heap-pop drop
[ add-active ] [ handle-reload ] bi
2008-09-15 03:59:24 -04:00
activate-new-intervals
] [ 2drop ] if
] if ;
GENERIC: assign-before ( insn -- )
GENERIC: assign-after ( insn -- )
2008-11-02 04:58:32 -05:00
: all-vregs ( insn -- vregs )
[ defs-vregs ] [ temp-vregs ] [ uses-vregs ] tri 3append ;
M: vreg-insn assign-before
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
M: insn assign-before drop ;
: compute-live-registers ( -- regs )
active-intervals get seq>> [ [ vreg>> ] [ reg>> ] bi ] { } map>assoc ;
: compute-live-spill-slots ( -- spill-slots )
spill-slots get values [ values ] map concat
[ [ 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
: <active-intervals> ( -- obj )
V{ } clone active-intervals boa ;
2008-09-15 05:22:12 -04:00
: init-assignment ( live-intervals -- )
<active-intervals> active-intervals set
2008-09-15 05:22:12 -04:00
<min-heap> unhandled-intervals set
[ 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
: assign-registers-in-block ( bb -- )
2008-09-15 03:59:24 -04:00
[
[
[
{
[ insn#>> activate-new-intervals ]
[ assign-before ]
[ , ]
[ insn#>> expire-old-intervals ]
[ assign-after ]
} cleave
] each
] V{ } make
] change-instructions drop ;
: assign-registers ( rpo live-intervals -- )
init-assignment
[ assign-registers-in-block ] each ;