Skip to content

Commit 0589e90

Browse files
committed
Don't leave unused blocks after Eval.f and Deadcode.remove_empty_blocks
1 parent 4bafb2e commit 0589e90

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

compiler/lib/deadcode.ml

+21-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ let annot st pc xi =
183183

184184
(****)
185185

186+
let remove_unused_blocks p =
187+
let visited = BitSet.create' p.free_pc in
188+
let rec mark_used pc =
189+
if not (BitSet.mem visited pc)
190+
then (
191+
BitSet.set visited pc;
192+
let block = Addr.Map.find pc p.blocks in
193+
List.iter
194+
~f:(fun i ->
195+
match i with
196+
| Let (_, Closure (_, (pc', _), _)) -> mark_used pc'
197+
| _ -> ())
198+
block.body;
199+
Code.fold_children p.blocks pc (fun pc' () -> mark_used pc') ())
200+
in
201+
mark_used p.start;
202+
{ p with blocks = Addr.Map.filter (fun pc _ -> BitSet.mem visited pc) p.blocks }
203+
204+
(****)
205+
186206
let rec add_arg_dep defs params args =
187207
match params, args with
188208
| x :: params, y :: args ->
@@ -250,7 +270,7 @@ let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
250270
})
251271
p.blocks
252272
in
253-
{ p with blocks }
273+
remove_unused_blocks { p with blocks }
254274

255275
let f ({ blocks; _ } as p : Code.program) =
256276
let t = Timer.make () in

compiler/lib/deadcode.mli

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ type variable_uses =
2424
val f : Code.program -> Code.program * variable_uses
2525

2626
val remove_empty_blocks : live_vars:variable_uses -> Code.program -> Code.program
27+
28+
val remove_unused_blocks : Code.program -> Code.program

compiler/lib/eval.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,12 @@ let f info p =
760760
let t = Timer.make () in
761761
let blocks = eval update_count ~target:(Config.target ()) info p.blocks in
762762
let blocks = drop_exception_handler drop_count blocks in
763+
let p = Deadcode.remove_unused_blocks { p with blocks } in
763764
if times () then Format.eprintf " eval: %a@." Timer.print t;
764765
if stats ()
765766
then
766767
Format.eprintf
767768
"Stats - eval: %d optimizations, %d dropped exception handlers@."
768769
!update_count
769770
!drop_count;
770-
{ p with blocks }
771+
p

0 commit comments

Comments
 (0)