2025-01-24 18:59:28 -08:00
|
|
|
(** Optimizer *)
|
|
|
|
|
open Ll
|
2025-01-24 21:23:08 -08:00
|
|
|
module Platform = Util.Platform
|
|
|
|
|
|
2025-01-24 18:59:28 -08:00
|
|
|
(* dead code elimination ---------------------------------------------------- *)
|
|
|
|
|
let dce (g:Cfg.t) : Cfg.t =
|
|
|
|
|
let ag = Alias.analyze g in
|
|
|
|
|
let lg = Liveness.analyze g in
|
|
|
|
|
Dce.run lg ag g
|
|
|
|
|
|
|
|
|
|
(* constant propagation ----------------------------------------------------- *)
|
|
|
|
|
let cp (g:Cfg.t) : Cfg.t =
|
|
|
|
|
let cg = Constprop.analyze g in
|
|
|
|
|
Constprop.run cg g
|
|
|
|
|
|
|
|
|
|
(* "full" optimization: n rounds of (dce followed by constant) propagation -- *)
|
|
|
|
|
let rec pass n (g:Cfg.t) =
|
|
|
|
|
if n <= 0
|
|
|
|
|
then g
|
|
|
|
|
else pass (n - 1) (g |> dce |> cp)
|
|
|
|
|
|
|
|
|
|
(* optimize an fdecl -------------------------------------------------------- *)
|
|
|
|
|
(* runs (two) passes of dce followed by constant propagation on the supplied
|
|
|
|
|
LL IR fdecl. *)
|
2025-01-24 23:10:01 -08:00
|
|
|
let opt_fdecl_O1 (gid,fdecl:Ll.gid * Ll.fdecl) : Ll.gid * Ll.fdecl =
|
2025-01-24 18:59:28 -08:00
|
|
|
let g = pass 2 (Cfg.of_ast fdecl) in
|
|
|
|
|
gid, Cfg.to_ast g
|
|
|
|
|
|
2025-01-24 23:10:01 -08:00
|
|
|
(* flag for the main compiler driver *)
|
|
|
|
|
let do_opt = ref false
|
2025-01-24 18:59:28 -08:00
|
|
|
|
|
|
|
|
(* optimize each fdecl in the program *)
|
|
|
|
|
let optimize (p:Ll.prog) : Ll.prog =
|
2025-01-24 23:10:01 -08:00
|
|
|
if !do_opt
|
2025-01-24 18:59:28 -08:00
|
|
|
then begin
|
|
|
|
|
Platform.verb @@ Printf.sprintf "..optimizing";
|
2025-01-24 23:10:01 -08:00
|
|
|
{ p with Ll.fdecls = List.map opt_fdecl_O1 p.Ll.fdecls }
|
2025-01-24 18:59:28 -08:00
|
|
|
end
|
|
|
|
|
else p
|
|
|
|
|
|