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-07-04 01:45:06 -04:00
|
|
|
fry make combinators sets locals
|
2008-10-07 21:00:38 -04:00
|
|
|
cpu.architecture
|
2009-07-07 04:28:55 -04:00
|
|
|
compiler.cfg
|
2008-10-20 02:56:28 -04:00
|
|
|
compiler.cfg.def-use
|
2009-07-07 04:28:55 -04:00
|
|
|
compiler.cfg.liveness
|
2008-09-15 03:59:24 -04:00
|
|
|
compiler.cfg.registers
|
|
|
|
compiler.cfg.instructions
|
2009-07-09 00:07:06 -04:00
|
|
|
compiler.cfg.linear-scan.mapping
|
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-07-04 00:38:52 -04:00
|
|
|
! Mapping from basic blocks to values which are live at the start
|
|
|
|
SYMBOL: register-live-ins
|
|
|
|
|
|
|
|
! Mapping from basic blocks to values which are live at the end
|
|
|
|
SYMBOL: register-live-outs
|
|
|
|
|
|
|
|
: init-assignment ( live-intervals -- )
|
|
|
|
V{ } clone pending-intervals set
|
|
|
|
<min-heap> unhandled-intervals set
|
|
|
|
H{ } clone register-live-ins set
|
|
|
|
H{ } clone register-live-outs set
|
|
|
|
init-unhandled ;
|
|
|
|
|
2009-06-05 19:06:47 -04:00
|
|
|
: handle-spill ( live-interval -- )
|
2009-07-09 00:07:06 -04:00
|
|
|
dup spill-to>> [
|
|
|
|
[ reg>> ] [ spill-to>> <spill-slot> ] [ vreg>> reg-class>> ] tri
|
|
|
|
register->memory
|
|
|
|
] [ drop ] if ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-07-01 18:41:07 -04:00
|
|
|
: first-split ( live-interval -- live-interval' )
|
|
|
|
dup split-before>> [ first-split ] [ ] ?if ;
|
|
|
|
|
2009-06-30 22:07:39 -04:00
|
|
|
: next-interval ( live-interval -- live-interval' )
|
2009-07-01 18:41:07 -04:00
|
|
|
split-next>> first-split ;
|
2009-06-30 22:07:39 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
: handle-copy ( live-interval -- )
|
2009-07-09 00:07:06 -04:00
|
|
|
dup split-next>> [
|
|
|
|
[ reg>> ] [ next-interval reg>> ] [ vreg>> reg-class>> ] tri
|
|
|
|
register->register
|
|
|
|
] [ drop ] if ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
2008-09-15 03:59:24 -04:00
|
|
|
: expire-old-intervals ( n -- )
|
2009-07-09 00:07:06 -04:00
|
|
|
[
|
|
|
|
[ pending-intervals get ] dip '[
|
|
|
|
dup end>> _ <
|
|
|
|
[ [ handle-spill ] [ handle-copy ] bi f ] [ drop t ] if
|
|
|
|
] filter-here
|
|
|
|
] { } make mapping-instructions % ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
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>> ]
|
2009-07-07 14:01:27 -04:00
|
|
|
[ start>> ]
|
2009-06-21 01:20:01 -04:00
|
|
|
} cleave f swap \ _reload boa , ;
|
2009-06-05 19:06:47 -04:00
|
|
|
|
|
|
|
: handle-reload ( live-interval -- )
|
2009-07-07 04:28:55 -04:00
|
|
|
dup reload-from>> [ insert-reload ] [ 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 ] [
|
2009-07-07 14:01:27 -04:00
|
|
|
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-07-04 01:45:06 -04:00
|
|
|
: prepare-insn ( n -- )
|
|
|
|
[ expire-old-intervals ] [ activate-new-intervals ] bi ;
|
2009-07-04 00:38:52 -04:00
|
|
|
|
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 )
|
2009-07-04 00:11:23 -04:00
|
|
|
[ [ vreg>> ] [ reg>> ] bi ] H{ } 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-30 19:10:53 -04:00
|
|
|
: check-assignment ( intervals -- )
|
2009-07-10 00:12:49 -04:00
|
|
|
dup [ copy-from>> ] map sift [ vreg>> ] map '[ vreg>> _ member? not ] filter
|
2009-06-30 19:10:53 -04:00
|
|
|
dup [ reg>> ] map all-unique? [ drop ] [ overlapping-registers ] if ;
|
|
|
|
|
2009-07-04 00:38:52 -04:00
|
|
|
: active-intervals ( n -- intervals )
|
|
|
|
pending-intervals get [ covers? ] with filter
|
2009-07-05 23:51:53 -04:00
|
|
|
check-assignment? get [ dup check-assignment ] when ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
M: vreg-insn assign-registers-in-insn
|
2009-07-05 23:51:53 -04:00
|
|
|
dup [ all-vregs ] [ insn#>> active-intervals ] bi
|
|
|
|
'[ _ [ vreg>> = ] with find nip ] map
|
2009-06-11 18:55:14 -04:00
|
|
|
register-mapping
|
|
|
|
>>regs drop ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
M: ##gc assign-registers-in-insn
|
2009-07-07 04:28:55 -04:00
|
|
|
! This works because ##gc is always the first instruction
|
|
|
|
! in a block.
|
2009-06-11 18:55:14 -04:00
|
|
|
dup call-next-method
|
2009-07-07 04:28:55 -04:00
|
|
|
basic-block get register-live-ins get at >>live-values
|
2009-06-02 19:23:47 -04:00
|
|
|
drop ;
|
|
|
|
|
2009-06-11 18:55:14 -04:00
|
|
|
M: insn assign-registers-in-insn drop ;
|
2009-05-31 19:21:23 -04:00
|
|
|
|
2009-07-07 04:28:55 -04:00
|
|
|
: compute-live-spill-slots ( vregs -- assoc )
|
|
|
|
spill-slots get '[ _ at dup [ <spill-slot> ] when ] assoc-map ;
|
|
|
|
|
|
|
|
: compute-live-registers ( n -- assoc )
|
|
|
|
active-intervals register-mapping ;
|
|
|
|
|
|
|
|
ERROR: bad-live-values live-values ;
|
|
|
|
|
|
|
|
: check-live-values ( assoc -- assoc )
|
|
|
|
check-assignment? get [
|
|
|
|
dup values [ not ] any? [ bad-live-values ] when
|
|
|
|
] when ;
|
|
|
|
|
|
|
|
: compute-live-values ( vregs n -- assoc )
|
|
|
|
! If a live vreg is not in active or inactive, then it must have been
|
|
|
|
! spilled.
|
|
|
|
[ compute-live-spill-slots ] [ compute-live-registers ] bi*
|
|
|
|
assoc-union check-live-values ;
|
|
|
|
|
2009-07-04 00:38:52 -04:00
|
|
|
: begin-block ( bb -- )
|
2009-07-07 04:28:55 -04:00
|
|
|
dup basic-block set
|
2009-07-05 23:51:53 -04:00
|
|
|
dup block-from prepare-insn
|
2009-07-07 04:28:55 -04:00
|
|
|
[ [ live-in ] [ block-from ] bi compute-live-values ] keep
|
|
|
|
register-live-ins get set-at ;
|
2009-07-04 00:38:52 -04:00
|
|
|
|
|
|
|
: end-block ( bb -- )
|
2009-07-07 04:28:55 -04:00
|
|
|
[ [ live-out ] [ block-to ] bi compute-live-values ] keep
|
|
|
|
register-live-outs get set-at ;
|
2009-07-04 00:38:52 -04:00
|
|
|
|
2009-07-04 01:45:06 -04:00
|
|
|
ERROR: bad-vreg vreg ;
|
2009-07-04 00:38:52 -04:00
|
|
|
|
2009-07-04 01:45:06 -04:00
|
|
|
: vreg-at-start ( vreg bb -- state )
|
|
|
|
register-live-ins get at ?at [ bad-vreg ] unless ;
|
2008-09-15 03:59:24 -04:00
|
|
|
|
2009-07-04 01:45:06 -04:00
|
|
|
: vreg-at-end ( vreg bb -- state )
|
|
|
|
register-live-outs get at ?at [ bad-vreg ] unless ;
|
|
|
|
|
|
|
|
:: assign-registers-in-block ( bb -- )
|
|
|
|
bb [
|
2008-09-15 03:59:24 -04:00
|
|
|
[
|
2009-07-04 01:45:06 -04:00
|
|
|
bb begin-block
|
2009-05-29 14:11:34 -04:00
|
|
|
[
|
2009-07-07 14:01:27 -04:00
|
|
|
{
|
|
|
|
[ insn#>> 1 - prepare-insn ]
|
|
|
|
[ insn#>> prepare-insn ]
|
|
|
|
[ assign-registers-in-insn ]
|
|
|
|
[ , ]
|
|
|
|
} cleave
|
2009-05-29 14:11:34 -04:00
|
|
|
] each
|
2009-07-04 01:45:06 -04:00
|
|
|
bb end-block
|
2009-05-29 14:11:34 -04:00
|
|
|
] V{ } make
|
2009-07-04 01:45:06 -04:00
|
|
|
] change-instructions drop ;
|
2009-05-29 14:11:34 -04:00
|
|
|
|
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 ;
|