]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/cleanup/cleanup.factor
Fix conflict
[factor.git] / basis / compiler / tree / cleanup / cleanup.factor
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
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 : cleanup-folding? ( #call -- ? )
40     node-output-infos
41     [ f ] [ [ literal?>> ] all? ] if-empty ;
42
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 ]
47     [
48         [ node-output-infos ] [ out-d>> ] bi
49         [ [ literal>> ] dip #push ] 2map
50     ]
51     [ in-d>> #drop ]
52     tri prefix ;
53
54 : add-method-dependency ( #call -- )
55     dup method>> word? [
56         [ word>> ] [ class>> ] bi depends-on-generic
57     ] [ drop ] if ;
58
59 : cleanup-inlining ( #call -- nodes )
60     [
61         dup method>>
62         [ add-method-dependency ]
63         [ word>> inlined-dependency depends-on ] if
64     ] [ body>> cleanup ] bi ;
65
66 ! Removing overflow checks
67 : (remove-overflow-check?) ( #call -- ? )
68     node-output-infos first class>> fixnum class<= ;
69
70 : small-shift? ( #call -- ? )
71     node-input-infos second interval>>
72     cell-bits tag-bits get - [ neg ] keep [a,b] interval-subset? ;
73
74 : remove-overflow-check? ( #call -- ? )
75     {
76         { [ dup word>> \ fixnum-shift eq? ] [ [ (remove-overflow-check?) ] [ small-shift? ] bi and ] }
77         { [ dup word>> no-overflow-variant ] [ (remove-overflow-check?) ] }
78         [ drop f ]
79     } cond ;
80
81 : remove-overflow-check ( #call -- #call )
82     [ no-overflow-variant ] change-word cleanup* ;
83
84 M: #call cleanup*
85     {
86         { [ dup body>> ] [ cleanup-inlining ] }
87         { [ dup cleanup-folding? ] [ cleanup-folding ] }
88         { [ dup remove-overflow-check? ] [ remove-overflow-check ] }
89         [ ]
90     } cond ;
91
92 : delete-unreachable-branches ( #branch -- )
93     dup live-branches>> '[
94         _
95         [ [ [ drop ] [ delete-nodes ] if ] 2each ]
96         [ select-children ]
97         2bi
98     ] change-children drop ;
99
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 ] }
106         [ 2drop ]
107     } case ;
108
109 SYMBOL: live-branches
110
111 : cleanup-children ( #branch -- )
112     [ [ cleanup ] map ] change-children drop ;
113
114 M: #branch cleanup*
115     {
116         [ delete-unreachable-branches ]
117         [ cleanup-children ]
118         [ fold-only-branch ]
119         [ live-branches>> live-branches set ]
120     } cleave ;
121
122 : output-fs ( values -- nodes )
123     [ f swap #push ] map ;
124
125 : eliminate-single-phi ( #phi -- node )
126     [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
127     [ [ drop ] [ output-fs ] bi* ]
128     [ #copy ]
129     if ;
130
131 : eliminate-phi ( #phi -- node )
132     live-branches get sift length {
133         { 0 [ out-d>> output-fs ] }
134         { 1 [ eliminate-single-phi ] }
135         [ drop ]
136     } case ;
137
138 M: #phi cleanup*
139     #! Remove #phi function inputs which no longer exist.
140     live-branches get
141     [ '[ _ sift-children ] change-phi-in-d ]
142     [ '[ _ sift-children ] change-phi-info-d ]
143     [ '[ _ sift-children ] change-terminated ] tri
144     eliminate-phi
145     live-branches off ;
146
147 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
148
149 : flatten-recursive ( #recursive -- nodes )
150     #! convert #enter-recursive and #return-recursive into
151     #! #copy nodes.
152     child>>
153     unclip >copy prefix
154     unclip-last >copy suffix ;
155
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 ;
160
161 M: node cleanup* ;