@@ -30,8 +30,7 @@ type closure_info =
3030 { cl_params : Var .t list
3131 ; cl_cont : int * Var .t list
3232 ; cl_prop : prop
33- ; cl_simpl : (Var.Set .t * int Var.Map .t * bool * Var.Set .t ) option
34- ; cl_loc : Parse_info .t option
33+ ; cl_simpl : (int Var.Map .t * Var.Set .t ) option
3534 }
3635
3736let block_size { branch; body; _ } =
@@ -98,7 +97,8 @@ let simple_function blocks size name params pc =
9897 pc
9998 blocks
10099 () ;
101- Some (! bound_vars, ! free_vars, Var.Map. mem name ! free_vars, ! tc)
100+ if Var.Map. mem name ! free_vars then raise Exit ;
101+ Some (! free_vars, ! tc)
102102 with Exit -> None
103103
104104(* ***)
@@ -133,14 +133,14 @@ let get_closures { blocks; _ } =
133133 (fun _ block closures ->
134134 List. fold_left block.body ~init: closures ~f: (fun closures i ->
135135 match i with
136- | Let (x , Closure (cl_params , cl_cont , cl_loc )) ->
136+ | Let (x , Closure (cl_params , cl_cont , _ )) ->
137137 (* we can compute this once during the pass
138138 as the property won't change with inlining *)
139139 let cl_prop = optimizable blocks (fst cl_cont) in
140140 let cl_simpl =
141141 simple_function blocks cl_prop.size x cl_params (fst cl_cont)
142142 in
143- Var.Map. add x { cl_params; cl_cont; cl_prop; cl_simpl; cl_loc } closures
143+ Var.Map. add x { cl_params; cl_cont; cl_prop; cl_simpl } closures
144144 | _ -> closures))
145145 blocks
146146 Var.Map. empty
@@ -180,7 +180,6 @@ let inline inline_count live_vars closures pc (outer, p) =
180180 ; cl_cont = clos_cont
181181 ; cl_prop = { size = f_size; optimizable = f_optimizable }
182182 ; cl_simpl
183- ; cl_loc
184183 } =
185184 Var.Map. find f closures
186185 in
@@ -230,19 +229,18 @@ let inline inline_count live_vars closures pc (outer, p) =
230229 [] , (outer, Branch (fresh_addr, args), { p with blocks; free_pc }))
231230 else
232231 match cl_simpl with
233- | Some (_, free_vars, recursive , tc_params)
232+ | Some (free_vars, tc_params)
234233 (* We inline/duplicate
235234 - single instruction functions (f_size = 1)
236235 - small funtions that call one of their arguments in
237236 tail position when the argument is a direct closure
238237 used only once. *)
239- when (Code.Var.Set. exists
240- (fun x ->
241- let farg_tc = Var.Map. find x map_param_to_arg in
242- Var.Map. mem farg_tc closures && live_vars.(Var. idx farg_tc) = 1 )
243- tc_params
244- || f_size < = 1 )
245- && not recursive ->
238+ when Code.Var.Set. exists
239+ (fun x ->
240+ let farg_tc = Var.Map. find x map_param_to_arg in
241+ Var.Map. mem farg_tc closures && live_vars.(Var. idx farg_tc) = 1 )
242+ tc_params
243+ || f_size < = 1 ->
246244 let () =
247245 (* Update live_vars *)
248246 Var.Map. iter
@@ -254,46 +252,39 @@ let inline inline_count live_vars closures pc (outer, p) =
254252 free_vars;
255253 live_vars.(Var. idx f) < - live_vars.(Var. idx f) - 1
256254 in
257- let p, f , params, clos_cont =
255+ let p, _f , params, clos_cont =
258256 Duplicate. closure p ~f ~params ~cont: clos_cont
259257 in
260- if recursive
261- then
262- ( Let (f, Closure (params, clos_cont, cl_loc))
263- :: Let (x, Apply { f; args; exact = true })
264- :: rem
265- , (outer, branch, p) )
266- else
267- let blocks, cont_pc, free_pc =
268- match rem, branch with
269- | [] , Return y when Var. compare x y = 0 ->
270- (* We do not need a continuation block for tail calls *)
271- p.blocks, None , p.free_pc
272- | _ ->
273- let fresh_addr = p.free_pc in
274- let free_pc = fresh_addr + 1 in
275- ( Addr.Map. add
276- fresh_addr
277- { params = [ x ]; body = rem; branch }
278- p.blocks
279- , Some fresh_addr
280- , free_pc )
281- in
282- let blocks = rewrite_closure blocks cont_pc (fst clos_cont) in
283- (* We do not really need this intermediate block.
258+ let blocks, cont_pc, free_pc =
259+ match rem, branch with
260+ | [] , Return y when Var. compare x y = 0 ->
261+ (* We do not need a continuation block for tail calls *)
262+ p.blocks, None , p.free_pc
263+ | _ ->
264+ let fresh_addr = p.free_pc in
265+ let free_pc = fresh_addr + 1 in
266+ ( Addr.Map. add
267+ fresh_addr
268+ { params = [ x ]; body = rem; branch }
269+ p.blocks
270+ , Some fresh_addr
271+ , free_pc )
272+ in
273+ let blocks = rewrite_closure blocks cont_pc (fst clos_cont) in
274+ (* We do not really need this intermediate block.
284275 It just avoids the need to find which function
285276 parameters are used in the function body. *)
286- let fresh_addr = free_pc in
287- let free_pc = fresh_addr + 1 in
288- let blocks =
289- Addr.Map. add
290- fresh_addr
291- { params; body = [] ; branch = Branch clos_cont }
292- blocks
293- in
294- let outer = { outer with size = outer.size + f_size } in
295- incr inline_count;
296- [] , (outer, Branch (fresh_addr, args), { p with blocks; free_pc })
277+ let fresh_addr = free_pc in
278+ let free_pc = fresh_addr + 1 in
279+ let blocks =
280+ Addr.Map. add
281+ fresh_addr
282+ { params; body = [] ; branch = Branch clos_cont }
283+ blocks
284+ in
285+ let outer = { outer with size = outer.size + f_size } in
286+ incr inline_count;
287+ [] , (outer, Branch (fresh_addr, args), { p with blocks; free_pc })
297288 | _ -> i :: rem, state)
298289 | _ -> i :: rem, state)
299290 in
0 commit comments