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
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>> [ [ eq? not ] with filter ] change-calls 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 flatten ;
39 : cleanup-folding? ( #call -- ? )
41 [ f ] [ [ literal?>> ] all? ] if-empty ;
43 : cleanup-folding ( #call -- nodes )
44 #! Replace a #call having a known result with a #drop of its
45 #! inputs followed by #push nodes for the outputs.
46 [ word>> inlined-dependency depends-on ]
48 [ node-output-infos ] [ out-d>> ] bi
49 [ [ literal>> ] dip #push ] 2map
54 : add-method-dependency ( #call -- )
56 [ word>> ] [ class>> ] bi depends-on-generic
59 : cleanup-inlining ( #call -- nodes )
62 [ add-method-dependency ]
63 [ word>> inlined-dependency depends-on ] if
64 ] [ body>> cleanup ] bi ;
66 ! Removing overflow checks
67 : no-overflow-variant ( op -- fast-op )
69 { fixnum+ fixnum+fast }
70 { fixnum- fixnum-fast }
71 { fixnum* fixnum*fast }
72 { fixnum-shift fixnum-shift-fast }
75 : (remove-overflow-check?) ( #call -- ? )
76 node-output-infos first class>> fixnum class<= ;
78 : small-shift? ( #call -- ? )
79 node-input-infos second interval>>
80 cell-bits tag-bits get - [ neg ] keep [a,b] interval-subset? ;
82 : remove-overflow-check? ( #call -- ? )
84 { [ dup word>> \ fixnum-shift eq? ] [ [ (remove-overflow-check?) ] [ small-shift? ] bi and ] }
85 { [ dup word>> no-overflow-variant ] [ (remove-overflow-check?) ] }
89 : remove-overflow-check ( #call -- #call )
90 [ in-d>> ] [ out-d>> ] [ word>> no-overflow-variant ] tri #call cleanup* ;
94 { [ dup body>> ] [ cleanup-inlining ] }
95 { [ dup cleanup-folding? ] [ cleanup-folding ] }
96 { [ dup remove-overflow-check? ] [ remove-overflow-check ] }
100 M: #declare cleanup* drop f ;
102 : delete-unreachable-branches ( #branch -- )
103 dup live-branches>> '[
105 [ [ [ drop ] [ delete-nodes ] if ] 2each ]
108 ] change-children drop ;
110 : fold-only-branch ( #branch -- node/nodes )
111 #! If only one branch is live we don't need to branch at
112 #! all; just drop the condition value.
113 dup live-children sift dup length {
115 { 1 [ first swap in-d>> #drop prefix ] }
119 SYMBOL: live-branches
121 : cleanup-children ( #branch -- )
122 [ [ cleanup ] map ] change-children drop ;
126 [ delete-unreachable-branches ]
129 [ live-branches>> live-branches set ]
132 : output-fs ( values -- nodes )
133 [ f swap #push ] map ;
135 : eliminate-single-phi ( #phi -- node )
136 [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
137 [ [ drop ] [ output-fs ] bi* ]
141 : eliminate-phi ( #phi -- node )
142 live-branches get sift length {
143 { 0 [ out-d>> output-fs ] }
144 { 1 [ eliminate-single-phi ] }
149 #! Remove #phi function inputs which no longer exist.
151 [ '[ _ sift-children ] change-phi-in-d ]
152 [ '[ _ sift-children ] change-phi-info-d ]
153 [ '[ _ sift-children ] change-terminated ] tri
157 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
159 : flatten-recursive ( #recursive -- nodes )
160 #! convert #enter-recursive and #return-recursive into
164 unclip-last >copy suffix ;
166 M: #recursive cleanup*
167 #! Inline bodies of #recursive blocks with no calls left.
168 [ cleanup ] change-child
169 dup label>> calls>> empty? [ flatten-recursive ] when ;