1 ! Copyright (C) 2008 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 ;
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 : (remove-overflow-check?) ( #call -- ? )
68 node-output-infos first class>> fixnum class<= ;
70 : small-shift? ( #call -- ? )
71 node-input-infos second interval>>
72 cell-bits tag-bits get - [ neg ] keep [a,b] interval-subset? ;
74 : remove-overflow-check? ( #call -- ? )
76 { [ dup word>> \ fixnum-shift eq? ] [ [ (remove-overflow-check?) ] [ small-shift? ] bi and ] }
77 { [ dup word>> no-overflow-variant ] [ (remove-overflow-check?) ] }
81 : remove-overflow-check ( #call -- #call )
82 [ no-overflow-variant ] change-word cleanup* ;
86 { [ dup body>> ] [ cleanup-inlining ] }
87 { [ dup cleanup-folding? ] [ cleanup-folding ] }
88 { [ dup remove-overflow-check? ] [ remove-overflow-check ] }
92 : delete-unreachable-branches ( #branch -- )
93 dup live-branches>> '[
95 [ [ [ drop ] [ delete-nodes ] if ] 2each ]
98 ] change-children drop ;
100 : fold-only-branch ( #branch -- node/nodes )
101 #! If only one branch is live we don't need to branch at
102 #! all; just drop the condition value.
103 dup live-children sift dup length {
104 { 0 [ drop in-d>> #drop ] }
105 { 1 [ first swap in-d>> #drop prefix ] }
109 SYMBOL: live-branches
111 : cleanup-children ( #branch -- )
112 [ [ cleanup ] map ] change-children drop ;
116 [ delete-unreachable-branches ]
119 [ live-branches>> live-branches set ]
122 : output-fs ( values -- nodes )
123 [ f swap #push ] map ;
125 : eliminate-single-phi ( #phi -- node )
126 [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
127 [ [ drop ] [ output-fs ] bi* ]
131 : eliminate-phi ( #phi -- node )
132 live-branches get sift length {
133 { 0 [ out-d>> output-fs ] }
134 { 1 [ eliminate-single-phi ] }
139 #! Remove #phi function inputs which no longer exist.
141 [ '[ _ sift-children ] change-phi-in-d ]
142 [ '[ _ sift-children ] change-phi-info-d ]
143 [ '[ _ sift-children ] change-terminated ] tri
147 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
149 : flatten-recursive ( #recursive -- nodes )
150 #! convert #enter-recursive and #return-recursive into
154 unclip-last >copy suffix ;
156 M: #recursive cleanup*
157 #! Inline bodies of #recursive blocks with no calls left.
158 [ cleanup ] change-child
159 dup label>> calls>> empty? [ flatten-recursive ] when ;