]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/parallel-copy/parallel-copy.factor
d09da24d6de27355d4c6caff278e6a4ede258a7a
[factor.git] / basis / compiler / cfg / parallel-copy / parallel-copy.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs compiler.cfg.instructions compiler.cfg.registers
4 compiler.cfg.ssa.destruction.leaders cpu.architecture deques
5 dlists fry kernel locals make namespaces sequences ;
6 FROM: sets => conjoin ;
7 IN: compiler.cfg.parallel-copy
8
9 <PRIVATE
10
11 SYMBOLS: locs preds to-do ready ;
12
13 : init-to-do ( bs -- )
14     to-do get push-all-back ;
15
16 : init-ready ( bs -- )
17     locs get '[ _ key? not ] filter ready get push-all-front ;
18
19 : init ( mapping -- )
20     <dlist> to-do set
21     <dlist> ready set
22     [ preds set ]
23     [ [ nip dup ] H{ } assoc-map-as locs set ]
24     [ keys [ init-to-do ] [ init-ready ] bi ] tri ;
25
26 :: process-ready ( b quot: ( dst src -- ) -- )
27     b preds get at :> a
28     a locs get at :> c
29     b c quot call
30     b a locs get set-at
31     a c = a preds get at and [ a ready get push-front ] when ; inline
32
33 :: process-to-do ( b temp: ( src -- dst ) quot: ( dst src -- ) -- )
34     b locs get at b = [
35         b temp call :> temp
36         temp b quot call
37         temp b locs get set-at
38         b ready get push-front
39     ] when ; inline
40
41 PRIVATE>
42
43 :: parallel-mapping ( mapping temp: ( src -- dst ) quot: ( dst src -- ) -- )
44     [
45         mapping init
46         to-do get [
47             ready get [
48                 quot process-ready
49             ] slurp-deque
50             temp quot process-to-do
51         ] slurp-deque
52     ] with-scope ; inline
53
54 : parallel-copy ( mapping -- insns )
55     [ next-vreg '[ drop _ ] [ any-rep ##copy, ] parallel-mapping ] { } make ;
56
57 <PRIVATE
58
59 SYMBOL: temp-vregs
60
61 : temp-vreg ( rep -- vreg )
62     temp-vregs get [ next-vreg-rep ] cache
63     [ leader-map get conjoin ] keep ;
64
65 PRIVATE>
66
67 : parallel-copy-rep ( mapping -- insns )
68     [
69         H{ } clone temp-vregs set
70         [ rep-of temp-vreg ] [ dup rep-of ##copy, ] parallel-mapping
71     ] { } make ;