Skip to content

Commit 0f32782

Browse files
authored
updates the Sub.compute_liveness function to handle SSA form (#1445)
1 parent 1ff7d38 commit 0f32782

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/bap/bap.mli

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8529,7 +8529,20 @@ module Std : sig
85298529
whole subroutine is the set of variables that are live at the
85308530
[Graphs.Tid.start] node.
85318531
8532+
When the subroutine is in the SSA form then the phi-nodes have
8533+
the following semantics. For a phi-node at block [b0],
8534+
[x0 := phi([b1,x1; b2,x2;...;bN,xN])], the defined variable
8535+
[x0] is considered to be at the entry of [b0], i.e., [x0] is
8536+
live-in of [b0], and the variable [xi] is live-out of basic
8537+
block [bi], for [i > 0].
8538+
8539+
Informally, a phi-node defines the values on the corresponding
8540+
edges of the predecessors.
8541+
8542+
85328543
@since 2.1
8544+
@since 2.5.0 supports SSA
8545+
@before 2.5.0 the subroutine must not be in the SSA form
85338546
*)
85348547
val compute_liveness : t -> (tid, Var.Set.t) Solution.t
85358548

lib/bap_sema/bap_sema_free_vars.ml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,26 @@ let blk_defs blk =
3030
Seq.fold ~init:Var.Set.empty ~f:(fun defs def ->
3131
Set.add defs (Ir_def.lhs def))
3232

33+
let update blk defs uses trans = Map.update trans blk ~f:(function
34+
| None -> {defs; uses}
35+
| Some had -> {
36+
defs = Set.union had.defs defs;
37+
uses = Set.union had.uses uses
38+
})
39+
3340
let block_transitions sub =
3441
Term.enum blk_t sub |>
3542
Seq.fold ~init:Tid.Map.empty ~f:(fun fs blk ->
36-
Map.add_exn fs (Term.tid blk) {
37-
defs = blk_defs blk;
38-
uses = Ir_blk.free_vars blk;
39-
})
43+
let init = update
44+
(Term.tid blk)
45+
(blk_defs blk)
46+
(Ir_blk.free_vars blk) fs in
47+
Term.enum phi_t blk |>
48+
Seq.fold ~init ~f:(fun init phi ->
49+
let defs = Var.Set.singleton @@ Ir_phi.lhs phi in
50+
Ir_phi.values phi |>
51+
Seq.fold ~init ~f:(fun fs (src,exp) ->
52+
update src defs (Exp.free_vars exp) fs)))
4053

4154
let compute_liveness sub =
4255
let g = G.create sub in

0 commit comments

Comments
 (0)