1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
open! Stdlib
let times = Debug.find "times"
open Code
let iter_cont_free_vars f (_, l) = List.iter ~f l
let iter_expr_free_vars f e =
match e with
| Constant _ -> ()
| Apply { f = x; args; _ } ->
f x;
List.iter ~f args
| Block (_, a, _) -> Array.iter ~f a
| Field (x, _) -> f x
| Closure _ -> ()
| Special _ -> ()
| Prim (_, l) ->
List.iter l ~f:(fun x ->
match x with
| Pv x -> f x
| Pc _ -> ())
let iter_instr_free_vars f i =
match i with
| Let (_, e) -> iter_expr_free_vars f e
| Set_field (x, _, y) ->
f x;
f y
| Offset_ref (x, _) -> f x
| Array_set (x, y, z) ->
f x;
f y;
f z
| Assign (_, y) -> f y
let iter_last_free_var f l =
match l with
| Return x | Raise (x, _) -> f x
| Stop -> ()
| Branch cont | Poptrap cont -> iter_cont_free_vars f cont
| Cond (x, cont1, cont2) ->
f x;
iter_cont_free_vars f cont1;
iter_cont_free_vars f cont2
| Switch (x, a1) ->
f x;
Array.iter a1 ~f:(fun c -> iter_cont_free_vars f c)
| Pushtrap (cont1, _, cont2) ->
iter_cont_free_vars f cont1;
iter_cont_free_vars f cont2
let iter_block_free_vars f block =
List.iter block.body ~f:(fun (i, _) -> iter_instr_free_vars f i);
iter_last_free_var f (fst block.branch)
let iter_instr_bound_vars f i =
match i with
| Let (x, _) -> f x
| Set_field _ | Offset_ref _ | Array_set _ | Assign _ -> ()
let iter_last_bound_vars f l =
match l with
| Return _ | Raise _ | Stop | Branch _ | Cond _ | Switch _ | Poptrap _ -> ()
| Pushtrap (_, x, _) -> f x
let iter_block_bound_vars f block =
List.iter ~f block.params;
List.iter block.body ~f:(fun (i, _) -> iter_instr_bound_vars f i);
iter_last_bound_vars f (fst block.branch)
type st =
{ index : int
; mutable lowlink : int
; mutable in_stack : bool
; mutable revisited : bool
}
let find_loops p =
let in_loop = ref Addr.Map.empty in
let index = ref 0 in
let state = ref Addr.Map.empty in
let stack = Stack.create () in
let rec traverse pc =
let st = { index = !index; lowlink = !index; in_stack = true; revisited = false } in
state := Addr.Map.add pc st !state;
incr index;
Stack.push pc stack;
Code.fold_children
p.blocks
pc
(fun pc' () ->
try
let st' = Addr.Map.find pc' !state in
if st'.in_stack
then (
st'.revisited <- true;
st.lowlink <- min st.lowlink st'.index)
with Not_found ->
traverse pc';
let st' = Addr.Map.find pc' !state in
st.lowlink <- min st.lowlink st'.lowlink)
();
if st.index = st.lowlink
then (
let l = ref [] in
while
let pc' = Stack.pop stack in
l := pc' :: !l;
(Addr.Map.find pc' !state).in_stack <- false;
pc' <> pc
do
()
done;
if st.revisited
then List.iter !l ~f:(fun pc' -> in_loop := Addr.Map.add pc' pc !in_loop))
in
Code.fold_closures p (fun _ _ (pc, _) () -> traverse pc) ();
!in_loop
let mark_variables in_loop p =
let vars = Var.Tbl.make () (-1) in
let visited = BitSet.create' p.free_pc in
let rec traverse pc =
if not (BitSet.mem visited pc)
then (
BitSet.set visited pc;
let block = Addr.Map.find pc p.blocks in
(try
let pc' = Addr.Map.find pc in_loop in
iter_block_bound_vars (fun x -> Var.Tbl.set vars x pc') block
with Not_found -> ());
List.iter block.body ~f:(fun (i, _) ->
match i with
| Let (_, Closure (_, (pc', _))) -> traverse pc'
| _ -> ());
Code.fold_children p.blocks pc (fun pc' () -> traverse pc') ())
in
traverse p.start;
vars
let free_variables vars in_loop p =
let all_freevars = ref Addr.Map.empty in
let freevars = ref Addr.Map.empty in
let visited = BitSet.create' p.free_pc in
let rec traverse pc =
if not (BitSet.mem visited pc)
then (
BitSet.set visited pc;
let block = Addr.Map.find pc p.blocks in
iter_block_free_vars
(fun x ->
let pc' = Var.Tbl.get vars x in
if pc' <> -1
then
let fv =
try Addr.Map.find pc' !all_freevars with Not_found -> Var.Set.empty
in
let s = Var.Set.add x fv in
all_freevars := Addr.Map.add pc' s !all_freevars)
block;
(try
let pc'' = Addr.Map.find pc in_loop in
all_freevars := Addr.Map.remove pc'' !all_freevars
with Not_found -> ());
List.iter block.body ~f:(fun (i, _) ->
match i with
| Let (_, Closure (_, (pc', _))) -> (
traverse pc';
try
let pc'' = Addr.Map.find pc in_loop in
let fv =
try Addr.Map.find pc'' !all_freevars with Not_found -> Var.Set.empty
in
freevars := Addr.Map.add pc' fv !freevars;
all_freevars := Addr.Map.remove pc'' !all_freevars
with Not_found -> freevars := Addr.Map.add pc' Var.Set.empty !freevars)
| _ -> ());
Code.fold_children p.blocks pc (fun pc' () -> traverse pc') ())
in
traverse p.start;
!freevars
let f p =
Code.invariant p;
let t = Timer.make () in
let bound = Code.Var.ISet.empty () in
let visited = BitSet.create' p.free_pc in
let free_vars =
Code.fold_closures_innermost_first
p
(fun _name_opt params (pc, args) acc ->
let free = ref Var.Set.empty in
let using x =
if Code.Var.ISet.mem bound x then () else free := Var.Set.add x !free
in
let rec traverse pc =
if not (BitSet.mem visited pc)
then (
BitSet.set visited pc;
let block = Addr.Map.find pc p.blocks in
iter_block_bound_vars (fun x -> Code.Var.ISet.add bound x) block;
iter_block_free_vars using block;
List.iter block.body ~f:(function
| Let (_, Closure (_, (pc_clo, _))), _ ->
Code.Var.Set.iter using (Code.Addr.Map.find pc_clo acc)
| _ -> ());
Code.fold_children p.blocks pc (fun pc' () -> traverse pc') ())
in
List.iter params ~f:(fun x -> Code.Var.ISet.add bound x);
List.iter args ~f:using;
traverse pc;
Code.Addr.Map.add pc !free acc)
Code.Addr.Map.empty
in
if times () then Format.eprintf " free vars 2: %a@." Timer.print t;
free_vars
let f_mutable p =
Code.invariant p;
let t = Timer.make () in
let in_loop = find_loops p in
let vars = mark_variables in_loop p in
let free_vars = free_variables vars in_loop p in
if times () then Format.eprintf " free vars 1: %a@." Timer.print t;
free_vars