1 ! Copyright (C) 2008 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators compiler.tree
4 compiler.tree.combinators
5 compiler.tree.escape-analysis.allocations
6 compiler.tree.escape-analysis.branches
7 compiler.tree.escape-analysis.nodes compiler.tree.recursive
8 disjoint-sets fry kernel namespaces sequences ;
9 IN: compiler.tree.escape-analysis.recursive
11 : congruent? ( alloc1 alloc2 -- ? )
13 { [ 2dup [ boolean? ] either? ] [ eq? ] }
14 { [ 2dup 2length @ = not ] [ 2drop f ] }
15 [ [ [ allocation ] bi@ congruent? ] 2all? ]
18 : check-fixed-point ( node alloc1 alloc2 -- )
19 [ congruent? ] 2all? [ drop ] [ label>> f >>fixed-point drop ] if ;
21 : node-input-allocations ( node -- allocations )
22 in-d>> [ allocation ] map ;
24 : node-output-allocations ( node -- allocations )
25 out-d>> [ allocation ] map ;
27 : recursive-stacks ( #enter-recursive -- stacks )
29 escaping-values get '[ [ _ disjoint-set-member? ] all? ] filter
32 : analyze-recursive-phi ( #enter-recursive -- )
33 [ ] [ recursive-stacks ] [ out-d>> ] tri
34 [ [ merge-values ] 2each ]
36 [ (merge-allocations) ] dip
37 [ [ allocation ] map check-fixed-point ]
38 [ record-allocations ]
42 M: #recursive escape-analysis* ( #recursive -- )
43 [ label>> return>> in-d>> introduce-values ]
47 [ first out-d>> introduce-values ]
48 [ first analyze-recursive-phi ]
54 M: #enter-recursive escape-analysis* ( #enter-recursive -- )
55 ! Handled by #recursive
58 M: #call-recursive escape-analysis* ( #call-label -- )
59 [ ] [ label>> return>> ] [ node-output-allocations ] tri
60 [ [ node-input-allocations ] dip check-fixed-point ]
61 [ drop swap [ in-d>> ] [ out-d>> ] bi* copy-values ]
64 M: #return-recursive escape-analysis* ( #return-recursive -- )
67 [ in-d>> ] [ label>> calls>> ] bi
68 [ node>> out-d>> escaping-values get '[ _ equate ] 2each ] with each