Skip to content

Commit 0665cf8

Browse files
committed
Set return variables in CFG simplifcation based on post condition
1 parent 81dce95 commit 0665cf8

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

compiler/entry/jazz2cl.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ let parse_and_print print arch call_conv ecoutput joutput output file funname =
116116
let proc = CL.fun_to_proc (snd prog) (List.nth (snd prog) 0) in
117117
let formals',pre_ghost_instr,post_ghost_instr = CL_vsimpl.GhostVector.unfold_vectors proc.formals proc.ret_vars in
118118
let prog' = pre_ghost_instr @ proc.prog @ post_ghost_instr in
119-
let cfg = CL_vsimpl.Cfg.cfg_of_prog_rev prog' in
120-
let clean_cfg = CL_vsimpl.SimplVector.simpl_cfg cfg proc.ret_vars in
121-
let prog' = CL_vsimpl.Cfg.prog_of_cfg clean_cfg in
122119
let (pre_epred, pre_rpred) = proc.pre in
123120
let (post_epred, post_rpred) = proc.post in
124121
let pre_epred' = CL_vsimpl.GhostVector.unfold_vghosts_epred formals' pre_epred in
125122
let pre_rpred' = CL_vsimpl.GhostVector.unfold_vghosts_rpred formals' pre_rpred in
126123
let post_epred' = CL_vsimpl.GhostVector.unfold_vghosts_epred formals' post_epred in
127124
let post_rpred' = CL_vsimpl.GhostVector.unfold_vghosts_rpred formals' post_rpred in
125+
let ret_vars = CL_vsimpl.SimplVector.get_ret_vars post_epred' post_rpred' in
126+
let cfg = CL_vsimpl.Cfg.cfg_of_prog_rev prog' in
127+
let clean_cfg = CL_vsimpl.SimplVector.simpl_cfg cfg ret_vars in
128+
let prog' = CL_vsimpl.Cfg.prog_of_cfg clean_cfg in
128129
let prog' = CL_vsimpl.GhostVector.unfold_cfg_clauses prog' formals' in
129130
let pre' = (pre_epred', pre_rpred') in
130131
let post' = (post_epred', post_rpred') in

compiler/src/CL_vsimpl.ml

+60
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ end
288288
module SimplVector = struct
289289
open Cfg
290290
open CL.Instr
291+
open CL.R
292+
open CL.I
291293

292294
let rec int_of_ty = function
293295
| CL.Uint n -> n
@@ -576,6 +578,64 @@ module SimplVector = struct
576578
| _ -> h :: remove_nops t
577579
end
578580

581+
let rec get_evars epred =
582+
let rec aux e =
583+
begin
584+
match e with
585+
| Iconst _ -> []
586+
| Ivar v -> [v]
587+
| Iunop (_, e') -> aux e'
588+
| Ibinop (e1, _, e2) ->
589+
(aux e1) @ (aux e2)
590+
| Ilimbs (_, el) -> List.flatten (List.map aux el)
591+
| IUnPack _ -> assert false
592+
end
593+
in
594+
match epred with
595+
| Eeq (e1, e2) ->
596+
let vl1 = aux e1 in
597+
let vl2 = aux e2 in
598+
vl1 @ vl2
599+
| Eeqmod (e1, e2, eps) ->
600+
let vl1 = aux e1 in
601+
let vl2 = aux e2 in
602+
let vl3 = List.flatten (List.map aux eps) in
603+
vl1 @ vl2 @ vl3
604+
605+
let rec get_rvars rpred =
606+
let rec aux e =
607+
begin
608+
match e with
609+
| Rvar v -> [v]
610+
| Rconst _ -> []
611+
| Ruext (e', _) -> aux e'
612+
| Rsext (e', _) -> aux e'
613+
| Runop (_, e') -> aux e'
614+
| Rbinop (e1, _, e2) -> (aux e1) @ (aux e2)
615+
| RVget (v, _) -> [v]
616+
| UnPack _ -> assert false
617+
| Rlimbs (_, el) -> List.flatten (List.map aux el)
618+
end
619+
in
620+
match rpred with
621+
| RPcmp (e1, _, e2) ->
622+
let vl1 = aux e1 in
623+
let vl2 = aux e2 in
624+
vl1 @ vl2
625+
| RPnot e -> get_rvars e
626+
| RPand rps -> List.flatten (List.map get_rvars rps)
627+
| RPor rps -> List.flatten (List.map get_rvars rps)
628+
| RPeqsmod (e1, e2, e3) ->
629+
let vl1 = aux e1 in
630+
let vl2 = aux e2 in
631+
let vl3 = aux e3 in
632+
vl1 @ vl2 @ vl3
633+
634+
let get_ret_vars epreds rpreds =
635+
let epred_rvars = List.flatten (List.map get_evars epreds) in
636+
let rpred_vars = List.flatten (List.map get_rvars rpreds) in
637+
epred_rvars @ rpred_vars (* FIXME: remove dups *)
638+
579639
let simpl_cfg cfg ret_vars =
580640
sr_lvals cfg;
581641
let nI = getPrevI cfg in

compiler/src/CL_vsimpl.mli

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ module GhostVector : sig
1818
end
1919

2020
module SimplVector: sig
21+
val get_ret_vars: CL.I.epred list -> CL.R.rpred list -> CL.tyvar list
2122
val simpl_cfg: Cfg.node -> CL.tyvar list -> Cfg.node
2223
end

0 commit comments

Comments
 (0)