Skip to content

Commit af4c47c

Browse files
authored
Replace Cfg_with_liveness with Cfg_with_infos (oxcaml#1521)
1 parent 41a378e commit af4c47c

32 files changed

+363
-352
lines changed

backend/asmgen.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,19 @@ let compile_fundecl ~ppf_dump ~funcnames fd_cmm =
293293
let cfg =
294294
fd
295295
++ Profile.record ~accumulate:true "cfgize" cfgize
296-
++ Cfg_with_liveness.make
296+
++ Cfg_with_infos.make
297297
++ Profile.record ~accumulate:true "cfg_deadcode" Cfg_deadcode.run
298298
in
299299
let cfg_description =
300-
Regalloc_validate.Description.create (Cfg_with_liveness.cfg_with_layout cfg)
300+
Regalloc_validate.Description.create (Cfg_with_infos.cfg_with_layout cfg)
301301
in
302302
cfg
303303
++ begin match regalloc with
304304
| IRC -> Profile.record ~accumulate:true "cfg_irc" Regalloc_irc.run
305305
| LS -> Profile.record ~accumulate:true "cfg_ls" Regalloc_ls.run
306306
| Upstream -> assert false
307307
end
308-
++ Cfg_with_liveness.cfg_with_layout
308+
++ Cfg_with_infos.cfg_with_layout
309309
++ Profile.record ~accumulate:true "cfg_validate_description" (Regalloc_validate.run cfg_description)
310310
++ Profile.record ~accumulate:true "cfg_simplify" Regalloc_utils.simplify_cfg
311311
++ Profile.record ~accumulate:true "save_cfg" save_cfg

backend/cfg/cfg_deadcode.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open! Regalloc_utils
44
module DLL = Flambda_backend_utils.Doubly_linked_list
55

66
let live_before :
7-
type a. a Cfg.instruction -> Cfg_with_liveness.liveness -> Reg.Set.t =
7+
type a. a Cfg.instruction -> Cfg_with_infos.liveness -> Reg.Set.t =
88
fun instr liveness ->
99
match Cfg_dataflow.Instr.Tbl.find_opt liveness instr.id with
1010
| None -> fatal "no liveness information for instruction %d" instr.id
@@ -28,12 +28,11 @@ let remove_deadcode (body : Cfg.basic_instruction_list) changed liveness
2828
changed := !changed || is_deadcode;
2929
not is_deadcode)
3030

