Switching to top instead of bottom for the defined variables

This commit is contained in:
elvis
2024-12-16 16:40:59 +01:00
parent 25f9f12525
commit 1f0d48263a
2 changed files with 46 additions and 16 deletions

View File

@ -18,6 +18,32 @@ module DVCfg = Dataflow.Make (CfgRISC.RISCSimpleStatements) (Variable)
module DVCeltSet = Set.Make(Variable)
let variables (instr : DVCfg.elt) : DVCfg.internal list =
let helper (acc: DVCeltSet.t) (instr: DVCfg.elt) =
match instr with
| Nop ->
acc
| BRegOp (_, r1, r2, r3) ->
DVCeltSet.add r1.index acc |>
DVCeltSet.add r2.index |>
DVCeltSet.add r3.index
| BImmOp (_, r1, _, r3)
| URegOp (_, r1, r3)
| Load (r1, r3)
| Store (r1, r3) ->
DVCeltSet.add r1.index acc |>
DVCeltSet.add r3.index
| LoadI (_, r3) ->
DVCeltSet.add r3.index acc
in
helper DVCeltSet.empty instr |> DVCeltSet.to_list
let variables_all (instructions : DVCfg.elt list) : DVCfg.internal list =
List.fold_left (fun (acc: DVCeltSet.t) (instr: DVCfg.elt) ->
DVCeltSet.union acc (variables instr |> DVCeltSet.of_list)
) DVCeltSet.empty instructions |> DVCeltSet.to_list
let variables_used (instr : DVCfg.elt) : DVCfg.internal list =
let helper (acc: DVCeltSet.t) (instr: DVCfg.elt) =
match instr with
@ -62,21 +88,21 @@ let variables_defined_all (instructions : DVCfg.elt list) : DVCfg.internal list
DVCeltSet.union acc (variables_defined instr |> DVCeltSet.of_list)
) DVCeltSet.empty instructions |> DVCeltSet.to_list
let _variables_defined_nth (instructions : DVCfg.elt list) (i: int) : DVCfg.internal list =
variables_defined (List.nth instructions i)
let _variables_defined_last (instructions : DVCfg.elt list) : DVCfg.internal list =
variables_defined (List.nth instructions ((List.length instructions) - 1))
(* init function, assign the epmpty set to everything *)
let init : (DVCfg.elt list -> DVCfg.internalnode) =
(* init function, assign the bottom to everything *)
let _init_bottom : (DVCfg.elt list -> DVCfg.internalnode) =
(fun l -> {internalin = [];
internalout = [];
internalbetween = (List.init (List.length l) (fun _ -> ([], [])))})
(* init function, assign the top to everything *)
let init_top (all_variables) : (DVCfg.elt list -> DVCfg.internalnode) =
(fun l -> {internalin = all_variables;
internalout = all_variables;
internalbetween = (List.init (List.length l)
(fun _ -> (all_variables, all_variables)))})
(* piece of code that computes vout for the whole block, not used,
use lub below *)
@ -171,8 +197,13 @@ let update (t: DVCfg.t) (node: Cfg.Node.t) : DVCfg.internalnode =
let compute_defined_variables (cfg: RISCCfg.t) : DVCfg.t =
let all_variables = List.fold_left
(fun acc (_, code) -> Utility.unique_union acc (variables_all code))
[]
(Cfg.NodeMap.to_list cfg.content)
in
DVCfg.from_cfg cfg
|> DVCfg.fixed_point ~init:init ~update:update
|> DVCfg.fixed_point ~init:(init_top all_variables) ~update:update