Skip to content

Commit 7743f88

Browse files
committed
updates the Sub.compute_liveness function to handle SSA form
1 parent 1ff7d38 commit 7743f88

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,29 @@ 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 def uses trans = Map.update trans blk ~f:(function
34+
| None -> {
35+
defs = Var.Set.singleton def;
36+
uses;
37+
}
38+
| Some had -> {
39+
defs = Set.add had.defs def;
40+
uses = Set.union had.uses uses
41+
})
42+
3343
let block_transitions sub =
3444
Term.enum blk_t sub |>
3545
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-
})
46+
let init = Map.add_exn fs (Term.tid blk) {
47+
defs = blk_defs blk;
48+
uses = Ir_blk.free_vars blk;
49+
} in
50+
Term.enum phi_t blk |>
51+
Seq.fold ~init ~f:(fun init phi ->
52+
let def = Ir_phi.lhs phi in
53+
Ir_phi.values phi |>
54+
Seq.fold ~init ~f:(fun trans (src,exp) ->
55+
update src def (Exp.free_vars exp) trans)))
4056

4157
let compute_liveness sub =
4258
let g = G.create sub in

0 commit comments

Comments
 (0)