1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors sequences sequences.deep combinators fry
4 classes.algebra namespaces assocs words math math.private
5 math.partial-dispatch math.intervals classes classes.tuple
6 classes.tuple.private layouts definitions stack-checker.state
9 compiler.tree.combinators
10 compiler.tree.propagation.info
11 compiler.tree.propagation.branches ;
12 IN: compiler.tree.cleanup
14 ! A phase run after propagation to finish the job, so to speak.
15 ! Codifies speculative inlining decisions, deletes branches
16 ! marked as never taken, and flattens local recursive blocks
17 ! that do not call themselves.
19 GENERIC: delete-node ( node -- )
21 M: #call-recursive delete-node
22 dup label>> [ [ eq? not ] with filter ] change-calls drop ;
24 M: #return-recursive delete-node
25 label>> f >>return drop ;
27 M: node delete-node drop ;
29 : delete-nodes ( nodes -- ) [ delete-node ] each-node ;
31 GENERIC: cleanup* ( node -- node/nodes )
33 : cleanup ( nodes -- nodes' )
34 #! We don't recurse into children here, instead the methods
35 #! do it since the logic is a bit more involved
36 [ cleanup* ] map flatten ;
38 : cleanup-folding? ( #call -- ? )
40 [ f ] [ [ literal?>> ] all? ] if-empty ;
42 : cleanup-folding ( #call -- nodes )
43 #! Replace a #call having a known result with a #drop of its
44 #! inputs followed by #push nodes for the outputs.
45 [ word>> inlined-dependency depends-on ]
47 [ node-output-infos ] [ out-d>> ] bi
48 [ [ literal>> ] dip #push ] 2map
53 : add-method-dependency ( #call -- )
55 [ word>> ] [ class>> ] bi depends-on-generic
58 : cleanup-inlining ( #call -- nodes )
61 [ add-method-dependency ]
62 [ word>> inlined-dependency depends-on ] if
63 ] [ body>> cleanup ] bi ;
65 ! Removing overflow checks
66 : (remove-overflow-check?) ( #call -- ? )
67 node-output-infos first class>> fixnum class<= ;
69 : small-shift? ( #call -- ? )
70 node-input-infos second interval>>
71 cell-bits tag-bits get - [ neg ] keep [a,b] interval-subset? ;
73 : remove-overflow-check? ( #call -- ? )
75 { [ dup word>> \ fixnum-shift eq? ] [ [ (remove-overflow-check?) ] [ small-shift? ] bi and ] }
76 { [ dup word>> no-overflow-variant ] [ (remove-overflow-check?) ] }
80 : remove-overflow-check ( #call -- #call )
81 [ in-d>> ] [ out-d>> ] [ word>> no-overflow-variant ] tri #call cleanup* ;
85 { [ dup body>> ] [ cleanup-inlining ] }
86 { [ dup cleanup-folding? ] [ cleanup-folding ] }
87 { [ dup remove-overflow-check? ] [ remove-overflow-check ] }
91 M: #declare cleanup* drop f ;
93 : delete-unreachable-branches ( #branch -- )
94 dup live-branches>> '[
96 [ [ [ drop ] [ delete-nodes ] if ] 2each ]
99 ] change-children drop ;
101 : fold-only-branch ( #branch -- node/nodes )
102 #! If only one branch is live we don't need to branch at
103 #! all; just drop the condition value.
104 dup live-children sift dup length {
106 { 1 [ first swap in-d>> #drop prefix ] }
110 SYMBOL: live-branches
112 : cleanup-children ( #branch -- )
113 [ [ cleanup ] map ] change-children drop ;
117 [ delete-unreachable-branches ]
120 [ live-branches>> live-branches set ]
123 : output-fs ( values -- nodes )
124 [ f swap #push ] map ;
126 : eliminate-single-phi ( #phi -- node )
127 [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
128 [ [ drop ] [ output-fs ] bi* ]
132 : eliminate-phi ( #phi -- node )
133 live-branches get sift length {
134 { 0 [ out-d>> output-fs ] }
135 { 1 [ eliminate-single-phi ] }
140 #! Remove #phi function inputs which no longer exist.
142 [ '[ _ sift-children ] change-phi-in-d ]
143 [ '[ _ sift-children ] change-phi-info-d ]
144 [ '[ _ sift-children ] change-terminated ] tri
148 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
150 : flatten-recursive ( #recursive -- nodes )
151 #! convert #enter-recursive and #return-recursive into
155 unclip-last >copy suffix ;
157 M: #recursive cleanup*
158 #! Inline bodies of #recursive blocks with no calls left.
159 [ cleanup ] change-child
160 dup label>> calls>> empty? [ flatten-recursive ] when ;