Skip to content

Commit 2c6284e

Browse files
committed
small refactoring
1 parent 42bde03 commit 2c6284e

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

compiler/lib/ref_unboxing.ml

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,51 @@ let rewrite_body unboxed_refs body ref_contents subst =
7474
in
7575
ref_contents, subst, List.rev l
7676

77-
let rewrite_cont relevant_vars vars (pc', args) =
77+
let rewrite_cont relevant_vars ref_contents (pc', args) =
7878
let refs, _ = Int.Hashtbl.find relevant_vars pc' in
79-
let vars = Var.Map.filter (fun x _ -> Var.Set.mem x refs) vars in
79+
let vars = Var.Map.filter (fun x _ -> Var.Set.mem x refs) ref_contents in
8080
pc', List.map ~f:snd (Var.Map.bindings vars) @ args
8181

82-
let rewrite_function p variables pc subst =
83-
let relevant_vars = Int.Hashtbl.create 16 in
82+
let rewrite_branch relevant_vars ref_contents branch =
83+
match branch with
84+
| Return _ | Raise _ | Stop -> branch
85+
| Branch cont -> Branch (rewrite_cont relevant_vars ref_contents cont)
86+
| Cond (x, cont, cont') ->
87+
Cond
88+
( x
89+
, rewrite_cont relevant_vars ref_contents cont
90+
, rewrite_cont relevant_vars ref_contents cont' )
91+
| Switch (x, a) ->
92+
Switch (x, Array.map ~f:(fun cont -> rewrite_cont relevant_vars ref_contents cont) a)
93+
| Pushtrap (cont, x, cont') ->
94+
Pushtrap
95+
( rewrite_cont relevant_vars ref_contents cont
96+
, x
97+
, rewrite_cont relevant_vars ref_contents cont' )
98+
| Poptrap cont -> Poptrap (rewrite_cont relevant_vars ref_contents cont)
99+
100+
let rewrite_function p ~unboxed_refs pc subst =
84101
let g = Structure.(dominator_tree (build_graph p.blocks pc)) in
85-
let rec traverse_tree g pc refs =
86-
let block = Addr.Map.find pc p.blocks in
87-
let refs' =
88-
List.fold_left
89-
~f:(fun s i ->
90-
match i with
91-
| Let (x, Block (0, [| _ |], (NotArray | Unknown), Maybe_mutable))
92-
when Var.Hashtbl.mem variables x -> Var.Set.add x s
93-
| _ -> s)
94-
~init:refs
95-
block.body
102+
let relevant_vars =
103+
let relevant_vars = Int.Hashtbl.create 16 in
104+
let rec traverse_tree g pc refs =
105+
let block = Addr.Map.find pc p.blocks in
106+
let refs' =
107+
List.fold_left
108+
~f:(fun s i ->
109+
match i with
110+
| Let (x, Block (0, [| _ |], (NotArray | Unknown), Maybe_mutable))
111+
when Var.Hashtbl.mem unboxed_refs x -> Var.Set.add x s
112+
| _ -> s)
113+
~init:refs
114+
block.body
115+
in
116+
Int.Hashtbl.add relevant_vars pc (refs, refs');
117+
Addr.Set.iter (fun pc' -> traverse_tree g pc' refs') (Structure.get_edges g pc)
96118
in
97-
Int.Hashtbl.add relevant_vars pc (refs, refs');
98-
Addr.Set.iter (fun pc' -> traverse_tree g pc' refs') (Structure.get_edges g pc)
119+
traverse_tree g pc Var.Set.empty;
120+
relevant_vars
99121
in
100-
traverse_tree g pc Var.Set.empty;
101122
let rec traverse_tree' g pc blocks subst =
102123
let block = Addr.Map.find pc p.blocks in
103124
let refs, refs' = Int.Hashtbl.find relevant_vars pc in
@@ -106,25 +127,7 @@ let rewrite_function p variables pc subst =
106127
in
107128
let params = List.map ~f:snd (Var.Map.bindings ref_contents) @ block.params in
108129
let ref_contents, subst, body = rewrite_body refs' block.body ref_contents subst in
109-
let branch =
110-
match block.branch with
111-
| Return _ | Raise _ | Stop -> block.branch
112-
| Branch cont -> Branch (rewrite_cont relevant_vars ref_contents cont)
113-
| Cond (x, cont, cont') ->
114-
Cond
115-
( x
116-
, rewrite_cont relevant_vars ref_contents cont
117-
, rewrite_cont relevant_vars ref_contents cont' )
118-
| Switch (x, a) ->
119-
Switch
120-
(x, Array.map ~f:(fun cont -> rewrite_cont relevant_vars ref_contents cont) a)
121-
| Pushtrap (cont, x, cont') ->
122-
Pushtrap
123-
( rewrite_cont relevant_vars ref_contents cont
124-
, x
125-
, rewrite_cont relevant_vars ref_contents cont' )
126-
| Poptrap cont -> Poptrap (rewrite_cont relevant_vars ref_contents cont)
127-
in
130+
let branch = rewrite_branch relevant_vars ref_contents block.branch in
128131
let blocks = Addr.Map.add pc { params; body; branch } blocks in
129132
Addr.Set.fold
130133
(fun pc' (blocks, subst) -> traverse_tree' g pc' blocks subst)
@@ -212,7 +215,7 @@ let f p =
212215
in
213216
let p, subst =
214217
Addr.Set.fold
215-
(fun pc (p, subst) -> rewrite_function p candidates pc subst)
218+
(fun pc (p, subst) -> rewrite_function p ~unboxed_refs:candidates pc subst)
216219
functions
217220
(p, Var.Map.empty)
218221
in

0 commit comments

Comments
 (0)