@@ -93,16 +93,18 @@ let phi p =
9393
9494let ( +> ) f g x = g (f x)
9595
96- let map_fst f (x , y , z ) = f x, y, z
96+ let map_fst f (x , y ) = f x, y
9797
98- let effects_and_exact_calls ~deadcode_sentinal (profile : Profile.t ) p =
98+ let effects_and_exact_calls ~keep_flow_data ~ deadcode_sentinal (profile : Profile.t ) p =
9999 let fast =
100100 match Config. effects () , profile with
101101 | (`Cps | `Double_translation ), _ -> false
102102 | _ , (O2 | O3 ) -> false
103103 | _ , O1 -> true
104104 in
105- let info = Global_flow. f ~fast p in
105+ let global_flow_data = Global_flow. f ~fast p in
106+ let _, info = global_flow_data in
107+ let global_flow_data = if keep_flow_data then Some global_flow_data else None in
106108 let pure_fun = Pure_fun. f p in
107109 let p, live_vars =
108110 if Config.Flag. globaldeadcode () && Config.Flag. deadcode ()
@@ -114,7 +116,8 @@ let effects_and_exact_calls ~deadcode_sentinal (profile : Profile.t) p =
114116 match Config. effects () with
115117 | `Cps | `Double_translation ->
116118 if debug () then Format. eprintf " Effects...@." ;
117- Effects. f ~flow_info: info ~live_vars p
119+ let p, trampolined_calls, in_cps = Effects. f ~flow_info: info ~live_vars p in
120+ (p, (trampolined_calls, in_cps, None ))
118121 |> map_fst
119122 (match Config. target () with
120123 | `Wasm -> Fun. id
@@ -124,8 +127,9 @@ let effects_and_exact_calls ~deadcode_sentinal (profile : Profile.t) p =
124127 Specialize. f ~function_arity: (fun f -> Global_flow. function_arity info f) p
125128 in
126129 ( p
127- , (Code.Var.Set. empty : Effects.trampolined_calls )
128- , (Code.Var.Set. empty : Effects.in_cps ) )
130+ , ( (Code.Var.Set. empty : Effects.trampolined_calls )
131+ , (Code.Var.Set. empty : Effects.in_cps )
132+ , global_flow_data ) )
129133
130134let print p =
131135 if debug () then Code.Print. program Format. err_formatter (fun _ _ -> " " ) p;
@@ -613,7 +617,7 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
613617 |> pack ~wrap_with_fun ~standalone
614618 |> check_js
615619
616- let optimize ~profile p =
620+ let optimize ~profile ~ keep_flow_data p =
617621 let deadcode_sentinal =
618622 (* If deadcode is disabled, this field is just fresh variable *)
619623 Code.Var. fresh_n " dummy"
@@ -626,7 +630,7 @@ let optimize ~profile p =
626630 | O2 -> o2
627631 | O3 -> o3)
628632 +> specialize_js_once_after
629- +> effects_and_exact_calls ~deadcode_sentinal profile
633+ +> effects_and_exact_calls ~keep_flow_data ~ deadcode_sentinal profile
630634 +> map_fst
631635 (match Config. target () , Config. effects () with
632636 | `JavaScript , `Disabled -> Generate_closure. f
@@ -637,12 +641,20 @@ let optimize ~profile p =
637641 in
638642 if times () then Format. eprintf " Start Optimizing...@." ;
639643 let t = Timer. make () in
640- let (program, variable_uses), trampolined_calls, in_cps = opt p in
644+ let (program, variable_uses), ( trampolined_calls, in_cps, global_flow_info) = opt p in
641645 let () = if times () then Format. eprintf " optimizations : %a@." Timer. print t in
642- { program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal }
646+ ( { program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal }
647+ , global_flow_info )
648+
649+ let optimize_for_wasm ~profile p =
650+ let optimized_code, global_flow_data = optimize ~profile ~keep_flow_data: true p in
651+ ( optimized_code
652+ , match global_flow_data with
653+ | Some data -> data
654+ | None -> Global_flow. f ~fast: false optimized_code.program )
643655
644656let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter p =
645- let optimized_code = optimize ~profile p in
657+ let optimized_code, _ = optimize ~profile ~keep_flow_data: false p in
646658 let exported_runtime = not standalone in
647659 let emit formatter =
648660 generate ~exported_runtime ~wrap_with_fun ~warn_on_unhandled_effect: standalone
0 commit comments