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
|
2009-06-11 18:55:14 -04:00
|
|
|
compiler.cfg.linear-scan.allocation.state
|
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
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
! This contains both active and inactive intervals; any interval
|
|
|
|
! such that start <= insn# <= end is in this set.
|
|
|
|
SYMBOL: pending-intervals
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: add-active ( live-interval -- )
|
2009-06-11 18:55:14 -04:00
|
|
|
pending-intervals get push ;
|
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 ;
|
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
ERROR: already-spilled ;
|
|
|
|
|
2009-06-05 19:06:47 -04:00
|
|
|
: record-spill ( live-interval -- )
|
|
|
|
[ dup spill-to>> ] [ vreg>> spill-slots-for ] bi
|
2009-06-11 18:55:14 -04:00
|
|
|
2dup key? [ already-spilled ] [ set-at ] if ;
|
2009-06-05 19:06:47 -04:00
|
|
|
|
2008-09-15 03:59:24 -04:00
|
|
|
: insert-spill ( live-interval -- )
|
2009-06-21 01:20:01 -04:00
|
|
|
{
|
|
|
|
[ reg>> ]
|
|
|
|
[ vreg>> reg-class>> ]
|
|
|
|
[ spill-to>> ]
|
|
|
|
[ end>> ]
|
|
|
|
} cleave f swap \ _spill boa , ;
|
2009-06-05 19:06:47 -04:00
|
|
|
|
|
|
|
: handle-spill ( live-interval -- )
|
|
|
|
dup spill-to>> [ [ record-spill ] [ insert-spill ] bi ] [ drop ] if ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
: insert-copy ( live-interval -- )
|
2009-06-21 01:20:01 -04:00
|
|
|
{
|
|
|
|
[ split-next>> reg>> ]
|
|
|
|
[ reg>> ]
|
|
|
|
[ vreg>> reg-class>> ]
|
|
|
|
[ end>> ]
|
|
|
|
} cleave f swap \ _copy boa , ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
|
|
|
: handle-copy ( live-interval -- )
|
|
|
|
dup [ spill-to>> not ] [ split-next>> ] bi and
|
|
|
|
[ insert-copy ] [ drop ] if ;
|
|
|
|
|
2008-09-15 03:59:24 -04:00
|
|
|
: expire-old-intervals ( n -- )
|
2009-06-11 18:55:14 -04:00
|
|
|
[ pending-intervals get ] dip '[
|
|
|
|
dup end>> _ <
|
|
|
|
[ [ handle-spill ] [ handle-copy ] bi f ] [ drop t ] if
|
|
|
|
] filter-here ;
|
|
|
|
|
|
|
|
ERROR: already-reloaded ;
|
2009-06-05 19:06:47 -04:00
|
|
|
|
|
|
|
: record-reload ( live-interval -- )
|
|
|
|
[ reload-from>> ] [ vreg>> spill-slots-for ] bi
|
2009-06-11 18:55:14 -04:00
|
|
|
2dup key? [ delete-at ] [ already-reloaded ] if ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
|
|
|
: insert-reload ( live-interval -- )
|
2009-06-21 01:20:01 -04:00
|
|
|
{
|
|
|
|
[ reg>> ]
|
|
|
|
[ vreg>> reg-class>> ]
|
|
|
|
[ reload-from>> ]
|
|
|
|
[ end>> ]
|
|
|
|
} cleave f swap \ _reload boa , ;
|
2009-06-05 19:06:47 -04:00
|
|
|
|
|
|
|
: 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-11 18:55:14 -04:00
|
|
|
GENERIC: assign-registers-in-insn ( insn -- )
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
: register-mapping ( live-intervals -- alist )
|
|
|
|
[ [ vreg>> ] [ reg>> ] bi ] { } map>assoc ;
|
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-30 17:07:58 -04:00
|
|
|
SYMBOL: check-assignment?
|
|
|
|
|
|
|
|
ERROR: overlapping-registers intervals ;
|
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
: active-intervals ( insn -- intervals )
|
2009-06-30 17:07:58 -04:00
|
|
|
insn#>> pending-intervals get [ covers? ] with filter
|
|
|
|
check-assignment? get [
|
|
|
|
dup [ reg>> ] map all-unique?
|
|
|
|
[ overlapping-registers ] unless
|
|
|
|
] when ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
M: vreg-insn assign-registers-in-insn
|
|
|
|
dup [ active-intervals ] [ all-vregs ] bi
|
|
|
|
'[ vreg>> _ member? ] filter
|
|
|
|
register-mapping
|
|
|
|
>>regs drop ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
: compute-live-registers ( insn -- regs )
|
2009-06-20 16:33:12 -04:00
|
|
|
[ active-intervals ] [ temp-vregs ] bi
|
|
|
|
'[ vreg>> _ memq? not ] filter
|
|
|
|
register-mapping ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
|
|
|
: 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 ;
|
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
M: ##gc assign-registers-in-insn
|
|
|
|
dup call-next-method
|
|
|
|
dup compute-live-registers >>live-registers
|
2009-06-02 19:23:47 -04:00
|
|
|
compute-live-spill-slots >>live-spill-slots
|
|
|
|
drop ;
|
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
M: insn assign-registers-in-insn drop ;
|
2009-05-31 19:21:23 -04:00
|
|
|
|
2008-09-15 05:22:12 -04:00
|
|
|
: init-assignment ( live-intervals -- )
|
2009-06-11 18:55:14 -04:00
|
|
|
V{ } clone pending-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-11 18:55:14 -04:00
|
|
|
[
|
|
|
|
insn#>>
|
|
|
|
[ expire-old-intervals ]
|
2009-06-13 18:35:40 -04:00
|
|
|
[ activate-new-intervals ]
|
2009-06-11 18:55:14 -04:00
|
|
|
bi
|
|
|
|
]
|
|
|
|
[ assign-registers-in-insn ]
|
|
|
|
[ , ]
|
|
|
|
tri
|
2009-05-29 14:11:34 -04:00
|
|
|
] each
|
|
|
|
] V{ } make
|
|
|
|
] change-instructions drop ;
|
|
|
|
|
2009-06-21 01:20:01 -04:00
|
|
|
: assign-registers ( live-intervals rpo -- )
|
|
|
|
[ init-assignment ] dip
|
2009-05-29 14:11:34 -04:00
|
|
|
[ assign-registers-in-block ] each ;
|