@@ -37,41 +37,42 @@ let times = Debug.find "times"
37
37
38
38
let stats = Debug. find " stats"
39
39
40
- let rewrite refs block m m' =
41
- let m, m' , l =
40
+ let rewrite_body unboxed_refs body ref_contents subst =
41
+ let ref_contents, subst , l =
42
42
List. fold_left
43
- ~f: (fun (m , m' , acc ) i ->
43
+ ~f: (fun (ref_contents , subst , acc ) i ->
44
44
match i with
45
45
| Let (x, Block (0 , [| y |], (NotArray | Unknown ), Maybe_mutable ))
46
- when Var.Set. mem x refs -> Var.Map. add x y m, m', acc
47
- | Let (y , Field (x , 0 , Non_float)) when Var.Map. mem x m ->
48
- (* Optimized away by Phisimpl *)
49
- m, Var.Map. add y (Var.Map. find x m) m', acc
50
- | Offset_ref (x , n ) when Var.Map. mem x m ->
51
- let y = Var. fresh () in
52
- ( Var.Map. add x y m
53
- , m'
46
+ when Var.Set. mem x unboxed_refs -> Var.Map. add x y ref_contents, subst, acc
47
+ | Let (y , Field (x , 0 , Non_float)) when Var.Map. mem x ref_contents ->
48
+ ref_contents, Var.Map. add y (Var.Map. find x ref_contents) subst, acc
49
+ | Offset_ref (x , n ) when Var.Map. mem x ref_contents ->
50
+ let y = Var. fork x in
51
+ ( Var.Map. add x y ref_contents
52
+ , subst
54
53
, Let
55
54
( y
56
55
, Prim
57
56
( Extern " %int_add"
58
- , [ Pv (Var.Map. find x m); Pc (Int (Targetint. of_int_exn n)) ] ) )
57
+ , [ Pv (Var.Map. find x ref_contents)
58
+ ; Pc (Int (Targetint. of_int_exn n))
59
+ ] ) )
59
60
:: acc )
60
- | Set_field (x , 0 , Non_float, y ) when Var.Map. mem x m ->
61
- Var.Map. add x y m, m' , acc
61
+ | Set_field (x , 0 , Non_float, y ) when Var.Map. mem x ref_contents ->
62
+ Var.Map. add x y ref_contents, subst , acc
62
63
| Event _ -> (
63
- ( m
64
- , m'
64
+ ( ref_contents
65
+ , subst
65
66
, match acc with
66
67
| Event _ :: prev ->
67
68
(* Avoid consecutive events (keep just the last one) *)
68
69
i :: prev
69
70
| _ -> i :: acc ))
70
- | _ -> m, m' , i :: acc)
71
- block. body
72
- ~init: (m, m' , [] )
71
+ | _ -> ref_contents, subst , i :: acc)
72
+ body
73
+ ~init: (ref_contents, subst , [] )
73
74
in
74
- m, m' , List. rev l
75
+ ref_contents, subst , List. rev l
75
76
76
77
let rewrite_cont relevant_vars vars (pc' , args ) =
77
78
let refs, _ = Int.Hashtbl. find relevant_vars pc' in
@@ -100,28 +101,29 @@ let rewrite_function p variables pc subst =
100
101
let rec traverse_tree ' g pc blocks subst =
101
102
let block = Addr.Map. find pc p.blocks in
102
103
let refs, refs' = Int.Hashtbl. find relevant_vars pc in
103
- let vars =
104
+ let ref_contents =
104
105
Var.Set. fold (fun x m -> Var.Map. add x (Var. fork x) m) refs Var.Map. empty
105
106
in
106
- let params = List. map ~f: snd (Var.Map. bindings vars ) @ block.params in
107
- let vars , subst, body = rewrite refs' block vars subst in
107
+ let params = List. map ~f: snd (Var.Map. bindings ref_contents ) @ block.params in
108
+ let ref_contents , subst, body = rewrite_body refs' block.body ref_contents subst in
108
109
let branch =
109
110
match block.branch with
110
111
| Return _ | Raise _ | Stop -> block.branch
111
- | Branch cont -> Branch (rewrite_cont relevant_vars vars cont)
112
+ | Branch cont -> Branch (rewrite_cont relevant_vars ref_contents cont)
112
113
| Cond (x , cont , cont' ) ->
113
114
Cond
114
115
( x
115
- , rewrite_cont relevant_vars vars cont
116
- , rewrite_cont relevant_vars vars cont' )
116
+ , rewrite_cont relevant_vars ref_contents cont
117
+ , rewrite_cont relevant_vars ref_contents cont' )
117
118
| Switch (x , a ) ->
118
- Switch (x, Array. map ~f: (fun cont -> rewrite_cont relevant_vars vars cont) a)
119
+ Switch
120
+ (x, Array. map ~f: (fun cont -> rewrite_cont relevant_vars ref_contents cont) a)
119
121
| Pushtrap (cont , x , cont' ) ->
120
122
Pushtrap
121
- ( rewrite_cont relevant_vars vars cont
123
+ ( rewrite_cont relevant_vars ref_contents cont
122
124
, x
123
- , rewrite_cont relevant_vars vars cont' )
124
- | Poptrap cont -> Poptrap (rewrite_cont relevant_vars vars cont)
125
+ , rewrite_cont relevant_vars ref_contents cont' )
126
+ | Poptrap cont -> Poptrap (rewrite_cont relevant_vars ref_contents cont)
125
127
in
126
128
let blocks = Addr.Map. add pc { params; body; branch } blocks in
127
129
Addr.Set. fold
0 commit comments