Elimination of unneeded computations over RTL: correctness proof.
Require Import Coqlib.
Require Import Errors.
Require Import Maps.
Require Import IntvSets.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import ExprEval Values_symbolictype Values_symbolic Normalise NormaliseSpec
Memory.
Require Import MemRel.
Require Import Globalenvs.
Require Import Events.
Require Import Smallstep.
Require Import Op.
Require Import Registers.
Require Import RTL.
Require Import Lattice.
Require Import Kildall.
Require Import ValueDomain.
Require Import ValueAnalysis.
Require Import NeedDomain.
Require Import NeedOp.
Require Import Deadcode.
Require Import Tactics.
Relating the memory states
The magree predicate is a variant of Mem.extends where we
allow the contents of the two memory states to differ arbitrarily
on some locations. The predicate P is true on the locations whose
contents must be in the lessdef relation.
Definition locset :=
block ->
Z ->
Prop.
Record magree (
m1 m2:
mem) (
P:
locset) :
Prop :=
mk_magree {
ma_access:
forall b, (
Mem.mem_access m1) #
b = (
Mem.mem_access m2) #
b;
ma_bs:
forall b,
(
Mem.mem_blocksize m1) #
b = (
Mem.mem_blocksize m2) #
b;
ma_bm:
forall b,
(
Mem.mem_mask m1) #
b = (
Mem.mem_mask m2) #
b;
ma_bt:
forall b,
(
Mem.mem_blocktype m1) #
b = (
Mem.mem_blocktype m2) #
b;
ma_memval:
forall b ofs,
Mem.perm m1 b ofs Cur Readable ->
P b ofs ->
memval_lessdef (
ZMap.get ofs (
PMap.get b (
Mem.mem_contents m1)))
(
ZMap.get ofs (
PMap.get b (
Mem.mem_contents m2)));
ma_nextblock:
Mem.nextblock m2 =
Mem.nextblock m1;
ma_nb_extra:
Mem.nb_extra m2 =
Mem.nb_extra m1
}.
Lemma ma_perm:
forall m1 m2 P,
magree m1 m2 P ->
forall b ofs k p,
Mem.perm m1 b ofs k p ->
Mem.perm m2 b ofs k p.
Proof.
unfold Mem.perm;
intros.
inv H.
rewrite ma_access0 in H0;
auto.
Qed.
Lemma magree_monotone:
forall m1 m2 (
P Q:
locset),
magree m1 m2 P ->
(
forall b ofs,
Q b ofs ->
P b ofs) ->
magree m1 m2 Q.
Proof.
intros. destruct H. constructor; auto.
Qed.
Lemma mextends_agree:
forall m1 m2 P,
mem_rel Val.lessdef m1 m2 ->
magree m1 m2 P.
Proof.
intros. inv H. constructor; intros; auto.
apply mem_contents_eq.
auto.
Qed.
Lemma magree_extends:
forall m1 m2 (
P:
locset),
(
forall b ofs,
P b ofs) ->
magree m1 m2 P ->
mem_rel Val.lessdef m1 m2.
Proof.
intros. destruct H0. constructor; auto.
- intros. apply ma_memval0; auto.
Qed.
Lemma magree_loadbytes:
forall m1 m2 P b ofs n bytes,
magree m1 m2 P ->
Mem.loadbytes m1 b ofs n =
Some bytes ->
(
forall i,
ofs <=
i <
ofs +
n ->
P b i) ->
exists bytes',
Mem.loadbytes m2 b ofs n =
Some bytes' /\
list_forall2 memval_lessdef bytes bytes'.
Proof.
Lemma magree_load:
forall m1 m2 P chunk b ofs v,
magree m1 m2 P ->
Mem.load chunk m1 b ofs =
Some v ->
(
forall i,
ofs <=
i <
ofs +
size_chunk chunk ->
P b i) ->
exists v',
Mem.load chunk m2 b ofs =
Some v' /\
Val.lessdef v v'.
Proof.
Lemma magree_storebytes_parallel:
forall m1 m2 (
P Q:
locset)
b ofs bytes1 m1'
bytes2,
magree m1 m2 P ->
Mem.storebytes m1 b ofs bytes1 =
Some m1' ->
(
forall b'
i,
Q b'
i ->
b' <>
b \/
i <
ofs \/
ofs +
Z_of_nat (
length bytes1) <=
i ->
P b'
i) ->
list_forall2 memval_lessdef bytes1 bytes2 ->
exists m2',
Mem.storebytes m2 b ofs bytes2 =
Some m2' /\
magree m1'
m2'
Q.
Proof.
Lemma magree_store_parallel:
forall m1 m2 (
P Q:
locset)
chunk b ofs v1 m1'
v2,
magree m1 m2 P ->
Mem.store chunk m1 b ofs v1 =
Some m1' ->
vagree v1 v2 (
store_argument chunk) ->
(
forall b'
i,
Q b'
i ->
b' <>
b \/
i <
ofs \/
ofs +
size_chunk chunk <=
i ->
P b'
i) ->
exists m2',
Mem.store chunk m2 b ofs v2 =
Some m2' /\
magree m1'
m2'
Q.
Proof.
Lemma magree_storebytes_left:
forall m1 m2 P b ofs bytes1 m1',
magree m1 m2 P ->
Mem.storebytes m1 b ofs bytes1 =
Some m1' ->
(
forall i,
ofs <=
i <
ofs +
Z_of_nat (
length bytes1) -> ~(
P b i)) ->
magree m1'
m2 P.
Proof.
Lemma magree_store_left:
forall m1 m2 P chunk b ofs v1 m1',
magree m1 m2 P ->
Mem.store chunk m1 b ofs v1 =
Some m1' ->
(
forall i,
ofs <=
i <
ofs +
size_chunk chunk -> ~(
P b i)) ->
magree m1'
m2 P.
Proof.
Lemma magree_unchecked_free:
forall m1 m2 (
P :
locset)
m1'
n,
MemReserve.release_boxes m1 n =
Some m1' ->
magree m1 m2 P ->
exists m2',
MemReserve.release_boxes m2 n =
Some m2' /\
magree m1'
m2'
P.
Proof.
intros m1 m2 P m1'
n REL H.
unfold MemReserve.release_boxes in *.
destr_in REL.
inv REL.
destr.
eexists;
split;
eauto.
inversion H.
constructor;
intros;
simpl;
auto.
exfalso.
erewrite ma_nb_extra in n0;
eauto.
Qed.
Lemma magree_free:
forall m1 m2 (
P Q:
locset)
b lo hi m1',
magree m1 m2 P ->
Mem.free m1 b lo hi =
Some m1' ->
(
forall b'
i,
Q b'
i ->
b' <>
b \/ ~(
lo <=
i <
hi) ->
P b'
i) ->
exists m2',
Mem.free m2 b lo hi =
Some m2' /\
magree m1'
m2'
Q.
Proof.
Properties of the need environment
Lemma add_need_all_eagree:
forall e e'
r ne,
eagree e e' (
add_need_all r ne) ->
eagree e e'
ne.
Proof.
Lemma add_need_all_lessdef:
forall e e'
r ne,
eagree e e' (
add_need_all r ne) ->
Val.lessdef e#
r e'#
r.
Proof.
Lemma add_need_eagree:
forall e e'
r nv ne,
eagree e e' (
add_need r nv ne) ->
eagree e e'
ne.
Proof.
Lemma add_need_vagree:
forall e e'
r nv ne,
eagree e e' (
add_need r nv ne) ->
vagree e#
r e'#
r nv.
Proof.
Lemma add_needs_all_eagree:
forall rl e e'
ne,
eagree e e' (
add_needs_all rl ne) ->
eagree e e'
ne.
Proof.
Lemma add_needs_all_lessdef:
forall rl e e'
ne,
eagree e e' (
add_needs_all rl ne) ->
Val.lessdef_list e##
rl e'##
rl.
Proof.
Lemma add_needs_eagree:
forall rl nvl e e'
ne,
eagree e e' (
add_needs rl nvl ne) ->
eagree e e'
ne.
Proof.
Lemma add_needs_vagree:
forall rl nvl e e'
ne,
eagree e e' (
add_needs rl nvl ne) ->
vagree_list e##
rl e'##
rl nvl.
Proof.
Lemma add_ros_need_eagree:
forall e e'
ros ne,
eagree e e' (
add_ros_need_all ros ne) ->
eagree e e'
ne.
Proof.
Hint Resolve add_need_all_eagree add_need_all_lessdef
add_need_eagree add_need_vagree
add_needs_all_eagree add_needs_all_lessdef
add_needs_eagree add_needs_vagree
add_ros_need_eagree:
na.
Lemma eagree_init_regs:
forall rl vl1 vl2 ne,
Val.lessdef_list vl1 vl2 ->
eagree (
init_regs vl1 rl) (
init_regs vl2 rl)
ne.
Proof.
induction rl;
intros until ne;
intros LD;
simpl.
-
red;
auto with na.
-
inv LD.
+
red;
auto with na.
+
apply eagree_update;
auto with na.
Qed.
Basic properties of the translation
Section PRESERVATION.
Variable prog:
program.
Variable tprog:
program.
Hypothesis TRANSF:
transf_program prog =
OK tprog.
Let ge :=
Genv.globalenv prog.
Let tge :=
Genv.globalenv tprog.
Let rm :=
romem_for_program prog.
Lemma symbols_preserved:
forall (
s:
ident),
Genv.find_symbol tge s =
Genv.find_symbol ge s.
Proof.
Lemma varinfo_preserved:
forall b,
Genv.find_var_info tge b =
Genv.find_var_info ge b.
Proof.
Lemma functions_translated:
forall m (
v:
expr_sym) (
f:
RTL.fundef),
Genv.find_funct m ge v =
Some f ->
exists tf,
Genv.find_funct m tge v =
Some tf /\
transf_fundef rm f =
OK tf.
Proof (
Genv.find_funct_transf_partial (
transf_fundef rm)
_ TRANSF).
Lemma function_ptr_translated:
forall (
b:
block) (
f:
RTL.fundef),
Genv.find_funct_ptr ge b =
Some f ->
exists tf,
Genv.find_funct_ptr tge b =
Some tf /\
transf_fundef rm f =
OK tf.
Proof (
Genv.find_funct_ptr_transf_partial (
transf_fundef rm)
_ TRANSF).
Lemma sig_function_translated:
forall f tf,
transf_fundef rm f =
OK tf ->
funsig tf =
funsig f.
Proof.
Lemma stacksize_translated:
forall f tf,
transf_function rm f =
OK tf ->
tf.(
fn_stacksize) =
f.(
fn_stacksize).
Proof.
Lemma transf_function_at:
forall f tf an pc instr,
transf_function rm f =
OK tf ->
analyze (
vanalyze rm f)
f =
Some an ->
f.(
fn_code)!
pc =
Some instr ->
tf.(
fn_code)!
pc =
Some(
transf_instr (
vanalyze rm f)
an pc instr).
Proof.
Lemma is_dead_sound_1:
forall nv,
is_dead nv =
true ->
nv =
Nothing.
Proof.
destruct nv; simpl; congruence.
Qed.
Lemma is_dead_sound_2:
forall nv,
is_dead nv =
false ->
nv <>
Nothing.
Proof.
intros; red; intros. subst nv; discriminate.
Qed.
Hint Resolve is_dead_sound_1 is_dead_sound_2:
na.
Lemma is_int_zero_sound:
forall nv,
is_int_zero nv =
true ->
nv =
I Int.zero.
Proof.
Lemma find_function_translated:
forall m tm ros rs fd trs ne,
find_function ge m ros rs =
Some fd ->
mem_lessdef m tm ->
eagree rs trs (
add_ros_need_all ros ne) ->
exists tfd,
find_function tge tm ros trs =
Some tfd /\
transf_fundef rm fd =
OK tfd.
Proof.
Semantic invariant
Inductive match_stackframes:
stackframe ->
stackframe ->
Prop :=
|
match_stackframes_intro:
forall res f sp pc e tf te an
(
FUN:
transf_function rm f =
OK tf)
(
ANL:
analyze (
vanalyze rm f)
f =
Some an)
(
RES:
forall v tv,
Val.lessdef v tv ->
eagree (
e#
res <-
v) (
te#
res<-
tv)
(
fst (
transfer f (
vanalyze rm f)
pc an!!
pc))),
match_stackframes (
Stackframe res f sp pc e)
(
Stackframe res tf sp pc te).
Inductive match_states:
state ->
state ->
Prop :=
|
match_regular_states:
forall s f sp pc e m ts tf te tm an
(
STACKS:
list_forall2 (
match_stackframes)
s ts)
(
FUN:
transf_function rm f =
OK tf)
(
ANL:
analyze (
vanalyze rm f)
f =
Some an)
(
ENV:
eagree e te (
fst (
transfer f (
vanalyze rm f)
pc an!!
pc)))
(
MEM:
magree m tm (
nlive ge sp (
snd (
transfer f (
vanalyze rm f)
pc an!!
pc)))),
match_states (
State s f sp pc e m)
(
State ts tf sp pc te tm)
|
match_call_states:
forall s f args m ts tf targs tm
(
STACKS:
list_forall2 (
match_stackframes)
s ts)
(
FUN:
transf_fundef rm f =
OK tf)
(
ARGS:
Val.lessdef_list args targs)
(
MEM:
mem_lessdef m tm),
match_states (
Callstate s f args m)
(
Callstate ts tf targs tm)
|
match_return_states:
forall s v m ts tv tm
(
STACKS:
list_forall2 (
match_stackframes)
s ts)
(
RES:
Val.lessdef v tv)
(
MEM:
mem_lessdef m tm),
match_states (
Returnstate s v m)
(
Returnstate ts tv tm).
match_states and CFG successors
Lemma analyze_successors:
forall f an pc instr pc',
analyze (
vanalyze rm f)
f =
Some an ->
f.(
fn_code)!
pc =
Some instr ->
In pc' (
successors_instr instr) ->
NA.ge an!!
pc (
transfer f (
vanalyze rm f)
pc'
an!!
pc').
Proof.
Lemma match_succ_states:
forall s f sp pc e m ts tf te tm an pc'
instr ne nm
(
STACKS:
list_forall2 (
match_stackframes)
s ts)
(
FUN:
transf_function rm f =
OK tf)
(
ANL:
analyze (
vanalyze rm f)
f =
Some an)
(
INSTR:
f.(
fn_code)!
pc =
Some instr)
(
SUCC:
In pc' (
successors_instr instr))
(
ANPC:
an!!
pc = (
ne,
nm))
(
ENV:
eagree e te ne)
(
MEM:
magree m tm (
nlive ge sp nm)),
match_states (
State s f sp pc'
e m)
(
State ts tf sp pc'
te tm).
Proof.
Properties of volatile memory accesses
Lemma compat_magree:
forall P m m'
(
MR:
magree m m'
P)
cm,
Mem.compat_m m Mem.M32 cm <->
Mem.compat_m m'
Mem.M32 cm.
Proof.
Lemma is_norm_m_rel:
forall P m m'
e v
(
MR:
magree m m'
P),
Mem.is_norm_m m e v <->
Mem.is_norm_m m'
e v.
Proof.
Lemma magree_norm':
forall P m m'
e,
magree m m'
P ->
Mem.mem_norm m e =
Mem.mem_norm m'
e.
Proof.
Lemma magree_norm:
forall P m m'
e e',
magree m m'
P ->
Val.lessdef e e' ->
Values.Val.lessdef (
Mem.mem_norm m e) (
Mem.mem_norm m'
e').
Proof.
intros.
rewrite (
magree_norm'
_ _ _ _ H).
generalize (
wf_mr_norm_ld m'
_ _ H0).
intros;
rewrite <-
lessdef_val;
auto.
Qed.
Lemma magree_in_bound:
forall P m m',
magree m m'
P ->
forall b ofs,
Mem.in_bound_m ofs m b <->
Mem.in_bound_m ofs m'
b.
Proof.
Lemma transf_volatile_store:
forall v1 v2 v1'
v2'
m tm chunk sp nm t v m',
volatile_store_sem chunk ge (
v1::
v2::
nil)
m t v m' ->
Val.lessdef v1 v1' ->
vagree v2 v2' (
store_argument chunk) ->
magree m tm (
nlive ge sp nm) ->
v =
Eval Vundef /\
exists tm',
volatile_store_sem chunk ge (
v1'::
v2'::
nil)
tm t (
Eval Vundef)
tm'
/\
magree m'
tm' (
nlive ge sp nm).
Proof.
Lemma vagree_vundef:
forall x nv,
vagree (
Eval Vundef)
x nv.
Proof.
des nv; ldt.
Qed.
Lemma eagree_set_undef:
forall e1 e2 ne r,
eagree e1 e2 ne ->
eagree (
e1#
r <- (
Eval Vundef))
e2 ne.
Proof.
The simulation diagram
Variable ns :
ident ->
nat.
Lemma aaddressing_sound:
forall prog s f sp pc e m addr args b ofs sv,
sound_state prog (
State s f sp pc e m) ->
eval_addressing (
Genv.globalenv prog) (
Eval (
Vptr sp Int.zero))
addr e##
args =
Some sv ->
Mem.mem_norm m sv =
Vptr b ofs ->
exists bc,
pmatch bc b ofs (
aaddressing (
ValueAnalysis.analyze (
romem_for_program prog)
f)!!
pc addr args)
/\
genv_match bc (
Genv.globalenv prog)
/\
bc sp =
BCstack.
Proof.
Theorem step_simulation:
forall S1 t S2,
step ge ns S1 t S2 ->
forall S1',
match_states S1 S1' ->
sound_state prog S1 ->
exists S2',
step tge ns S1'
t S2' /\
match_states S2 S2'.
Proof.
Variable sg :
ident ->
Z.
Lemma transf_initial_states:
forall st1,
initial_state prog sg st1 ->
exists st2,
initial_state tprog sg st2 /\
match_states st1 st2.
Proof.
Lemma transf_final_states:
forall st1 st2 r,
match_states st1 st2 ->
final_state st1 r ->
final_state st2 r.
Proof.
intros.
inv H0.
inv H.
inv STACKS.
constructor.
exploit magree_norm;
eauto.
eapply mextends_agree with (
P :=
fun b ofs =>
True).
eauto.
rewrite H1;
intro A;
inv A;
auto.
Qed.
Semantic preservation
Hypothesis sg_pos:
forall i,
sg i > 0.
Theorem transf_program_correct:
forward_simulation (
RTL.semantics prog ns sg) (
RTL.semantics tprog ns sg).
Proof.
End PRESERVATION.