2025-01-24 18:59:28 -08:00
|
|
|
(** Dead Code Elimination *)
|
|
|
|
|
open Ll
|
|
|
|
|
open Datastructures
|
|
|
|
|
|
|
|
|
|
|
2025-01-24 23:10:01 -08:00
|
|
|
(* dce_block ---------------------------------------------------------------- *)
|
2025-01-24 18:59:28 -08:00
|
|
|
(* TASK: This function should optimize a block by removing dead instructions
|
|
|
|
|
- lb: a function from uids to the live-OUT set at the
|
|
|
|
|
corresponding program point
|
|
|
|
|
- ab: the alias map flowing IN to each program point in the block
|
|
|
|
|
- b: the current ll block
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
Call instructions are never considered to be dead (they might produce
|
|
|
|
|
side effects)
|
|
|
|
|
|
|
|
|
|
Store instructions are not dead if the pointer written to is live _or_
|
|
|
|
|
the pointer written to may be aliased.
|
|
|
|
|
|
|
|
|
|
Other instructions are dead if the value they compute is not live.
|
|
|
|
|
|
|
|
|
|
Hint: Consider using List.filter
|
|
|
|
|
*)
|
2025-01-24 23:10:01 -08:00
|
|
|
let dce_block (lb:uid -> Liveness.Fact.t)
|
|
|
|
|
(ab:uid -> Alias.fact)
|
|
|
|
|
(b:Ll.block) : Ll.block =
|
|
|
|
|
failwith "Dce.dce_block unimplemented"
|
2025-01-24 18:59:28 -08:00
|
|
|
|
2025-01-24 23:10:01 -08:00
|
|
|
(* Run DCE on all the blocks of a given control-flow-graph. *)
|
2025-01-24 18:59:28 -08:00
|
|
|
let run (lg:Liveness.Graph.t) (ag:Alias.Graph.t) (cfg:Cfg.t) : Cfg.t =
|
|
|
|
|
|
|
|
|
|
LblS.fold (fun l cfg ->
|
|
|
|
|
let b = Cfg.block cfg l in
|
|
|
|
|
|
|
|
|
|
(* compute liveness at each program point for the block *)
|
|
|
|
|
let lb = Liveness.Graph.uid_out lg l in
|
|
|
|
|
|
|
|
|
|
(* compute aliases at each program point for the block *)
|
|
|
|
|
let ab = Alias.Graph.uid_in ag l in
|
|
|
|
|
|
|
|
|
|
(* compute optimized block *)
|
|
|
|
|
let b' = dce_block lb ab b in
|
|
|
|
|
Cfg.add_block l b' cfg
|
|
|
|
|
) (Cfg.nodes cfg) cfg
|
|
|
|
|
|