]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/cleanup/cleanup.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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 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
7 stack-checker.branches
8 compiler.intrinsics
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>> [ [ eq? not ] with filter ] change-calls 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 flatten ;
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     [ in-d>> ] [ out-d>> ] [ word>> no-overflow-variant ] tri #call 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 M: #declare cleanup* drop f ;
93
94 : delete-unreachable-branches ( #branch -- )
95     dup live-branches>> '[
96         _
97         [ [ [ drop ] [ delete-nodes ] if ] 2each ]
98         [ select-children ]
99         2bi
100     ] change-children drop ;
101
102 : fold-only-branch ( #branch -- node/nodes )
103     #! If only one branch is live we don't need to branch at
104     #! all; just drop the condition value.
105     dup live-children sift dup length {
106         { 0 [ 2drop f ] }
107         { 1 [ first swap in-d>> #drop prefix ] }
108         [ 2drop ]
109     } case ;
110
111 SYMBOL: live-branches
112
113 : cleanup-children ( #branch -- )
114     [ [ cleanup ] map ] change-children drop ;
115
116 M: #branch cleanup*
117     {
118         [ delete-unreachable-branches ]
119         [ cleanup-children ]
120         [ fold-only-branch ]
121         [ live-branches>> live-branches set ]
122     } cleave ;
123
124 : output-fs ( values -- nodes )
125     [ f swap #push ] map ;
126
127 : eliminate-single-phi ( #phi -- node )
128     [ phi-in-d>> first ] [ out-d>> ] bi over [ +bottom+ eq? ] all?
129     [ [ drop ] [ output-fs ] bi* ]
130     [ #copy ]
131     if ;
132
133 : eliminate-phi ( #phi -- node )
134     live-branches get sift length {
135         { 0 [ out-d>> output-fs ] }
136         { 1 [ eliminate-single-phi ] }
137         [ drop ]
138     } case ;
139
140 M: #phi cleanup*
141     #! Remove #phi function inputs which no longer exist.
142     live-branches get
143     [ '[ _ sift-children ] change-phi-in-d ]
144     [ '[ _ sift-children ] change-phi-info-d ]
145     [ '[ _ sift-children ] change-terminated ] tri
146     eliminate-phi
147     live-branches off ;
148
149 : >copy ( node -- #copy ) [ in-d>> ] [ out-d>> ] bi #copy ;
150
151 : flatten-recursive ( #recursive -- nodes )
152     #! convert #enter-recursive and #return-recursive into
153     #! #copy nodes.
154     child>>
155     unclip >copy prefix
156     unclip-last >copy suffix ;
157
158 M: #recursive cleanup*
159     #! Inline bodies of #recursive blocks with no calls left.
160     [ cleanup ] change-child
161     dup label>> calls>> empty? [ flatten-recursive ] when ;
162
163 M: node cleanup* ;