]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/parallel-copy/parallel-copy.factor
starting point - Fix bootstrap; I broke it in merging. Disable environment variables...
[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 cpu.architecture compiler.cfg.registers
4 compiler.cfg.instructions deques dlists fry kernel locals namespaces
5 sequences hashtables ;
6 IN: compiler.cfg.parallel-copy
7
8 ! Revisiting Out-of-SSA Translation for Correctness, Code Quality, and Efficiency
9 ! http://hal.archives-ouvertes.fr/docs/00/34/99/25/PDF/OutSSA-RR.pdf,
10 ! Algorithm 1
11
12 <PRIVATE
13
14 SYMBOLS: locs preds to-do ready ;
15
16 : init-to-do ( bs -- )
17     to-do get push-all-back ;
18
19 : init-ready ( bs -- )
20     locs get '[ _ key? not ] filter ready get push-all-front ;
21
22 : init ( mapping -- )
23     <dlist> to-do set
24     <dlist> ready set
25     [ preds set ]
26     [ [ nip dup ] H{ } assoc-map-as locs set ]
27     [ keys [ init-to-do ] [ init-ready ] bi ] tri ;
28
29 :: process-ready ( b quot: ( dst src -- ) -- )
30     b preds get at :> a
31     a locs get at :> c
32     b c quot call
33     b a locs get set-at
34     a c = a preds get at and [ a ready get push-front ] when ; inline
35
36 :: process-to-do ( b temp: ( src -- dst ) quot: ( dst src -- ) -- )
37     ! Note that we check if b = loc(b), not b = loc(pred(b)) as the
38     ! paper suggests. Confirmed by one of the authors at
39     ! http://www.reddit.com/comments/93253/some_lecture_notes_on_ssa_form/c0bco4f
40     b locs get at b = [
41         b temp call :> temp
42         temp b quot call
43         temp b locs get set-at
44         b ready get push-front
45     ] when ; inline
46
47 PRIVATE>
48
49 :: parallel-mapping ( mapping temp: ( src -- dst ) quot: ( dst src -- ) -- )
50     ! mapping is a list of { dst src } pairs
51     [
52         mapping init
53         to-do get [
54             ready get [
55                 quot process-ready
56             ] slurp-deque
57             temp quot process-to-do
58         ] slurp-deque
59     ] with-scope ; inline
60
61 : parallel-copy ( mapping -- )
62     ! mapping is a list of { dst src } pairs
63     next-vreg '[ drop _ ] [ any-rep ##copy, ] parallel-mapping ;
64
65 <PRIVATE
66
67 SYMBOL: temp-vregs
68
69 : temp-vreg ( rep -- vreg )
70     temp-vregs get [ next-vreg-rep ] cache ;
71
72 PRIVATE>
73
74 : parallel-copy-rep ( mapping -- )
75     ! mapping is a list of { dst src } pairs
76     H{ } clone temp-vregs set
77     [ rep-of temp-vreg ] [ dup rep-of ##copy, ] parallel-mapping ;