@@ -193,18 +193,26 @@ let empty_body b =
193
193
194
194
let remove_empty_blocks ~live_vars (p : Code.program ) : Code.program =
195
195
let shortcuts = Hashtbl. create 16 in
196
- let rec resolve_rec visited ((pc , args ) as cont ) =
196
+ let rec resolve_rec keep_empty visited ((pc , args ) as cont0 ) =
197
197
if Addr.Set. mem pc visited
198
- then cont
198
+ then cont0
199
199
else
200
200
match Hashtbl. find_opt shortcuts pc with
201
201
| Some (params , cont ) ->
202
- let pc', args' = resolve_rec (Addr.Set. add pc visited) cont in
203
- let s = Subst. from_map (Subst. build_mapping params args) in
204
- pc', List. map ~f: s args'
205
- | None -> cont
202
+ let pc', args' = resolve_rec keep_empty (Addr.Set. add pc visited) cont in
203
+ if keep_empty && not (List. is_empty args')
204
+ then cont0
205
+ else
206
+ let s = Subst. from_map (Subst. build_mapping params args) in
207
+ pc', List. map ~f: s args'
208
+ | None -> cont0
209
+ in
210
+ let resolve cont = resolve_rec false Addr.Set. empty cont in
211
+ let resolve_inside_switch =
212
+ match Config. target () with
213
+ | `JavaScript -> resolve
214
+ | `Wasm -> resolve_rec true Addr.Set. empty
206
215
in
207
- let resolve cont = resolve_rec Addr.Set. empty cont in
208
216
Addr.Map. iter
209
217
(fun pc block ->
210
218
match block with
@@ -233,7 +241,7 @@ let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
233
241
match branch with
234
242
| Branch cont -> Branch (resolve cont)
235
243
| Cond (x , cont1 , cont2 ) -> Cond (x, resolve cont1, resolve cont2)
236
- | Switch (x , a1 ) -> Switch (x, Array. map ~f: resolve a1)
244
+ | Switch (x , a1 ) -> Switch (x, Array. map ~f: resolve_inside_switch a1)
237
245
| Pushtrap (cont1 , x , cont2 ) -> Pushtrap (resolve cont1, x, resolve cont2)
238
246
| Poptrap cont -> Poptrap (resolve cont)
239
247
| Return _ | Raise _ | Stop -> branch)
0 commit comments