Require Import Coqlib.
Require Import Maps.
Require Import Maps2.
Require Import AST.
Require Import Op.
Require Import Utils.
Require Import Integers.
Require Import Floats.
Require Import Classical.
Require Import Dom.
Require Import SSA.
Require Import SSAutils.
Require Import Smallstep.
Require Import Globalenvs.
Require Import Values.
Require Import Events.
Require Import SCCPopt.
Require Import ConstpropOp.
Require Import DLib.
Require Import Utilsvalidproof.
Require Import KildallComp.
Require Import Dsd.
Require Import Relations.
Require Import SSAinv.
Require Import OptInv.
Require Import SCCPopt.
Require Import SCCPoptProp.
Require Import Registers.
Require Import Linking.
Unset Allow StrictProp.
Correctness proof of the SCCP optimization
Section PRESERVATION.
Definition match_prog (
p:
SSA.program) (
tp:
SSA.program) :=
match_program (
fun cu f tf =>
tf =
transf_fundef f)
eq p tp.
Lemma transf_program_match:
forall p,
match_prog p (
transf_program p).
Proof.
Section CORRECTNESS.
Hint Unfold exec:
core.
Variable prog:
program.
Variable tprog:
program.
Hypothesis TRANSL:
match_prog prog tprog.
Hypothesis HWF:
wf_ssa_program prog.
Definition ge :=
Genv.globalenv prog.
Definition tge :=
Genv.globalenv tprog.
Lemma same_symbols:
forall s,
Genv.find_symbol tge s =
Genv.find_symbol ge s.
Proof.
Lemma match_prog_wf_ssa :
wf_ssa_program tprog.
Proof.
red.
intros.
red in HWF.
inv TRANSL.
intuition.
revert H0 H HWF.
induction 1;
intros.
-
inv H.
-
inv H1.
+
inv H.
inv H4.
destruct f1 ;
simpl in * ;
try constructor;
auto.
eapply Opt.transf_function_preserve_wf_ssa_function;
eauto.
*
eapply new_code_same_or_Iop;
eauto.
*
exploit (
HWF (
Internal f)
id);
eauto.
destruct a1,
g;
simpl in * ;
try congruence.
left.
inv H;
simpl in *;
auto.
intros.
inv H4;
auto.
+
eapply IHlist_forall2;
eauto.
Qed.
Lemma funct_ptr_translated:
forall (
b:
Values.block) (
f:
fundef),
Genv.find_funct_ptr ge b =
Some f ->
Genv.find_funct_ptr tge b =
Some (
transf_fundef f).
Proof.
Lemma functions_translated:
forall (
v:
val) (
f:
fundef),
Genv.find_funct ge v =
Some f ->
Genv.find_funct tge v =
Some (
transf_fundef f).
Proof.
Lemma sig_preserved:
forall f,
funsig (
transf_fundef f) =
funsig f.
Proof.
destruct f; simpl ; try reflexivity.
Qed.
Lemma transf_ros_correct:
forall ros rs f,
find_function ge ros rs =
Some f ->
find_function tge ros rs =
Some (
transf_fundef f).
Proof.
Lemma same_eval_addressing:
forall sp addr rs args,
eval_addressing tge sp addr rs ##
args =
eval_addressing ge sp addr rs ##
args.
Proof.
Hint Resolve same_symbols:
core.
Lemma same_eval:
forall sp op rs args m,
eval_operation tge sp op rs ##
args m =
eval_operation ge sp op rs ##
args m.
Proof.
Lemma senv_equiv :
Senv.equiv ge tge.
Proof.
Simulation relation
Inductive match_stackframes :
list stackframe ->
list stackframe ->
Prop :=
|
match_stackframes_nil:
match_stackframes nil nil
|
match_stackframes_cons:
forall res f sp pc rs s st b
(
SP:
sp = (
Vptr b Ptrofs.zero))
(
STACK: (
match_stackframes s st))
(
WFF:
wf_ssa_function f)
(
HG:
forall v,
gamma SCCP f ge sp pc (
rs#
res <-
v))
(
EXE:
exec f pc),
match_stackframes
((
Stackframe res f sp pc rs) ::
s)
((
Stackframe res (
transf_function f)
sp pc rs) ::
st).
Variant match_states:
SSA.state ->
SSA.state ->
Prop :=
|
match_states_intro:
forall s st sp pc rs m f b
(
SP:
sp = (
Vptr b Ptrofs.zero))
(
SINV:
s_inv ge (
State s f sp pc rs m))
(
HG:
gamma SCCP f ge sp pc rs)
(
EXE:
exec f pc)
(
STACK:
match_stackframes s st),
match_states (
State s f sp pc rs m) (
State st (
transf_function f)
sp pc rs m)
|
match_states_call:
forall s st f args m
(
SINV:
s_inv ge (
Callstate s f args m))
(
STACK:
match_stackframes s st),
match_states (
Callstate s f args m) (
Callstate st (
transf_fundef f)
args m)
|
match_states_return:
forall s st v m
(
SINV:
s_inv ge (
Returnstate s v m))
(
STACK:
match_stackframes s st),
match_states (
Returnstate s v m) (
Returnstate st v m).
Hint Resolve sdom_dom_pred fn_code_reached fn_entry_pred
fn_phicode_inv list_nth_z_in:
core.
Hint Constructors clos_refl_trans SSA.step:
core.
Lemma match_stackframes_sfg_inv :
forall s st,
match_stackframes s st ->
sfg_inv SCCP prog s.
Proof.
induction 1 ; go.
Qed.
Lemma transf_initial_states:
forall st1,
SSA.initial_state prog st1 ->
exists st2,
SSA.initial_state tprog st2 /\
match_states st1 st2.
Proof.
Lemma transf_final_states:
forall st1 st2 r,
match_states st1 st2 ->
SSA.final_state st1 r ->
SSA.final_state st2 r.
Proof.
intros. inv H0. inv H.
inv STACK.
constructor.
Qed.
Soundness invariant of the analysis
Lemma subj_red_gamma :
forall prog (
WFP:
wf_ssa_program prog),
forall (
f f':
function) (
HWF :
wf_ssa_function f)
t m m'
rs rs'
sp sp'
pc pc'
s s',
gamma SCCP f (
Genv.globalenv prog) (
Vptr sp Ptrofs.zero)
pc rs ->
sfg_inv SCCP prog s ->
exec f pc ->
s_inv (
Genv.globalenv prog) (
State s f (
Vptr sp Ptrofs.zero)
pc rs m) ->
SSA.step (
Genv.globalenv prog) (
State s f (
Vptr sp Ptrofs.zero)
pc rs m)
t
(
State s'
f' (
Vptr sp'
Ptrofs.zero)
pc'
rs'
m') ->
gamma SCCP f' (
Genv.globalenv prog) (
Vptr sp'
Ptrofs.zero)
pc'
rs'.
Proof.
Hint Resolve match_stackframes_sfg_inv
subj_red subj_red_gamma:
core.
Ltac match_go :=
match goal with
|
id:
SSA.step _ _ _ _ |-
match_states _ _ =>
econstructor;
eauto ; [
eapply SSAinv.subj_red;
eauto
|
eapply subj_red_gamma;
eauto
|
eapply exec_step in id;
eauto]
end.
Ltac TransfInstr :=
match goal with
|
f :
function,
id: (
fn_code _)! ?
pc =
Some ?
instr |-
_ =>
case_eq (
DS.fixpoint f handle_instr (
initial f)) ;
intros lv es FIX;
set (
lv' :=
fun reg =>
PMap.get reg lv)
in * ;
try assert ((
fn_code (
transf_function f)) !
pc =
Some(
transf_instr lv'
pc instr))
by (
unfold transf_function,
Opt.transf_function,
fn_code;
simpl;
rewrite PTree.gmap;
try rewrite FIX;
unfold option_map;
destruct f ;
simpl in *;
flatten ;
reflexivity);
simpl transf_instr in *
end.
Hint Extern 1 (
wf_ssa_function _) =>
invh s_inv:
core.
Lemma transf_step_correct:
forall s1 t s2,
SSA.step ge s1 t s2 ->
SSA.step ge s1 t s2 ->
forall s1' (
MS:
match_states s1 s1'),
exists s2',
SSA.step tge s1'
t s2' /\
match_states s2 s2'.
Proof.
Theorem transf_program_correct:
forward_simulation (
SSA.semantics prog) (
SSA.semantics tprog).
Proof.
End CORRECTNESS.
End PRESERVATION.