31-
let run cfg_with_liveness =
32-
let liveness = Cfg_with_liveness.liveness cfg_with_liveness in
31+
let run cfg_with_infos =
32+
let liveness = Cfg_with_infos.liveness cfg_with_infos in
3333
let changed = ref false in
34-
Cfg.iter_blocks (Cfg_with_liveness.cfg cfg_with_liveness)
35-
~f:(fun _label block ->
34+
Cfg.iter_blocks (Cfg_with_infos.cfg cfg_with_infos) ~f:(fun _label block ->
3635
remove_deadcode block.body changed liveness
3736
(live_before block.terminator liveness));
38-
if !changed then Cfg_with_liveness.invalidate_liveness cfg_with_liveness;
39-
cfg_with_liveness
37+
if !changed then Cfg_with_infos.invalidate_liveness cfg_with_infos;
38+
cfg_with_infos

backend/cfg/cfg_deadcode.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
(* Dead code elimination: remove pure instructions whose results are not
44
used. *)
5-
val run : Cfg_with_liveness.t -> Cfg_with_liveness.t
5+
val run : Cfg_with_infos.t -> Cfg_with_infos.t

backend/cfg/cfg_with_infos.ml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[@@@ocaml.warning "+a-30-40-41-42"]
2+
3+
type liveness = Cfg_liveness.Liveness.domain Cfg_dataflow.Instr.Tbl.t
4+
5+
let liveness_analysis : Cfg_with_layout.t -> liveness =
6+
fun cfg_with_layout ->
7+
let cfg = Cfg_with_layout.cfg cfg_with_layout in
8+
let init = { Cfg_liveness.before = Reg.Set.empty; across = Reg.Set.empty } in
9+
match
10+
Cfg_liveness.Liveness.run cfg ~init ~map:Cfg_liveness.Liveness.Instr ()
11+
with
12+
| Ok liveness -> liveness
13+
| Aborted _ -> .
14+
| Max_iterations_reached ->
15+
Misc.fatal_errorf "Unable to compute liveness from CFG for function %s@."
16+
cfg.Cfg.fun_name
17+
18+
type t =
19+
{ cfg_with_layout : Cfg_with_layout.t;
20+
liveness : liveness option ref;
21+
dominators : Cfg_dominators.t option ref;
22+
loop_infos : Cfg_loop_infos.t option ref
23+
}
24+
25+
let make cfg_with_layout =
26+
{ cfg_with_layout;
27+
liveness = ref None;
28+
dominators = ref None;
29+
loop_infos = ref None
30+
}
31+
32+
let cfg_with_layout t = t.cfg_with_layout
33+
34+
let cfg t = Cfg_with_layout.cfg t.cfg_with_layout
35+
36+
let fold_blocks t ~f ~init = Cfg.fold_blocks (cfg t) ~f ~init
37+
38+
let get_block_exn t label = Cfg.get_block_exn (cfg t) label
39+
40+
let[@inline] compute_if_necessary r ~f =
41+
match !r with
42+
| Some value -> value
43+
| None ->
44+
let value = f () in
45+
r := Some value;
46+
value
47+
48+
let liveness t =
49+
compute_if_necessary t.liveness ~f:(fun () ->
50+
liveness_analysis t.cfg_with_layout)
51+
52+
let liveness_find t id = Cfg_dataflow.Instr.Tbl.find (liveness t) id
53+
54+
let liveness_find_opt t id = Cfg_dataflow.Instr.Tbl.find_opt (liveness t) id
55+
56+
let invalidate_liveness t = t.liveness := None
57+
58+
let dominators t =
59+
compute_if_necessary t.dominators ~f:(fun () -> Cfg_dominators.build (cfg t))
60+
61+
let loop_infos t =
62+
compute_if_necessary t.loop_infos ~f:(fun () ->
63+
Cfg_loop_infos.build (cfg t) (dominators t))
64+
65+
let invalidate_dominators_and_loop_infos t =
66+
t.dominators := None;
67+
t.loop_infos := None

backend/cfg/cfg_with_infos.mli

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[@@@ocaml.warning "+a-30-40-41-42"]
2+
3+
type liveness = Cfg_liveness.Liveness.domain Cfg_dataflow.Instr.Tbl.t
4+
5+
(** Holds a Cfg_with_layout.t value, and "caches" for:
6+
- the liveness information;
7+
- the dominators information;
8+
- the loop information.
9+
10+
Each cache can be invalidated, and should be when modification of the
11+
underlying CFG would cause the cached information to become invalid.
12+
13+
Typically, any structural change to the graph, or change to the `arg` and
14+
`res` field of the instrutions is likely to result in a different liveness
15+
information, but only structural changes to the graph would affect dominators
16+
and loop information. *)
17+
type t
18+
19+
val make : Cfg_with_layout.t -> t
20+
21+
val cfg_with_layout : t -> Cfg_with_layout.t
22+
23+
val cfg : t -> Cfg.t
24+
25+
val fold_blocks :
26+
t -> f:(Label.t -> Cfg.basic_block -> 'a -> 'a) -> init:'a -> 'a
27+
28+
val get_block_exn : t -> Label.t -> Cfg.basic_block
29+
30+
val liveness : t -> liveness
31+
32+
val liveness_find : t -> int -> Cfg_liveness.Liveness.domain
33+
34+
val liveness_find_opt : t -> int -> Cfg_liveness.Liveness.domain option
35+
36+
val invalidate_liveness : t -> unit
37+
38+
val dominators : t -> Cfg_dominators.t
39+
40+
val loop_infos : t -> Cfg_loop_infos.t
41+
42+
val invalidate_dominators_and_loop_infos : t -> unit

backend/cfg/cfg_with_liveness.ml

Lines changed: 0 additions & 49 deletions
This file was deleted.

backend/cfg/cfg_with_liveness.mli

Lines changed: 0 additions & 26 deletions
This file was deleted.

backend/regalloc/regalloc_invariants.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ let postcondition_layout : Cfg_with_layout.t -> unit =
222222
fatal "register class %d has the following unused slots: %s" reg_class
223223
(string_of_set unused))
224224

225-
let postcondition_liveness : Cfg_with_liveness.t -> unit =
226-
fun cfg_with_liveness ->
227-
postcondition_layout (Cfg_with_liveness.cfg_with_layout cfg_with_liveness);
228-
let cfg = Cfg_with_liveness.cfg cfg_with_liveness in
225+
let postcondition_liveness : Cfg_with_infos.t -> unit =
226+
fun cfg_with_infos ->
227+
postcondition_layout (Cfg_with_infos.cfg_with_layout cfg_with_infos);
228+
let cfg = Cfg_with_infos.cfg cfg_with_infos in
229229
let entry_block = Cfg.get_block_exn cfg cfg.entry_label in
230230
let live_at_entry_point =
231-
Cfg_with_liveness.liveness_find cfg_with_liveness
231+
Cfg_with_infos.liveness_find cfg_with_infos
232232
(Cfg.first_instruction_id entry_block)
233233
in
234234
Reg.Set.iter

backend/regalloc/regalloc_invariants.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ val precondition : Cfg_with_layout.t -> unit
44

55
val postcondition_layout : Cfg_with_layout.t -> unit
66

7-
val postcondition_liveness : Cfg_with_liveness.t -> unit
7+
val postcondition_liveness : Cfg_with_infos.t -> unit

0 commit comments

Comments
 (0)