1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors sequences 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.dependencies
10 compiler.tree.combinators
11 compiler.tree.propagation.info
12 compiler.tree.propagation.branches ;
13 IN: compiler.tree.cleanup
15 ! A phase run after propagation to finish the job, so to speak.
16 ! Codifies speculative inlining decisions, deletes branches
17 ! marked as never taken, and flattens local recursive blocks
18 ! that do not call themselves.
20 GENERIC: delete-node ( node -- )
22 M: #call-recursive delete-node
23 dup label>> calls>> [ node>> eq? not ] with filter! drop ;
25 M: #return-recursive delete-node
26 label>> f >>return drop ;
28 M: node delete-node drop ;
30 : delete-nodes ( nodes -- ) [ delete-node ] each-node ;
32 GENERIC: cleanup* ( node -- node/nodes )
34 : cleanup ( nodes -- nodes' )
35 #! We don't recurse into children here, instead the methods
36 #! do it since the logic is a bit more involved
37 [ cleanup* ] map-flat ;
40 : cleanup-folding? ( #call -- ? )
42 [ f ] [ [ literal?>> ] all? ] if-empty ;
44 : (cleanup-folding) ( #call -- nodes )
45 #! Replace a #call having a known result with a #drop of its
46 #! inputs followed by #push nodes for the outputs.
48 [ node-output-infos ] [ out-d>> ] bi
49 [ [ literal>> ] dip #push ] 2map
54 : >predicate-folding< ( #call -- value-info class result )
55 [ node-input-infos first ]
56 [ word>> "predicating" word-prop ]
57 [ node-output-infos first literal>> ] tri ;
59 : record-predicate-folding ( #call -- )
60 >predicate-folding< pick literal?>>
61 [ [ literal>> ] 2dip depends-on-instance-predicate ]
62 [ [ class>> ] 2dip depends-on-class-predicate ]
65 : record-folding ( #call -- )
67 [ record-predicate-folding ]
68 [ word>> depends-on-definition ]
71 : cleanup-folding ( #call -- nodes )
72 [ (cleanup-folding) ] [ record-folding ] bi ;
75 : add-method-dependency ( #call -- )
77 [ [ class>> ] [ word>> ] bi depends-on-generic ]
78 [ [ class>> ] [ word>> ] [ method>> ] tri depends-on-method ]
82 : record-inlining ( #call -- )
84 [ add-method-dependency ]
85 [ word>> depends-on-definition ] if ;
87 : cleanup-inlining ( #call -- nodes )
88 [ record-inlining ] [ body>> cleanup ] bi ;
90 ! Removing overflow checks
91 : (remove-overflow-check?) ( #call -- ? )
92 node-output-infos first class>> fixnum class<= ;
94 : small-shift? ( #call -- ? )
95 node-input-infos second interval>>
96 cell-bits tag-bits get - [ neg ] keep [a,b] interval-subset? ;
98 : remove-overflow-check? ( #call -- ? )
100 { [ dup word>> \ fixnum-shift eq? ] [ [ (remove-overflow-check?) ] [ small-shift? ] bi and ] }
101 { [ dup word>> no-overflow-variant ] [ (remove-overflow-check?) ] }
105 : remove-overflow-check ( #call -- #call )
106 [ no-overflow-variant ] change-word cleanup* ;
110 { [ dup body>> ] [ cleanup-inlining ] }
111 { [ dup cleanup-folding? ] [ cleanup-folding ] }
112 { [ dup remove-overflow-check? ] [ remove-overflow-check ] }
116 : delete-unreachable-branches ( #branch -- )
117 dup live-branches>> '[
119 [ [ [ drop ] [ delete-nodes ] if ] 2each ]
122 ] change-children drop ;
124 : fold-only-branch ( #branch -- node/nodes )
125 #! If only one branch is live we don't need to branch at
126 #! all; just drop the condition value.
127 dup live-children sift dup length {
128 { 0 [ drop in-d>> #drop ] }
129 { 1 [ first swap in-d>> #drop prefix ] }
133 SYMBOL: live-branches
135 : cleanup-children ( #branch -- )
136 [ [ cleanup ] map ] change-children drop ;
140 [ delete-unreachable-branches ]
143 [ live-branches>> live-branches set ]
146 : output-fs ( values -- nodes )
147 [ f swap #push ] map ;
149 : eliminate-single-phi ( #phi -- node )
150 [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
151 [ [ drop ] [ output-fs ] bi* ]
155 : eliminate-phi ( #phi -- node )
156 live-branches get sift length {
157 { 0 [ out-d>> output-fs ] }
158 { 1 [ eliminate-single-phi ] }
163 #! Remove #phi function inputs which no longer exist.
165 [ '[ _ sift-children ] change-phi-in-d ]
166 [ '[ _ sift-children ] change-phi-info-d ]
167 [ '[ _ sift-children ] change-terminated ] tri
171 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
173 : flatten-recursive ( #recursive -- nodes )
174 #! convert #enter-recursive and #return-recursive into
178 unclip-last >copy suffix ;
180 M: #recursive cleanup*
181 #! Inline bodies of #recursive blocks with no calls left.
182 [ cleanup ] change-child
183 dup label>> calls>> empty? [ flatten-recursive ] when ;