Source file global_deadcode.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
open Code
open Stdlib
let debug = Debug.find "globaldeadcode"
let times = Debug.find "times"
(** Definition of a variable [x]. *)
type def =
| Expr of expr (** [x] is defined by an expression. *)
| Param (** [x] is a block or closure parameter. *)
module Domain : sig
(** Liveness of a variable [x], forming a lattice structure. *)
type t = private
| Top (** [x] is live and not a block. *)
| Live of t IntMap.t
(** [x] is a live block with a (non-empty) set of live fields. *)
| Dead (** [x] is dead. *)
val equal : t -> t -> bool
val bot : t
val top : t
val live_block : t
val live_field : int -> t -> t
val join : t -> t -> t
end = struct
type t =
| Top
| Live of t IntMap.t
| Dead
let rec equal l1 l2 =
match l1, l2 with
| Top, Top | Dead, Dead -> true
| Live f1, Live f2 -> IntMap.equal equal f1 f2
| Top, (Dead | Live _) | Live _, (Dead | Top) | Dead, (Live _ | Top) -> false
let bot = Dead
let top = Top
let rec depth l =
match l with
| Top | Dead -> 0
| Live f -> 1 + IntMap.fold (fun _ l' acc -> max (depth l') acc) f 0
let rec truncate depth l =
match l with
| Top | Dead -> l
| Live f ->
if depth = 0 then Top else Live (IntMap.map (fun l' -> truncate (depth - 1) l') f)
let depth_treshold = 4
let live_block = Live IntMap.empty
let live_field i l =
Live
(IntMap.singleton
i
(if depth l > depth_treshold then truncate depth_treshold l else l))
(** Join the liveness according to lattice structure. *)
let rec join l1 l2 =
match l1, l2 with
| _, Top | Top, _ -> Top
| Live f1, Live f2 -> Live (IntMap.union (fun _ l1 l2 -> Some (join l1 l2)) f1 f2)
| Dead, Live f | Live f, Dead -> Live f
| Dead, Dead -> Dead
end
let iter_with_scope prog f =
Code.fold_closures
prog
(fun scope _ (pc, _) _ () ->
Code.traverse
{ fold = fold_children }
(fun pc () -> f scope (Addr.Map.find pc prog.blocks))
pc
prog.blocks
())
()
let definitions prog =
let defs = Var.Tbl.make () Param in
let set_def x d = Var.Tbl.set defs x d in
Addr.Map.iter
(fun _ block ->
List.iter
~f:(fun i ->
match i with
| Let (x, e) -> set_def x (Expr e)
| Assign (x, _) -> set_def x Param
| Event _ | Set_field (_, _, _, _) | Offset_ref (_, _) | Array_set (_, _, _) ->
())
block.body)
prog.blocks;
defs
let variable_may_escape x (global_info : Global_flow.info) =
match global_info.info_variable_may_escape.(Var.idx x) with
| Escape | Escape_constant -> true
| No -> false
(** Type of variable usage. *)
type usage_kind =
| Compute (** variable y is used to compute x *)
| Propagate of
{ scope : Var.t list
; src : Var.t
} (** values of y propagate to x when the scope is live *)
| Scope (** variable x is defined in function y *)
(** Compute the adjacency list for the dependency graph of given program. An edge between
variables [x] and [y] is marked [Compute] if [x] is used in the definition of [y]. It is marked
as [Propagate] if [x] is applied as a closure or block argument the parameter [y].
We use information from global flow to try to add edges between function calls and their return values
at known call sites. *)
let usages prog (global_info : Global_flow.info) scoped_live_vars :
(usage_kind * Var.Set.t) list Var.Tbl.t =
let uses = Var.Tbl.make () [] in
let add_uses kind x vars =
let p = kind, vars in
Var.Tbl.set uses x (p :: Var.Tbl.get uses x);
match kind with
| Propagate { scope; _ } ->
List.iter ~f:(fun z -> Var.Tbl.set uses z (p :: Var.Tbl.get uses z)) scope
| _ -> ()
in
let add_use kind x y = add_uses kind x (Var.Set.singleton y) in
let add_arg_dep params args =
List.iter2 ~f:(fun x y -> add_use (Propagate { scope = []; src = x }) x y) params args
in
let add_cont_deps (pc, args) =
match try Some (Addr.Map.find pc prog.blocks) with Not_found -> None with
| Some block -> add_arg_dep block.params args
| None -> ()
in
let add_expr_uses scope x e : unit =
match e with
| Apply { f; args; _ } ->
(match Var.Tbl.get global_info.info_approximation f with
| Top -> ()
| Values { known; _ } ->
Var.Set.iter
(fun k ->
match global_info.info_defs.(Var.idx k) with
| Expr (Closure (params, _, _)) ->
if List.compare_lengths params args = 0
then (
let scope = k :: scope in
add_uses
(Propagate { scope; src = x })
x
(Var.Map.find k global_info.info_return_vals);
List.iter2
~f:(fun x y -> add_use (Propagate { scope; src = x }) x y)
params
args)
| _ -> ())
known);
add_use Compute x f;
List.iter
~f:(fun a -> if variable_may_escape a global_info then add_use Compute x a)
args
| Block (_, vars, _, _) -> Array.iter ~f:(add_use Compute x) vars
| Field (z, _, _) -> add_use Compute x z
| Constant _ -> ()
| Special _ -> ()
| Closure (_, cont, _) -> add_cont_deps cont
| Prim (_, args) ->
List.iter
~f:(fun arg ->
match arg with
| Pv v -> add_use Compute x v
| Pc _ -> ())
args
in
let add_block_uses scope block =
List.iter
~f:(fun i ->
match i with
| Let (x, e) -> add_expr_uses scope x e
| Assign (x, y) -> add_use (Propagate { scope = []; src = x }) x y
| Event _ | Set_field (_, _, _, _) | Offset_ref (_, _) | Array_set (_, _, _) -> ())
block.body;
match block.branch with
| Return _ | Raise _ | Stop -> ()
| Branch cont -> add_cont_deps cont
| Cond (_, cont1, cont2) ->
add_cont_deps cont1;
add_cont_deps cont2
| Switch (_, a) -> Array.iter ~f:add_cont_deps a
| Pushtrap (cont, _, cont_h) ->
add_cont_deps cont;
add_cont_deps cont_h
| Poptrap cont -> add_cont_deps cont
in
iter_with_scope prog (fun f block ->
add_block_uses
(match f with
| Some f -> [ f ]
| None -> [])
block);
Var.Tbl.iter
(fun scope h ->
match h with
| None -> ()
| Some h -> Var.Hashtbl.iter (fun x _ -> add_use Scope scope x) h)
scoped_live_vars;
uses
(** Return the set of variables used in a given expression *)
let expr_vars e =
let vars = Var.Set.empty in
match e with
| Apply { f; args; _ } ->
let vars = Var.Set.add f vars in
List.fold_left ~f:(fun acc x -> Var.Set.add x acc) ~init:vars args
| Block (_, params, _, _) ->
Array.fold_left ~f:(fun acc x -> Var.Set.add x acc) ~init:vars params
| Field (z, _, _) -> Var.Set.add z vars
| Prim (_, args) ->
List.fold_left
~f:(fun acc v ->
match v with
| Pv v -> Var.Set.add v acc
| Pc _ -> acc)
~init:vars
args
| Constant _ | Closure (_, _, _) | Special _ -> vars
(** Compute the initial liveness of each variable in the program.
A variable [x] is marked as [Top] if
+ It is used in an impure expression (as defined by [Pure_fun.pure_expr]);
+ It is used in a conditonal/switch;
+ It is raised by an exception;
+ It is used in another stateful instruction (like setting a block or array field);
+ Or, it is returned or applied to a function and the global flow analysis marked it as escaping.
A variable [x[i]] is marked as [Live {i}] if it is used in an instruction where field [i] is referenced or set. *)
let liveness prog pure_funs (global_info : Global_flow.info) =
let live_vars = Var.Tbl.make () Domain.bot in
let scoped_live_vars = Var.Tbl.make () None in
let get_hashtbl scope =
match Var.Tbl.get scoped_live_vars scope with
| Some h -> h
| None ->
let h = Var.Hashtbl.create 8 in
Var.Tbl.set scoped_live_vars scope (Some h);
h
in
let add_top scope v =
match scope with
| None -> Var.Tbl.set live_vars v Domain.top
| Some scope ->
let h = get_hashtbl scope in
Var.Hashtbl.replace h v Domain.top
in
let add_live_field scope v i =
let update_field l i = Domain.join l (Domain.live_field i Domain.top) in
match scope with
| None -> Var.Tbl.set live_vars v (update_field (Var.Tbl.get live_vars v) i)
| Some scope ->
let h = get_hashtbl scope in
Var.Hashtbl.replace
h
v
(update_field (try Var.Hashtbl.find h v with Not_found -> Domain.bot) i)
in
let live_instruction scope i =
match i with
| Let (_, e) -> (
if not (Pure_fun.pure_expr pure_funs e)
then
match e with
| Apply { f; args; _ } ->
add_top scope f;
List.iter
~f:(fun x -> if variable_may_escape x global_info then add_top scope x)
args
| Block (_, _, _, _)
| Field (_, _, _)
| Closure (_, _, _)
| Constant _
| Prim (_, _)
| Special _ ->
let vars = expr_vars e in
Var.Set.iter (fun x -> add_top scope x) vars)
| Set_field (x, i, _, y) ->
add_live_field scope x i;
add_top scope y
| Array_set (x, y, z) ->
add_top scope x;
add_top scope y;
add_top scope z
| Offset_ref (x, _) -> add_live_field scope x 0
| Event _ | Assign (_, _) -> ()
in
let live_block scope block =
List.iter ~f:(fun i -> live_instruction scope i) block.body;
match block.branch with
| Return x -> if variable_may_escape x global_info then add_top scope x
| Raise (x, _) -> add_top scope x
| Cond (x, _, _) -> add_top scope x
| Switch (x, _) -> add_top scope x
| Stop | Branch _ | Poptrap _ | Pushtrap _ -> ()
in
iter_with_scope prog live_block;
live_vars, scoped_live_vars
let variables deps =
let vars = Var.ISet.empty () in
Var.Tbl.iter (fun v _ -> Var.ISet.add vars v) deps;
vars
(** Propagate liveness of the usages of a variable [x] to [x]. The liveness of [x] is
defined by joining its current liveness and the contribution of each vairable [y]
that uses [x]. *)
let propagate defs scoped_live_vars ~state ~dep:y ~target:x ~action:usage_kind =
match usage_kind with
| Compute -> (
match Var.Tbl.get state y with
| Domain.Dead -> Domain.bot
| Live fields as l -> (
match Var.Tbl.get defs y with
| Expr (Block (_, vars, _, _)) ->
let live = ref Domain.bot in
Array.iteri
~f:(fun i v ->
if Var.equal v x
then
match IntMap.find_opt i fields with
| Some l -> live := Domain.join !live l
| None -> ())
vars;
!live
| Expr (Field (_, i, _)) -> Domain.live_field i l
| Expr (Prim (IsInt, _)) -> Domain.live_block
| _ -> Domain.top)
| Top -> (
match Var.Tbl.get defs y with
| Expr (Field (_, i, _)) -> Domain.live_field i Domain.top
| Expr (Prim (IsInt, _)) -> Domain.live_block
| _ -> Domain.top))
| Propagate { scope; src } ->
if
List.for_all scope ~f:(fun z ->
match Var.Tbl.get state z with
| Dead -> false
| _ -> true)
then Var.Tbl.get state src
else Domain.bot
| Scope -> (
match Var.Tbl.get state y with
| Dead -> Domain.bot
| _ -> (
match Var.Tbl.get scoped_live_vars y with
| Some h -> Var.Hashtbl.find h x
| None -> assert false))
module Solver =
Dgraph.Solver (Var) (Var.ISet) (Var.Tbl)
(struct
type t = usage_kind
end)
(Domain)
let solver vars uses defs live_vars scoped_live_vars =
let g =
{ Solver.domain = vars
; iter_children =
(fun f x ->
List.iter
~f:(fun (usage_kind, vars) -> Var.Set.iter (fun y -> f y usage_kind) vars)
(Var.Tbl.get uses x))
}
in
Solver.f ~state:live_vars g (propagate defs scoped_live_vars)
(** Replace each instance of a dead variable with a sentinal value.
Blocks that end in dead variables are compacted to the first live entry.
Dead variables are replaced when
+ They appear in a dead field of a block; or
+ They are returned; or
+ They are applied to a function.
*)
let zero prog pure_funs sentinal live_table =
let compact_vars vars =
let i = ref (Array.length vars - 1) in
while !i >= 0 && Var.equal vars.(!i) sentinal do
i := !i - 1
done;
if !i + 1 < Array.length vars then Array.sub vars ~pos:0 ~len:(!i + 1) else vars
in
let is_live v =
match Var.Tbl.get live_table v with
| Domain.Dead -> false
| Top | Live _ -> true
in
let zero_var x = if is_live x then x else sentinal in
let zero_instr instr =
match instr with
| Let (x, e) -> (
match e with
| Block (start, vars, is_array, mut) -> (
match Var.Tbl.get live_table x with
| Live fields ->
let vars =
Array.mapi
~f:(fun i v -> if IntMap.mem i fields then v else sentinal)
vars
|> compact_vars
in
let e = Block (start, vars, is_array, mut) in
Let (x, e)
| _ -> instr)
| Apply ap ->
let args = List.map ~f:zero_var ap.args in
Let (x, Apply { ap with args })
| Field (_, _, _) | Closure (_, _, _) | Constant _ | Prim (_, _) | Special _ ->
instr)
| Event _
| Assign (_, _)
| Set_field (_, _, _, _)
| Offset_ref (_, _)
| Array_set (_, _, _) -> instr
in
let zero_block block =
let body = List.map ~f:(fun instr -> zero_instr instr) block.body in
let branch =
match block.branch with
| Return x ->
let live_tc =
match List.last body with
| Some (Let (x', (Apply _ as e))) ->
Code.Var.equal x x' && (is_live x' || not (Pure_fun.pure_expr pure_funs e))
| Some _ | None -> false
in
if live_tc then Return x else Return (zero_var x)
| Raise (_, _)
| Stop | Branch _
| Cond (_, _, _)
| Switch (_, _)
| Pushtrap (_, _, _)
| Poptrap _ -> block.branch
in
{ block with body; branch }
in
let blocks = prog.blocks |> Addr.Map.map zero_block in
{ prog with blocks }
module Print = struct
let rec live_to_string = function
| Domain.Live fields ->
"live { "
^ IntMap.fold
(fun i l s -> s ^ Format.sprintf "%d: %s; " i (live_to_string l))
fields
""
^ "}"
| Top -> "top"
| Dead -> "dead"
let print_uses uses =
Format.eprintf "Usages:\n";
Var.Tbl.iter
(fun v ds ->
Format.eprintf "%a: { " Var.print v;
List.iter
~f:(fun (k, s) ->
Var.Set.iter
(fun d ->
Format.eprintf
"(%a, %s) "
Var.print
d
(match k with
| Compute -> "C"
| Propagate { scope; src } ->
"P("
^ String.concat
~sep:" "
(List.map ~f:(fun x -> Format.asprintf "%a" Var.print x) scope)
^ Format.asprintf "/%a)" Var.print src
| Scope -> "S"))
s)
ds;
Format.eprintf "}\n")
uses
let print_live_tbl live_table =
Format.eprintf "Liveness:\n";
Var.Tbl.iter
(fun v l -> Format.eprintf "%a: %s\n" Var.print v (live_to_string l))
live_table
end
(** Add a sentinal variable declaration to the IR. The fresh variable is assigned to `undefined`. *)
let add_sentinal p sentinal =
let instr = Let (sentinal, Constant (Int Targetint.zero)) in
Code.prepend p [ instr ]
(** Run the liveness analysis and replace dead variables with the given sentinal. *)
let f pure_funs p ~deadcode_sentinal global_info =
Code.invariant p;
let t = Timer.make () in
let p =
match global_info.Global_flow.info_defs.(Var.idx deadcode_sentinal) with
| Expr _ -> p
| _ -> add_sentinal p deadcode_sentinal
in
let defs = definitions p in
let live_table, scoped_live_vars = liveness p pure_funs global_info in
let uses = usages p global_info scoped_live_vars in
let vars = variables uses in
solver vars uses defs live_table scoped_live_vars;
if debug ()
then (
Format.eprintf "Before Zeroing:@.";
Code.Print.program Format.err_formatter (fun _ _ -> "") p;
Print.print_uses uses;
Print.print_live_tbl live_table);
let p = zero p pure_funs deadcode_sentinal live_table in
if debug ()
then (
Format.eprintf "After Zeroing:@.";
Code.Print.program Format.err_formatter (fun _ _ -> "") p);
if times () then Format.eprintf " global dead code elim.: %a@." Timer.print t;
Code.invariant p;
p