Proofs of the liveness properties from the liveness checker of RTLpathLivengen.
Require Import Coqlib.
Require Import Maps.
Require Import Lattice.
Require Import AST.
Require Import Op.
Require Import Registers.
Require Import Globalenvs Smallstep RTL RTLpath RTLpathLivegen.
Require Import Bool Errors Linking Values Events.
Require Import Program.
Definition match_prog (
p:
RTL.program) (
tp:
program) :=
match_program (
fun _ f tf =>
transf_fundef f =
OK tf)
eq p tp.
Lemma transf_program_match:
forall prog tprog,
transf_program prog =
OK tprog ->
match_prog prog tprog.
Proof.
Section PRESERVATION.
Variables prog:
RTL.program.
Variables tprog:
program.
Hypothesis TRANSL:
match_prog prog tprog.
Let ge :=
Genv.globalenv prog.
Let tpge :=
Genv.globalenv tprog.
Let tge :=
Genv.globalenv (
RTLpath.transf_program tprog).
Lemma symbols_preserved s:
Genv.find_symbol tge s =
Genv.find_symbol ge s.
Proof.
Lemma senv_transitivity x y z:
Senv.equiv x y ->
Senv.equiv y z ->
Senv.equiv x z.
Proof.
Lemma senv_preserved:
Senv.equiv ge tge.
Proof.
Lemma function_ptr_preserved v f:
Genv.find_funct_ptr ge v =
Some f ->
exists tf,
Genv.find_funct_ptr tpge v =
Some tf /\
transf_fundef f =
OK tf.
Proof.
Lemma function_ptr_RTL_preserved v f:
Genv.find_funct_ptr ge v =
Some f ->
Genv.find_funct_ptr tge v =
Some f.
Proof.
Lemma find_function_preserved ros rs fd:
RTL.find_function ge ros rs =
Some fd ->
RTL.find_function tge ros rs =
Some fd.
Proof.
Local Hint Resolve symbols_preserved senv_preserved:
core.
Lemma transf_program_RTL_correct:
forward_simulation (
RTL.semantics prog) (
RTL.semantics (
RTLpath.transf_program tprog)).
Proof.
Theorem transf_program_correct:
forward_simulation (
RTL.semantics prog) (
RTLpath.semantics tprog).
Proof.
Theorem all_fundef_liveness_ok b f:
Genv.find_funct_ptr tpge b =
Some f ->
liveness_ok_fundef f.
Proof.
End PRESERVATION.
Local Open Scope lazy_bool_scope.
Local Open Scope option_monad_scope.
Local Notation ext alive := (
fun r =>
Regset.In r alive).
Lemma regset_add_spec live r1 r2:
Regset.In r1 (
Regset.add r2 live) <-> (
r1 =
r2 \/
Regset.In r1 live).
Proof.
Definition eqlive_reg (
alive:
Regset.elt ->
Prop) (
rs1 rs2:
regset):
Prop :=
forall r, (
alive r) ->
rs1#
r =
rs2#
r.
Lemma eqlive_reg_refl alive rs:
eqlive_reg alive rs rs.
Proof.
Lemma eqlive_reg_symmetry alive rs1 rs2:
eqlive_reg alive rs1 rs2 ->
eqlive_reg alive rs2 rs1.
Proof.
Lemma eqlive_reg_trans alive rs1 rs2 rs3:
eqlive_reg alive rs1 rs2 ->
eqlive_reg alive rs2 rs3 ->
eqlive_reg alive rs1 rs3.
Proof.
unfold eqlive_reg;
intros H0 H1 r H.
rewrite H0;
eauto.
Qed.
Lemma eqlive_reg_update (
alive:
Regset.elt ->
Prop)
rs1 rs2 r v:
eqlive_reg (
fun r1 =>
r1 <>
r /\
alive r1)
rs1 rs2 ->
eqlive_reg alive (
rs1 #
r <-
v) (
rs2 #
r <-
v).
Proof.
Lemma eqlive_reg_monotonic (
alive1 alive2:
Regset.elt ->
Prop)
rs1 rs2:
eqlive_reg alive2 rs1 rs2 -> (
forall r,
alive1 r ->
alive2 r) ->
eqlive_reg alive1 rs1 rs2.
Proof.
Lemma eqlive_reg_triv rs1 rs2: (
forall r,
rs1#
r =
rs2#
r) <->
eqlive_reg (
fun _ =>
True)
rs1 rs2.
Proof.
Lemma eqlive_reg_triv_trans alive rs1 rs2 rs3:
eqlive_reg alive rs1 rs2 -> (
forall r,
rs2#
r =
rs3#
r) ->
eqlive_reg alive rs1 rs3.
Proof.
Local Hint Resolve Regset.mem_2 Regset.subset_2:
core.
Lemma lazy_and_true (
b1 b2:
bool):
b1 &&&
b2 =
true <->
b1 =
true /\
b2 =
true.
Proof.
destruct b1; simpl; intuition.
Qed.
Lemma list_mem_correct (
rl:
list reg) (
alive:
Regset.t):
list_mem rl alive =
true ->
forall r,
List.In r rl ->
ext alive r.
Proof.
induction rl;
simpl;
try rewrite lazy_and_true;
intuition subst;
auto.
Qed.
Lemma eqlive_reg_list (
alive:
Regset.elt ->
Prop)
args rs1 rs2:
eqlive_reg alive rs1 rs2 -> (
forall r,
List.In r args -> (
alive r)) ->
rs1##
args =
rs2##
args.
Proof.
induction args;
simpl;
auto.
intros EQLIVE ALIVE;
rewrite IHargs;
auto.
unfold eqlive_reg in EQLIVE.
rewrite EQLIVE;
auto.
Qed.
Lemma eqlive_reg_listmem (
alive:
Regset.t)
args rs1 rs2:
eqlive_reg (
ext alive)
rs1 rs2 ->
list_mem args alive =
true ->
rs1##
args =
rs2##
args.
Proof.
Record eqlive_istate alive (
st1 st2:
istate):
Prop :=
{
eqlive_continue:
icontinue st1 =
icontinue st2;
eqlive_ipc:
ipc st1 =
ipc st2;
eqlive_irs:
eqlive_reg alive (
irs st1) (
irs st2);
eqlive_imem: (
imem st1) = (
imem st2) }.
Lemma iinst_checker_eqlive ge sp pm alive i res rs1 rs2 m st1:
eqlive_reg (
ext alive)
rs1 rs2 ->
iinst_checker pm alive i =
Some res ->
istep ge i sp rs1 m =
Some st1 ->
exists st2,
istep ge i sp rs2 m =
Some st2 /\
eqlive_istate (
ext (
fst res))
st1 st2.
Proof.
intros EQLIVE.
destruct i;
simpl;
try_simplify_someHyps.
-
repeat (
econstructor;
eauto).
-
inversion_ASSERT;
try_simplify_someHyps.
inversion_SOME v.
intros EVAL.
erewrite <-
eqlive_reg_listmem;
eauto.
try_simplify_someHyps.
repeat (
econstructor;
simpl;
eauto).
eapply eqlive_reg_update.
eapply eqlive_reg_monotonic;
eauto.
intros r0;
rewrite regset_add_spec.
intuition.
-
inversion_ASSERT;
try_simplify_someHyps.
destruct t.
inversion_SOME a0.
intros EVAL.
erewrite <-
eqlive_reg_listmem;
eauto.
try_simplify_someHyps.
inversion_SOME v;
try_simplify_someHyps.
repeat (
econstructor;
simpl;
eauto).
2:
erewrite <- (
eqlive_reg_listmem _ _ rs1 rs2);
eauto;
destruct (
eval_addressing _ _ _ _);
try destruct (
Memory.Mem.loadv _ _ _);
try (
intros;
inv H1;
repeat (
econstructor;
simpl;
eauto)).
all:
eapply eqlive_reg_update;
eapply eqlive_reg_monotonic;
eauto;
intros r0;
rewrite regset_add_spec;
intuition.
-
(
repeat inversion_ASSERT);
try_simplify_someHyps.
inversion_SOME a0.
intros EVAL.
erewrite <-
eqlive_reg_listmem;
eauto.
rewrite <- (
EQLIVE r);
auto.
inversion_SOME v;
try_simplify_someHyps.
try_simplify_someHyps.
repeat (
econstructor;
simpl;
eauto).
-
inversion_ASSERT.
inversion_SOME b.
intros EVAL.
intros ARGS;
erewrite <-
eqlive_reg_listmem;
eauto.
try_simplify_someHyps.
repeat (
econstructor;
simpl;
eauto).
exploit exit_checker_res;
eauto.
intro;
subst;
simpl.
auto.
Qed.
Lemma iinst_checker_istep_continue ge sp pm alive i res rs m st:
iinst_checker pm alive i =
Some res ->
istep ge i sp rs m =
Some st ->
icontinue st =
true ->
(
snd res)=(
ipc st).
Proof.
Lemma exit_checker_eqlive A (
pm:
path_map) (
alive:
Regset.t) (
pc:
node) (
v:
A)
res rs1 rs2:
exit_checker pm alive pc v =
Some res ->
eqlive_reg (
ext alive)
rs1 rs2 ->
exists path,
pm!
pc =
Some path /\
eqlive_reg (
ext path.(
input_regs))
rs1 rs2.
Proof.
Lemma iinst_checker_eqlive_stopped ge sp pm alive i res rs1 rs2 m st1:
eqlive_reg (
ext alive)
rs1 rs2 ->
istep ge i sp rs1 m =
Some st1 ->
iinst_checker pm alive i =
Some res ->
icontinue st1 =
false ->
exists path st2,
pm!(
ipc st1) =
Some path /\
istep ge i sp rs2 m =
Some st2 /\
eqlive_istate (
ext path.(
input_regs))
st1 st2.
Proof.
intros EQLIVE.
set (
tmp :=
istep ge i sp rs2).
destruct i;
simpl;
try_simplify_someHyps;
repeat (
inversion_ASSERT ||
inversion_SOME b);
try_simplify_someHyps;
try congruence.
1-3:
explore_destruct;
simpl;
try_simplify_someHyps;
repeat (
inversion_ASSERT ||
inversion_SOME b);
try_simplify_someHyps;
try congruence.
unfold tmp;
clear tmp;
simpl.
intros EVAL;
erewrite <-
eqlive_reg_listmem;
eauto.
try_simplify_someHyps.
destruct b eqn:
EQb;
simpl in * |-;
try congruence.
intros;
exploit exit_checker_eqlive;
eauto.
intros (
path &
PATH &
EQLIVE2).
repeat (
econstructor;
simpl;
eauto).
Qed.
Lemma ipath_checker_eqlive_normal ge ps (
f:
function)
sp pm:
forall alive pc res rs1 rs2 m st1,
eqlive_reg (
ext alive)
rs1 rs2 ->
ipath_checker ps f pm alive pc =
Some res ->
isteps ge ps f sp rs1 m pc =
Some st1 ->
icontinue st1 =
true ->
exists st2,
isteps ge ps f sp rs2 m pc =
Some st2 /\
eqlive_istate (
ext (
fst res))
st1 st2.
Proof.
induction ps as [|
ps];
simpl;
try_simplify_someHyps.
-
repeat (
econstructor;
simpl;
eauto).
-
inversion_SOME i;
try_simplify_someHyps.
inversion_SOME res0.
inversion_SOME st0.
intros.
exploit iinst_checker_eqlive;
eauto.
destruct 1
as (
st2 &
ISTEP & [
CONT PC RS MEM]).
try_simplify_someHyps.
rewrite <-
CONT, <-
MEM, <-
PC.
destruct (
icontinue st0)
eqn:
CONT'.
*
intros;
exploit iinst_checker_istep_continue;
eauto.
rewrite <-
PC;
intros X;
rewrite X in * |-.
eauto.
*
try_simplify_someHyps.
congruence.
Qed.
Lemma ipath_checker_isteps_continue ge ps (
f:
function)
sp pm:
forall alive pc res rs m st,
ipath_checker ps f pm alive pc =
Some res ->
isteps ge ps f sp rs m pc =
Some st ->
icontinue st =
true ->
(
snd res)=(
ipc st).
Proof.
induction ps as [|
ps];
simpl;
try_simplify_someHyps.
inversion_SOME i;
try_simplify_someHyps.
inversion_SOME res0.
inversion_SOME st0.
destruct (
icontinue st0)
eqn:
CONT'.
-
intros;
exploit iinst_checker_istep_continue;
eauto.
intros EQ;
rewrite EQ in * |-;
clear EQ;
eauto.
-
try_simplify_someHyps;
congruence.
Qed.
Lemma ipath_checker_eqlive_stopped ge ps (
f:
function)
sp pm:
forall alive pc res rs1 rs2 m st1,
eqlive_reg (
ext alive)
rs1 rs2 ->
ipath_checker ps f pm alive pc =
Some res ->
isteps ge ps f sp rs1 m pc =
Some st1 ->
icontinue st1 =
false ->
exists path st2,
pm!(
ipc st1) =
Some path /\
isteps ge ps f sp rs2 m pc =
Some st2 /\
eqlive_istate (
ext path.(
input_regs))
st1 st2.
Proof.
induction ps as [|
ps];
simpl;
try_simplify_someHyps;
try congruence.
inversion_SOME i;
try_simplify_someHyps.
inversion_SOME res0.
inversion_SOME st0.
intros.
destruct (
icontinue st0)
eqn:
CONT';
try_simplify_someHyps;
intros.
*
intros;
exploit iinst_checker_eqlive;
eauto.
destruct 1
as (
st2 &
ISTEP & [
CONT PC RS MEM]).
exploit iinst_checker_istep_continue;
eauto.
intros PC'.
try_simplify_someHyps.
rewrite PC', <-
CONT, <-
MEM, <-
PC,
CONT'.
eauto.
*
intros;
exploit iinst_checker_eqlive_stopped;
eauto.
intros EQLIVE;
generalize EQLIVE;
destruct 1
as (
path &
st2 &
PATH &
ISTEP & [
CONT PC RS MEM]).
try_simplify_someHyps.
rewrite <-
CONT, <-
MEM, <-
PC,
CONT'.
try_simplify_someHyps.
Qed.
Inductive eqlive_stackframes:
stackframe ->
stackframe ->
Prop :=
|
eqlive_stackframes_intro path res f sp pc rs1 rs2
(
LIVE:
liveness_ok_function f)
(
PATH:
f.(
fn_path)!
pc =
Some path)
(
EQUIV:
forall v,
eqlive_reg (
ext path.(
input_regs)) (
rs1 #
res <-
v) (
rs2 #
res <-
v)):
eqlive_stackframes (
Stackframe res f sp pc rs1) (
Stackframe res f sp pc rs2).
Inductive eqlive_states:
state ->
state ->
Prop :=
|
eqlive_states_intro
path st1 st2 f sp pc rs1 rs2 m
(
STACKS:
list_forall2 eqlive_stackframes st1 st2)
(
LIVE:
liveness_ok_function f)
(
PATH:
f.(
fn_path)!
pc =
Some path)
(
EQUIV:
eqlive_reg (
ext path.(
input_regs))
rs1 rs2):
eqlive_states (
State st1 f sp pc rs1 m) (
State st2 f sp pc rs2 m)
|
eqlive_states_call st1 st2 f args m
(
LIVE:
liveness_ok_fundef f)
(
STACKS:
list_forall2 eqlive_stackframes st1 st2):
eqlive_states (
Callstate st1 f args m) (
Callstate st2 f args m)
|
eqlive_states_return st1 st2 v m
(
STACKS:
list_forall2 eqlive_stackframes st1 st2):
eqlive_states (
Returnstate st1 v m) (
Returnstate st2 v m).
Section LivenessProperties.
Variable prog:
program.
Let pge :=
Genv.globalenv prog.
Let ge :=
Genv.globalenv (
RTLpath.transf_program prog).
Hypothesis all_fundef_liveness_ok:
forall b f,
Genv.find_funct_ptr pge b =
Some f ->
liveness_ok_fundef f.
Lemma find_funct_liveness_ok v fd:
Genv.find_funct pge v =
Some fd ->
liveness_ok_fundef fd.
Proof.
Lemma find_function_liveness_ok ros rs f:
find_function pge ros rs =
Some f ->
liveness_ok_fundef f.
Proof.
Lemma find_function_eqlive alive ros rs1 rs2:
eqlive_reg (
ext alive)
rs1 rs2 ->
reg_sum_mem ros alive =
true ->
find_function pge ros rs1 =
find_function pge ros rs2.
Proof.
intros EQLIVE.
destruct ros; simpl; auto.
intros H; erewrite (EQLIVE r); eauto.
Qed.
Lemma final_inst_checker_from_iinst_checker i sp rs m st pm alive por:
istep ge i sp rs m =
Some st ->
final_inst_checker pm alive por i =
None.
Proof.
destruct i; simpl; try congruence.
Qed.
Lemma exit_checker_eqlive_ext1 (
pm:
path_map) (
alive:
Regset.t) (
pc:
node)
r rs1 rs2:
exit_checker pm (
Regset.add r alive)
pc tt =
Some tt ->
eqlive_reg (
ext alive)
rs1 rs2 ->
exists path,
pm!
pc =
Some path /\ (
forall v,
eqlive_reg (
ext path.(
input_regs)) (
rs1 #
r <-
v) (
rs2 #
r <-
v)).
Proof.
Local Hint Resolve in_or_app:
local.
Lemma eqlive_eval_builtin_args alive rs1 rs2 sp m args vargs:
eqlive_reg alive rs1 rs2 ->
Events.eval_builtin_args ge (
fun r =>
rs1 #
r)
sp m args vargs ->
(
forall r,
List.In r (
params_of_builtin_args args) ->
alive r) ->
Events.eval_builtin_args ge (
fun r =>
rs2 #
r)
sp m args vargs.
Proof.
unfold Events.eval_builtin_args.
intros EQLIVE;
induction 1
as [|
a1 al b1 bl EVAL1 EVALL];
simpl.
{
econstructor;
eauto. }
intro X.
assert (
X1:
eqlive_reg (
fun r =>
In r (
params_of_builtin_arg a1))
rs1 rs2).
{
eapply eqlive_reg_monotonic;
eauto with local. }
lapply IHEVALL;
eauto with local.
clear X IHEVALL;
intro X.
econstructor;
eauto.
generalize X1;
clear EVALL X1 X.
induction EVAL1;
simpl;
try (
econstructor;
eauto;
fail).
-
intros X1;
erewrite X1; [
econstructor;
eauto |
eauto ].
-
intros;
econstructor.
+
eapply IHEVAL1_1;
eauto.
eapply eqlive_reg_monotonic;
eauto.
simpl;
intros;
eauto with local.
+
eapply IHEVAL1_2;
eauto.
eapply eqlive_reg_monotonic;
eauto.
simpl;
intros;
eauto with local.
-
intros;
econstructor.
+
eapply IHEVAL1_1;
eauto.
eapply eqlive_reg_monotonic;
eauto.
simpl;
intros;
eauto with local.
+
eapply IHEVAL1_2;
eauto.
eapply eqlive_reg_monotonic;
eauto.
simpl;
intros;
eauto with local.
Qed.
Lemma exit_checker_eqlive_builtin_res (
pm:
path_map) (
alive:
Regset.t) (
pc:
node)
rs1 rs2 (
res:
builtin_res reg):
exit_checker pm (
reg_builtin_res res alive)
pc tt =
Some tt ->
eqlive_reg (
ext alive)
rs1 rs2 ->
exists path,
pm!
pc =
Some path /\ (
forall vres,
eqlive_reg (
ext path.(
input_regs)) (
regmap_setres res vres rs1) (
regmap_setres res vres rs2)).
Proof.
Lemma exit_list_checker_eqlive (
pm:
path_map) (
alive:
Regset.t) (
tbl:
list node)
rs1 rs2 pc:
forall n,
exit_list_checker pm alive tbl =
true ->
eqlive_reg (
ext alive)
rs1 rs2 ->
list_nth_z tbl n =
Some pc ->
exists path,
pm!
pc =
Some path /\
eqlive_reg (
ext path.(
input_regs))
rs1 rs2.
Proof.
induction tbl;
simpl.
-
intros;
try congruence.
-
intros n;
rewrite lazy_and_Some_tt_true;
destruct (
zeq n 0)
eqn:
Hn.
*
try_simplify_someHyps;
intuition.
exploit exit_checker_eqlive;
eauto.
*
intuition.
eapply IHtbl;
eauto.
Qed.
Lemma final_inst_checker_eqlive (
f:
function)
sp alive por pc i rs1 rs2 m stk1 stk2 t s1:
list_forall2 eqlive_stackframes stk1 stk2 ->
eqlive_reg (
ext alive)
rs1 rs2 ->
Regset.Subset por alive ->
liveness_ok_function f ->
(
fn_code f) !
pc =
Some i ->
path_last_step ge pge stk1 f sp pc rs1 m t s1 ->
final_inst_checker (
fn_path f)
alive por i =
Some tt ->
exists s2,
path_last_step ge pge stk2 f sp pc rs2 m t s2 /\
eqlive_states s1 s2.
Proof.
Lemma inst_checker_eqlive (
f:
function)
sp alive por pc i rs1 rs2 m stk1 stk2 t s1:
list_forall2 eqlive_stackframes stk1 stk2 ->
eqlive_reg (
ext alive)
rs1 rs2 ->
liveness_ok_function f ->
(
fn_code f) !
pc =
Some i ->
path_last_step ge pge stk1 f sp pc rs1 m t s1 ->
inst_checker (
fn_path f)
alive por i =
Some tt ->
exists s2,
path_last_step ge pge stk2 f sp pc rs2 m t s2 /\
eqlive_states s1 s2.
Proof.
Lemma path_step_eqlive path stk1 f sp rs1 m pc t s1 stk2 rs2:
path_step ge pge (
psize path)
stk1 f sp rs1 m pc t s1 ->
list_forall2 eqlive_stackframes stk1 stk2 ->
eqlive_reg (
ext (
input_regs path))
rs1 rs2 ->
liveness_ok_function f ->
(
fn_path f) !
pc =
Some path ->
exists s2,
path_step ge pge (
psize path)
stk2 f sp rs2 m pc t s2 /\
eqlive_states s1 s2.
Proof.
Theorem step_eqlive t s1 s1'
s2:
step ge pge s1 t s1' ->
eqlive_states s1 s2 ->
exists s2',
step ge pge s2 t s2' /\
eqlive_states s1'
s2'.
Proof.
End LivenessProperties.