]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/cleanup/cleanup.factor
Add some utility words to stack-checker.dependencies in preparation for a refactoring
[factor.git] / basis / compiler / tree / cleanup / cleanup.factor
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
7 stack-checker.branches
8 compiler.utilities
9 compiler.tree
10 compiler.tree.combinators
11 compiler.tree.propagation.info
12 compiler.tree.propagation.branches ;
13 IN: compiler.tree.cleanup
14
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.
19
20 GENERIC: delete-node ( node -- )
21
22 M: #call-recursive delete-node
23     dup label>> calls>> [ node>> eq? not ] with filter! drop ;
24
25 M: #return-recursive delete-node
26     label>> f >>return drop ;
27
28 M: node delete-node drop ;
29
30 : delete-nodes ( nodes -- ) [ delete-node ] each-node ;
31
32 GENERIC: cleanup* ( node -- node/nodes )
33
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 ;
38
39 ! Constant folding
40 : cleanup-folding? ( #call -- ? )
41     node-output-infos
42     [ f ] [ [ literal?>> ] all? ] if-empty ;
43
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.
47     [
48         [ node-output-infos ] [ out-d>> ] bi
49         [ [ literal>> ] dip #push ] 2map
50     ]
51     [ in-d>> #drop ]
52     bi prefix ;
53
54 : record-predicate-folding ( #call -- )
55     [ node-input-infos first class>> ]
56     [ word>> "predicating" word-prop ]
57     [ node-output-infos first literal>> ] tri
58     [ depends-on-class<= ] [ depends-on-classes-disjoint ] if ;
59
60 : record-folding ( #call -- )
61     dup word>> predicate?
62     [ record-predicate-folding ]
63     [ word>> depends-on-definition ]
64     if ;
65
66 : cleanup-folding ( #call -- nodes )
67     [ (cleanup-folding) ] [ record-folding ] bi ;
68
69 ! Method inlining
70 : add-method-dependency ( #call -- )
71     dup method>> word? [
72         [ [ class>> ] [ word>> ] bi depends-on-generic ]
73         [ [ class>> ] [ word>> ] [ method>> ] tri depends-on-method ]
74         bi
75     ] [ drop ] if ;
76
77 : record-inlining ( #call -- )
78     dup method>>
79     [ add-method-dependency ]
80     [ word>> depends-on-definition ] if ;
81
82 : cleanup-inlining ( #call -- nodes )
83     [ record-inlining ] [ body>> cleanup ] bi ;
84
85 ! Removing overflow checks
86 : (remove-overflow-check?) ( #call -- ? )
87     node-output-infos first class>> fixnum class<= ;
88
89 : small-shift? ( #call -- ? )
90     node-input-infos second interval>>
91     cell-bits tag-bits get - [ neg ] keep [a,b] interval-subset? ;
92
93 : remove-overflow-check? ( #call -- ? )
94     {
95         { [ dup word>> \ fixnum-shift eq? ] [ [ (remove-overflow-check?) ] [ small-shift? ] bi and ] }
96         { [ dup word>> no-overflow-variant ] [ (remove-overflow-check?) ] }
97         [ drop f ]
98     } cond ;
99
100 : remove-overflow-check ( #call -- #call )
101     [ no-overflow-variant ] change-word cleanup* ;
102
103 M: #call cleanup*
104     {
105         { [ dup body>> ] [ cleanup-inlining ] }
106         { [ dup cleanup-folding? ] [ cleanup-folding ] }
107         { [ dup remove-overflow-check? ] [ remove-overflow-check ] }
108         [ ]
109     } cond ;
110
111 : delete-unreachable-branches ( #branch -- )
112     dup live-branches>> '[
113         _
114         [ [ [ drop ] [ delete-nodes ] if ] 2each ]
115         [ select-children ]
116         2bi
117     ] change-children drop ;
118
119 : fold-only-branch ( #branch -- node/nodes )
120     #! If only one branch is live we don't need to branch at
121     #! all; just drop the condition value.
122     dup live-children sift dup length {
123         { 0 [ drop in-d>> #drop ] }
124         { 1 [ first swap in-d>> #drop prefix ] }
125         [ 2drop ]
126     } case ;
127
128 SYMBOL: live-branches
129
130 : cleanup-children ( #branch -- )
131     [ [ cleanup ] map ] change-children drop ;
132
133 M: #branch cleanup*
134     {
135         [ delete-unreachable-branches ]
136         [ cleanup-children ]
137         [ fold-only-branch ]
138         [ live-branches>> live-branches set ]
139     } cleave ;
140
141 : output-fs ( values -- nodes )
142     [ f swap #push ] map ;
143
144 : eliminate-single-phi ( #phi -- node )
145     [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
146     [ [ drop ] [ output-fs ] bi* ]
147     [ #copy ]
148     if ;
149
150 : eliminate-phi ( #phi -- node )
151     live-branches get sift length {
152         { 0 [ out-d>> output-fs ] }
153         { 1 [ eliminate-single-phi ] }
154         [ drop ]
155     } case ;
156
157 M: #phi cleanup*
158     #! Remove #phi function inputs which no longer exist.
159     live-branches get
160     [ '[ _ sift-children ] change-phi-in-d ]
161     [ '[ _ sift-children ] change-phi-info-d ]
162     [ '[ _ sift-children ] change-terminated ] tri
163     eliminate-phi
164     live-branches off ;
165
166 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
167
168 : flatten-recursive ( #recursive -- nodes )
169     #! convert #enter-recursive and #return-recursive into
170     #! #copy nodes.
171     child>>
172     unclip >copy prefix
173     unclip-last >copy suffix ;
174
175 M: #recursive cleanup*
176     #! Inline bodies of #recursive blocks with no calls left.
177     [ cleanup ] change-child
178     dup label>> calls>> empty? [ flatten-recursive ] when ;
179
180 M: node cleanup* ;