2009-06-26 22:48:21 -04:00
|
|
|
! Copyright (C) 2009 Slava Pestov, Doug Coleman.
|
2009-06-11 18:55:14 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-06-26 22:48:21 -04:00
|
|
|
USING: accessors arrays assocs classes.parser classes.tuple
|
|
|
|
combinators combinators.short-circuit compiler.cfg.instructions
|
|
|
|
compiler.cfg.linear-scan.live-intervals compiler.cfg.liveness
|
2009-06-28 17:43:17 -04:00
|
|
|
fry hashtables kernel locals make math math.order
|
2009-06-26 22:48:21 -04:00
|
|
|
namespaces parser prettyprint random sequences sets
|
2009-06-28 17:43:17 -04:00
|
|
|
sorting.functor sorting.slots words io ;
|
2009-06-11 18:55:14 -04:00
|
|
|
IN: compiler.cfg.linear-scan.resolve
|
|
|
|
|
2009-06-21 01:20:01 -04:00
|
|
|
<<
|
|
|
|
|
|
|
|
TUPLE: operation from to reg-class ;
|
|
|
|
|
|
|
|
SYNTAX: OPERATION:
|
|
|
|
CREATE-CLASS dup save-location
|
|
|
|
[ operation { } define-tuple-class ]
|
|
|
|
[
|
|
|
|
[ scan-word scan-word ] keep
|
|
|
|
'[
|
|
|
|
[ [ _ execute ] [ _ execute ] bi* ]
|
|
|
|
[ vreg>> reg-class>> ]
|
|
|
|
bi _ boa ,
|
|
|
|
] (( from to -- )) define-declared
|
|
|
|
] bi ;
|
|
|
|
|
|
|
|
>>
|
|
|
|
|
2009-06-22 01:24:51 -04:00
|
|
|
: reload-from ( bb live-interval -- n/f )
|
|
|
|
2dup [ block-from ] [ start>> ] bi* =
|
|
|
|
[ nip reload-from>> ] [ 2drop f ] if ;
|
|
|
|
|
|
|
|
: spill-to ( bb live-interval -- n/f )
|
|
|
|
2dup [ block-to ] [ end>> ] bi* =
|
|
|
|
[ nip spill-to>> ] [ 2drop f ] if ;
|
|
|
|
|
2009-06-21 01:20:01 -04:00
|
|
|
OPERATION: memory->memory spill-to>> reload-from>>
|
|
|
|
OPERATION: register->memory reg>> reload-from>>
|
|
|
|
OPERATION: memory->register spill-to>> reg>>
|
|
|
|
OPERATION: register->register reg>> reg>>
|
|
|
|
|
2009-06-22 01:24:51 -04:00
|
|
|
:: add-mapping ( bb1 bb2 li1 li2 -- )
|
|
|
|
bb2 li2 reload-from [
|
|
|
|
bb1 li1 spill-to
|
|
|
|
[ li1 li2 memory->memory ]
|
|
|
|
[ li1 li2 register->memory ] if
|
2009-06-21 01:20:01 -04:00
|
|
|
] [
|
2009-06-22 01:24:51 -04:00
|
|
|
bb1 li1 spill-to
|
|
|
|
[ li1 li2 memory->register ]
|
|
|
|
[ li1 li2 register->register ] if
|
2009-06-21 01:20:01 -04:00
|
|
|
] if ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
|
|
|
: resolve-value-data-flow ( bb to vreg -- )
|
2009-06-22 01:24:51 -04:00
|
|
|
[ 2dup ] dip
|
2009-06-11 18:55:14 -04:00
|
|
|
live-intervals get at
|
|
|
|
[ [ block-to ] dip child-interval-at ]
|
|
|
|
[ [ block-from ] dip child-interval-at ]
|
2009-06-22 01:24:51 -04:00
|
|
|
bi-curry bi* 2dup eq? [ 2drop 2drop ] [ add-mapping ] if ;
|
2009-06-21 01:20:01 -04:00
|
|
|
|
|
|
|
: compute-mappings ( bb to -- mappings )
|
|
|
|
[
|
|
|
|
dup live-in keys
|
|
|
|
[ resolve-value-data-flow ] with with each
|
|
|
|
] { } make ;
|
|
|
|
|
|
|
|
GENERIC: >insn ( operation -- )
|
|
|
|
|
|
|
|
M: memory->memory >insn
|
|
|
|
[ from>> ] [ to>> ] bi = [ "Not allowed" throw ] unless ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
2009-06-21 01:20:01 -04:00
|
|
|
M: register->memory >insn
|
|
|
|
[ from>> ] [ reg-class>> ] [ to>> ] tri _spill ;
|
|
|
|
|
|
|
|
M: memory->register >insn
|
|
|
|
[ to>> ] [ reg-class>> ] [ from>> ] tri _reload ;
|
|
|
|
|
|
|
|
M: register->register >insn
|
|
|
|
[ to>> ] [ from>> ] [ reg-class>> ] tri _copy ;
|
|
|
|
|
2009-06-26 22:48:21 -04:00
|
|
|
GENERIC: >collision-table ( operation -- )
|
|
|
|
|
|
|
|
M: memory->memory >collision-table
|
|
|
|
[ from>> ] [ to>> ] bi = [ "Not allowed" throw ] unless ;
|
|
|
|
|
|
|
|
M: register->memory >collision-table
|
|
|
|
[ from>> ] [ reg-class>> ] [ to>> ] tri _spill ;
|
|
|
|
|
|
|
|
M: memory->register >collision-table
|
|
|
|
[ to>> ] [ reg-class>> ] [ from>> ] tri _reload ;
|
|
|
|
|
|
|
|
M: register->register >collision-table
|
|
|
|
[ to>> ] [ from>> ] [ reg-class>> ] tri _copy ;
|
|
|
|
|
|
|
|
SYMBOL: froms
|
|
|
|
SYMBOL: tos
|
|
|
|
|
|
|
|
SINGLETONS: memory register ;
|
|
|
|
|
|
|
|
GENERIC: from-loc ( operation -- obj )
|
|
|
|
M: memory->memory from-loc drop memory ;
|
|
|
|
M: register->memory from-loc drop register ;
|
|
|
|
M: memory->register from-loc drop memory ;
|
|
|
|
M: register->register from-loc drop register ;
|
|
|
|
|
|
|
|
GENERIC: to-loc ( operation -- obj )
|
|
|
|
M: memory->memory to-loc drop memory ;
|
|
|
|
M: register->memory to-loc drop memory ;
|
|
|
|
M: memory->register to-loc drop register ;
|
|
|
|
M: register->register to-loc drop register ;
|
|
|
|
|
|
|
|
: from-reg ( operation -- seq )
|
|
|
|
[ from-loc ] [ from>> ] [ reg-class>> ] tri 3array ;
|
|
|
|
|
|
|
|
: to-reg ( operation -- seq )
|
|
|
|
[ to-loc ] [ to>> ] [ reg-class>> ] tri 3array ;
|
|
|
|
|
|
|
|
: start? ( operations -- pair )
|
|
|
|
from-reg tos get key? not ;
|
|
|
|
|
2009-06-28 17:43:17 -04:00
|
|
|
: independent-assignment? ( operations -- pair )
|
|
|
|
to-reg froms get key? not ;
|
|
|
|
|
2009-06-26 22:48:21 -04:00
|
|
|
: init-temp-spill ( operations -- )
|
|
|
|
[ [ to>> ] [ from>> ] bi max ] [ max ] map-reduce
|
|
|
|
1 + temp-spill set ;
|
|
|
|
|
|
|
|
: set-tos/froms ( operations -- )
|
|
|
|
{
|
2009-06-28 17:43:17 -04:00
|
|
|
[ [ [ from-reg ] keep ] H{ } map>assoc froms set ]
|
|
|
|
[ [ [ to-reg ] keep ] H{ } map>assoc tos set ]
|
2009-06-26 22:48:21 -04:00
|
|
|
} cleave ;
|
|
|
|
|
2009-06-28 17:43:17 -04:00
|
|
|
:: (trace-chain) ( obj hashtable -- )
|
|
|
|
obj to-reg froms get at* [
|
|
|
|
obj over hashtable clone [ maybe-set-at ] keep swap
|
|
|
|
[ (trace-chain) ] [ , drop ] if
|
|
|
|
] [
|
|
|
|
drop hashtable ,
|
|
|
|
] if ;
|
2009-06-26 22:48:21 -04:00
|
|
|
|
2009-06-28 17:43:17 -04:00
|
|
|
: trace-chain ( obj -- seq )
|
2009-06-26 22:48:21 -04:00
|
|
|
[
|
2009-06-28 17:43:17 -04:00
|
|
|
dup dup associate (trace-chain)
|
|
|
|
] { } make [ keys ] map concat reverse ;
|
|
|
|
|
|
|
|
: trace-chains ( seq -- seq' )
|
|
|
|
[ trace-chain ] map concat ;
|
|
|
|
|
|
|
|
: break-cycle-n ( operations -- operations' )
|
|
|
|
unclip [
|
2009-06-26 22:48:21 -04:00
|
|
|
[ from>> temp-spill get ]
|
|
|
|
[ reg-class>> ] bi \ register->memory boa
|
|
|
|
] [
|
|
|
|
[ to>> temp-spill [ get ] [ inc ] bi swap ]
|
|
|
|
[ reg-class>> ] bi \ memory->register boa
|
|
|
|
] bi [ 1array ] bi@ surround ;
|
|
|
|
|
|
|
|
: break-cycle ( operations -- operations' )
|
|
|
|
dup length {
|
2009-06-28 17:43:17 -04:00
|
|
|
{ 1 [ ] }
|
2009-06-26 22:48:21 -04:00
|
|
|
[ drop break-cycle-n ]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
: (group-cycles) ( seq -- )
|
|
|
|
[
|
2009-06-28 17:43:17 -04:00
|
|
|
dup set-tos/froms
|
|
|
|
unclip trace-chain
|
|
|
|
[ diff ] keep , (group-cycles)
|
2009-06-26 22:48:21 -04:00
|
|
|
] unless-empty ;
|
|
|
|
|
|
|
|
: group-cycles ( seq -- seqs )
|
|
|
|
[ (group-cycles) ] { } make ;
|
|
|
|
|
2009-06-28 17:43:17 -04:00
|
|
|
: remove-dead-mappings ( seq -- seq' )
|
|
|
|
prune [ [ from-reg ] [ to-reg ] bi = not ] filter ;
|
2009-06-26 22:48:21 -04:00
|
|
|
|
|
|
|
: parallel-mappings ( operations -- seq )
|
2009-06-28 17:43:17 -04:00
|
|
|
[
|
|
|
|
[ independent-assignment? not ] partition %
|
|
|
|
[ start? not ] partition
|
|
|
|
[ trace-chain ] map concat dup %
|
|
|
|
diff group-cycles [ break-cycle ] map concat %
|
|
|
|
] { } make remove-dead-mappings ;
|
2009-06-26 22:48:21 -04:00
|
|
|
|
2009-06-21 01:20:01 -04:00
|
|
|
: mapping-instructions ( mappings -- insns )
|
2009-06-26 22:48:21 -04:00
|
|
|
[
|
|
|
|
[ init-temp-spill ]
|
|
|
|
[ set-tos/froms ]
|
|
|
|
[ parallel-mappings ] tri
|
|
|
|
[ [ >insn ] each ] { } make
|
|
|
|
] with-scope ;
|
2009-06-21 01:20:01 -04:00
|
|
|
|
|
|
|
: fork? ( from to -- ? )
|
2009-06-28 17:43:17 -04:00
|
|
|
{
|
|
|
|
[ drop successors>> length 1 >= ]
|
|
|
|
[ nip predecessors>> length 1 = ]
|
|
|
|
} 2&& ; inline
|
2009-06-21 01:20:01 -04:00
|
|
|
|
|
|
|
: insert-position/fork ( from to -- before after )
|
|
|
|
nip instructions>> [ >array ] [ dup delete-all ] bi swap ;
|
|
|
|
|
|
|
|
: join? ( from to -- ? )
|
2009-06-28 17:43:17 -04:00
|
|
|
{
|
|
|
|
[ drop successors>> length 1 = ]
|
|
|
|
[ nip predecessors>> length 1 >= ]
|
|
|
|
} 2&& ; inline
|
2009-06-21 01:20:01 -04:00
|
|
|
|
|
|
|
: insert-position/join ( from to -- before after )
|
2009-06-22 01:24:51 -04:00
|
|
|
drop instructions>> dup pop 1array ;
|
2009-06-21 01:20:01 -04:00
|
|
|
|
|
|
|
: insert-position ( bb to -- before after )
|
|
|
|
{
|
|
|
|
{ [ 2dup fork? ] [ insert-position/fork ] }
|
|
|
|
{ [ 2dup join? ] [ insert-position/join ] }
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: 3append-here ( seq2 seq1 seq3 -- )
|
|
|
|
#! Mutate seq1
|
|
|
|
swap '[ _ push-all ] bi@ ;
|
|
|
|
|
|
|
|
: perform-mappings ( mappings bb to -- )
|
|
|
|
pick empty? [ 3drop ] [
|
|
|
|
[ mapping-instructions ] 2dip
|
|
|
|
insert-position 3append-here
|
|
|
|
] if ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
|
|
|
: resolve-edge-data-flow ( bb to -- )
|
2009-06-21 01:20:01 -04:00
|
|
|
[ compute-mappings ] [ perform-mappings ] 2bi ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
|
|
|
: resolve-block-data-flow ( bb -- )
|
2009-06-21 01:20:01 -04:00
|
|
|
dup successors>> [ resolve-edge-data-flow ] with each ;
|
2009-06-11 18:55:14 -04:00
|
|
|
|
|
|
|
: resolve-data-flow ( rpo -- )
|
2009-06-26 22:48:21 -04:00
|
|
|
[ resolve-block-data-flow ] each ;
|