(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Solvers for dataflow inequations. *)

Require Import Coqlib.
Require Import Iteration.
Require Import Maps.
Require Import Lattice.

(* To avoid useless definitions of inductors in extracted code. *)
Local Unset Elimination Schemes.
Local Unset Case Analysis Schemes.

(** A forward dataflow problem is a set of inequations of the form
- [X(s) >= transf n X(n)]
  if program point [s] is a successor of program point [n]
- [X(n) >= a]
  if [n] is an entry point and [a] its minimal approximation.

The unknowns are the [X(n)], indexed by program points (e.g. nodes in the
CFG graph of a RTL function).  They range over a given ordered set that
represents static approximations of the program state at each point.
The [transf] function is the abstract transfer function: it computes an
approximation [transf n X(n)] of the program state after executing instruction
at point [n], as a function of the approximation [X(n)] of the program state
before executing that instruction.

Symmetrically, a backward dataflow problem is a set of inequations of the form
- [X(n) >= transf s X(s)]
  if program point [s] is a successor of program point [n]
- [X(n) >= a]
  if [n] is an entry point and [a] its minimal approximation.

The only difference with a forward dataflow problem is that the transfer
function [transf] now computes the approximation before a program point [s]
from the approximation [X(s)] after point [s].

This file defines three solvers for dataflow problems.  The first two
solve (optimally) forward and backward problems using Kildall's worklist
algorithm.  They assume that the unknowns range over a semi-lattice, that is,
an ordered type equipped with a least upper bound operation.

The last solver corresponds to propagation over extended basic blocks:
it returns approximate solutions of forward problems where the unknowns
range over any ordered type having a greatest element [top].  It simply
sets [X(n) = top] for all merge points [n], that is, program points having
several predecessors.  This solver is useful when least upper bounds of
approximations do not exist or are too expensive to compute. *)

(** * Solving forward dataflow problems using Kildall's algorithm *)

(** A forward dataflow solver has the following generic interface.
  Unknowns range over the type [L.t], which is equipped with
  semi-lattice operations (see file [Lattice]).  *)

Module Type DATAFLOW_SOLVER.

  Declare Module L: SEMILATTICE.

  (** [fixpoint successors transf ep ev] is the solver.
    It returns either an error or a mapping from program points to
    values of type [L.t] representing the solution.  [successors]
    is a finite map returning the list of successors of the given program
    point. [transf] is the transfer function, [ep] the entry point,
    and [ev] the minimal abstract value for [ep]. *)

  Parameter fixpoint:
    forall {A: Type} (code: PTree.t A) (successors: A -> list positive)
           (transf: positive -> L.t -> L.t)
           (ep: positive) (ev: L.t),
    option (PMap.t L.t).

  (** The [fixpoint_solution] theorem shows that the returned solution,
    if any, satisfies the dataflow inequations. *)

  Axiom fixpoint_solution:
    forall A (code: PTree.t A) successors transf ep ev res n instr s,
    fixpoint code successors transf ep ev = Some res ->
    code!n = Some instr -> In s (successors instr) ->
    (forall n, L.eq (transf n L.bot) L.bot) ->
    L.ge res!!s (transf n res!!n).

  (** The [fixpoint_entry] theorem shows that the returned solution,
    if any, satisfies the additional constraint over the entry point. *)

  Axiom fixpoint_entry:
    forall A (code: PTree.t A) successors transf ep ev res,
    fixpoint code successors transf ep ev = Some res ->
    L.ge res!!ep ev.

  (** Finally, any property that is preserved by [L.lub] and [transf]
      and that holds for [L.bot] also holds for the results of
      the analysis. *)

  Axiom fixpoint_invariant:
    forall A (code: PTree.t A) successors transf ep ev
           (P: L.t -> Prop),
    P L.bot ->
    (forall x y, P x -> P y -> P (L.lub x y)) ->
    (forall pc instr x, code!pc = Some instr -> P x -> P (transf pc x)) ->
    P ev ->
    forall res pc,
    fixpoint code successors transf ep ev = Some res ->
    P res!!pc.

End DATAFLOW_SOLVER.

(** Kildall's algorithm manipulates worklists, which are sets of CFG nodes
  equipped with a ``pick next node to examine'' operation.
  The algorithm converges faster if the nodes are chosen in an order
  consistent with a reverse postorder traversal of the CFG.
  For now, we parameterize the dataflow solver over a module
  that implements sets of CFG nodes. *)

Module Type NODE_SET.

  Parameter t: Type.
  Parameter empty: t.
  Parameter add: positive -> t -> t.
  Parameter pick: t -> option (positive * t).
  Parameter all_nodes: forall {A: Type}, PTree.t A -> t.

  Parameter In: positive -> t -> Prop.
  Axiom empty_spec:
    forall n, ~In n empty.
  Axiom add_spec:
    forall n n' s, In n' (add n s) <-> n = n' \/ In n' s.
  Axiom pick_none:
    forall s n, pick s = None -> ~In n s.
  Axiom pick_some:
    forall s n s', pick s = Some(n, s') ->
    forall n', In n' s <-> n = n' \/ In n' s'.
  Axiom all_nodes_spec:
    forall A (code: PTree.t A) n instr,
    code!n = Some instr -> In n (all_nodes code).

End NODE_SET.

(** Reachability in a control-flow graph. *)

Section REACHABLE.

Context {A: Type} (code: PTree.t A) (successors: A -> list positive).

Inductive reachable: positive -> positive -> Prop :=
  | reachable_refl: forall n, reachable n n
  | reachable_left: forall n1 n2 n3 i,
      code!n1 = Some i -> In n2 (successors i) -> reachable n2 n3 ->
      reachable n1 n3.

Scheme reachable_ind := Induction for reachable Sort Prop.

Lemma reachable_trans:
  forall n1 n2, reachable n1 n2 -> forall n3, reachable n2 n3 -> reachable n1 n3.
Proof.
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
  induction 1; intros.
- auto.
- econstructor; eauto.
Qed.

Lemma reachable_right:
  forall n1 n2 n3 i,
  reachable n1 n2 -> code!n2 = Some i -> In n3 (successors i) ->
  reachable n1 n3.
Proof.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  intros. apply reachable_trans with n2; auto. econstructor; eauto. constructor.
Qed.

End REACHABLE.

(** We now define a generic solver for forward dataflow inequations
  that works over any semi-lattice structure. *)

Module Dataflow_Solver (LAT: SEMILATTICE) (NS: NODE_SET) <:
                          DATAFLOW_SOLVER with Module L := LAT.

Module L := LAT.

Section Kildall.

Context {A: Type}.
Variable code: PTree.t A.
Variable successors: A -> list positive.
Variable transf: positive -> L.t -> L.t.

(** The state of the iteration has three components:
- [aval]: A mapping from program points to values of type [L.t] representing
  the candidate solution found so far.
- [worklist]: A worklist of program points that remain to be considered.
- [visited]: A set of program points that were visited already
  (i.e. put on the worklist at some point in the past).

Only the first two components are computationally relevant.  The third
is a ghost variable used only for stating and proving invariants.
For this reason, [visited] is defined at sort [Prop] so that it is
erased during program extraction.
*)

Record state : Type :=
  mkstate { aval: PTree.t L.t; worklist: NS.t; visited: positive -> Prop }.

Definition abstr_value (n: positive) (s: state) : L.t :=
  match s.(aval)!n with
  | None => L.bot
  | Some v => v
  end.

(** Kildall's algorithm, in pseudo-code, is as follows:
<<
    while worklist is not empty, do
        extract a node n from worklist
        compute out = transf n aval[n]
        for each successor s of n:
            compute in = lub aval[s] out
            if in <> aval[s]:
                aval[s] := in
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                worklist := worklist union {s}
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                visited := visited union {s}
            end if
        end for
    end while
    return aval
>>
*)

(** [propagate_succ] corresponds, in the pseudocode,
  to the body of the [for] loop iterating over all successors. *)

Definition propagate_succ (s: state) (out: L.t) (n: positive) :=
  match s.(aval)!n with
  | None =>
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
      {| aval := PTree.set n out s.(aval);
         worklist := NS.add n s.(worklist);
         visited := fun p => p = n \/ s.(visited) p |}
  | Some oldl =>
      let newl := L.lub oldl out in
      if L.beq oldl newl
      then s
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
      else {| aval := PTree.set n newl s.(aval);
              worklist := NS.add n s.(worklist);
              visited := fun p => p = n \/ s.(visited) p |}
  end.

(** [propagate_succ_list] corresponds, in the pseudocode,
  to the [for] loop iterating over all successors. *)

Fixpoint propagate_succ_list (s: state) (out: L.t) (succs: list positive)
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                             {struct succs} : state :=
  match succs with
  | nil => s
  | n :: rem => propagate_succ_list (propagate_succ s out n) out rem
  end.

(** [step] corresponds to the body of the outer [while] loop in the
  pseudocode. *)

Definition step (s: state) : PMap.t L.t + state :=
  match NS.pick s.(worklist) with
  | None =>
      inl _ (L.bot, s.(aval))
  | Some(n, rem) =>
      match code!n with
      | None =>
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
          inr _ {| aval := s.(aval); worklist := rem; visited := s.(visited) |}
      | Some instr =>
          inr _ (propagate_succ_list
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                  {| aval := s.(aval); worklist := rem; visited := s.(visited) |}
                  (transf n (abstr_value n s))
                  (successors instr))
      end
  end.

(** The whole fixpoint computation is the iteration of [step] from
  an initial state. *)

Definition fixpoint_from (start: state) : option (PMap.t L.t) :=
  PrimIter.iterate _ _ step start.

(** There are several ways to build the initial state.  For forward
  dataflow analyses, the initial worklist is the function entry point,
  and the initial mapping sets the function entry point to the given
  abstract value, and leaves unset all other program points, which
  corresponds to [L.bot] initial abstract values. *)

Definition start_state (enode: positive) (eval: L.t) :=
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {| aval := PTree.set enode eval (PTree.empty L.t);
     worklist := NS.add enode NS.empty;
     visited := fun n => n = enode |}.

Definition fixpoint (enode: positive) (eval: L.t) :=
  fixpoint_from (start_state enode eval).

(** For backward analyses (viewed as forward analyses on the reversed CFG),
  the following two variants are more useful.  Both start with an
  empty initial mapping, where all program points start at [L.bot].
  The first initializes the worklist with a given set of entry points
  in the reversed CFG.  (See the backward dataflow solver below for
  how this list is computed.)  The second start state construction
  initializes the worklist with all program points of the given CFG. *)

Definition start_state_nodeset (enodes: NS.t) :=
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {| aval := PTree.empty L.t;
     worklist := enodes;
     visited := fun n => NS.In n enodes |}.

Definition fixpoint_nodeset (enodes: NS.t) :=
  fixpoint_from (start_state_nodeset enodes).

Definition start_state_allnodes :=
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {| aval := PTree.empty L.t;
     worklist := NS.all_nodes code;
     visited := fun n => exists instr, code!n = Some instr |}.

Definition fixpoint_allnodes :=
  fixpoint_from start_state_allnodes.

(** ** Characterization of the propagation functions *)

Inductive optge: option L.t -> option L.t -> Prop :=
  | optge_some: forall l l',
      L.ge l l' -> optge (Some l) (Some l')
  | optge_none: forall ol,
      optge ol None.

Remark optge_refl: forall ol, optge ol ol.
Proof. destruct ol; constructor. apply L.ge_refl; apply L.eq_refl. Qed.

Remark optge_trans: forall ol1 ol2 ol3, optge ol1 ol2 -> optge ol2 ol3 -> optge ol1 ol3.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros. inv H0.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  inv H. constructor. eapply L.ge_trans; eauto.
(* Goal: @reachable A code successors pc pc *)
  constructor.
Qed.

Remark optge_abstr_value:
  forall st st' n,
  optge st.(aval)!n st'.(aval)!n ->
  L.ge (abstr_value n st) (abstr_value n st').
Proof.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  intros. unfold abstr_value. inv H. auto. apply L.ge_bot.
Qed.

Lemma propagate_succ_charact:
  forall st out n,
  let st' := propagate_succ st out n in
     optge st'.(aval)!n (Some out)
  /\ (forall s, n <> s -> st'.(aval)!s = st.(aval)!s)
  /\ (forall s, optge st'.(aval)!s st.(aval)!s)
  /\ (NS.In n st'.(worklist) \/ st'.(aval)!n = st.(aval)!n)
  /\ (forall n', NS.In n' st.(worklist) -> NS.In n' st'.(worklist))
  /\ (forall n', NS.In n' st'.(worklist) -> n' = n \/ NS.In n' st.(worklist))
  /\ (forall n', st.(visited) n' -> st'.(visited) n')
  /\ (forall n', st'.(visited) n' -> NS.In n' st'.(worklist) \/ st.(visited) n')
  /\ (forall n', st.(aval)!n' = None -> st'.(aval)!n' <> None -> st'.(visited) n').
Proof.
  unfold propagate_succ; intros; simpl.
  destruct st.(aval)!n as [v|] eqn:E;
  [predSpec L.beq L.beq_correct v (L.lub v out) | idtac].
- (* already there, unchanged *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  repeat split; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite E. constructor. eapply L.ge_trans. apply L.ge_refl. apply H; auto. apply L.ge_lub_right.
  + apply optge_refl.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + right; auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
  + congruence.
- (* already there, updated *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  simpl; repeat split; intros.
(* Goal: @reachable A code successors pc pc *)
  + rewrite PTree.gss. constructor. apply L.ge_lub_right.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite PTree.gso by auto. auto.
  + rewrite PTree.gsspec. destruct (peq s n).
(* Goal: @reachable A code successors pc pc *)
    subst s. rewrite E. constructor. apply L.ge_lub_left.
(* Goal: optge (@PTree.get L.t n (aval s)) (@PTree.get L.t n (aval s)) *)
(* Goal: forall _ : visited s n, visited s n *)
    apply optge_refl.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite NS.add_spec. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite NS.add_spec. auto.
  + rewrite NS.add_spec in H0. intuition.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + destruct H0; auto. subst n'. rewrite NS.add_spec; auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite PTree.gsspec in H1. destruct (peq n' n). auto. congruence.
- (* not previously there, updated *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  simpl; repeat split; intros.
  + rewrite PTree.gss. apply optge_refl.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite PTree.gso by auto. auto.
  + rewrite PTree.gsspec. destruct (peq s n).
(* Goal: @reachable A code successors pc pc *)
    subst s. rewrite E. constructor.
(* Goal: optge (@PTree.get L.t n (aval s)) (@PTree.get L.t n (aval s)) *)
(* Goal: forall _ : visited s n, visited s n *)
    apply optge_refl.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite NS.add_spec. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite NS.add_spec. auto.
  + rewrite NS.add_spec in H. intuition.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + destruct H; auto. subst n'. rewrite NS.add_spec. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite PTree.gsspec in H0. destruct (peq n' n). auto. congruence.
Qed.

Lemma propagate_succ_list_charact:
  forall out l st,
  let st' := propagate_succ_list st out l in
     (forall n, In n l -> optge st'.(aval)!n (Some out))
  /\ (forall n, ~In n l -> st'.(aval)!n = st.(aval)!n)
  /\ (forall n, optge st'.(aval)!n st.(aval)!n)
  /\ (forall n, NS.In n st'.(worklist) \/ st'.(aval)!n = st.(aval)!n)
  /\ (forall n', NS.In n' st.(worklist) -> NS.In n' st'.(worklist))
  /\ (forall n', NS.In n' st'.(worklist) -> In n' l \/ NS.In n' st.(worklist))
  /\ (forall n', st.(visited) n' -> st'.(visited) n')
  /\ (forall n', st'.(visited) n' -> NS.In n' st'.(worklist) \/ st.(visited) n')
  /\ (forall n', st.(aval)!n' = None -> st'.(aval)!n' <> None -> st'.(visited) n').
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  induction l; simpl; intros.
- repeat split; intros.
  + contradiction.
  + apply optge_refl.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + auto.
  + congruence.
- generalize (propagate_succ_charact st out a).
(* Goal: None *)
  set (st1 := propagate_succ st out a).
(* Goal: None *)
  intros (A1 & A2 & A3 & A4 & A5 & A6 & A7 & A8 & A9).
(* Goal: None *)
  generalize (IHl st1).
(* Goal: None *)
  set (st2 := propagate_succ_list st1 out l).
(* Goal: None *)
  intros (B1 & B2 & B3 & B4 & B5 & B6 & B7 & B8 & B9). clear IHl.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  repeat split; intros.
  + destruct H.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    * subst n. eapply optge_trans; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    * auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite B2 by tauto. apply A2; tauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + eapply optge_trans; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + destruct (B4 n). auto.
(* Goal: None *)
    destruct (peq n a).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    * subst n. destruct A4. left; auto. right; congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    * right. rewrite H. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + exploit B6; eauto. intros [P|P]. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    exploit A6; eauto. intuition.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + eauto.
  + specialize (B8 n'); specialize (A8 n'). intuition.
  + destruct st1.(aval)!n' eqn:ST1.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply B7. apply A9; auto. congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply B9; auto.
Qed.

(** Characterization of [fixpoint_from]. *)

Inductive steps: state -> state -> Prop :=
  | steps_base: forall s, steps s s
  | steps_right: forall s1 s2 s3, steps s1 s2 -> step s2 = inr s3 -> steps s1 s3.

Scheme steps_ind := Induction for steps Sort Prop.

Lemma fixpoint_from_charact:
  forall start res,
  fixpoint_from start = Some res ->
  exists st, steps start st /\ NS.pick st.(worklist) = None /\ res = (L.bot, st.(aval)).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint; intros.
  eapply (PrimIter.iterate_prop _ _ step
              (fun st => steps start st)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
              (fun res => exists st, steps start st /\ NS.pick (worklist st) = None /\ res = (L.bot, aval st))); eauto.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros. destruct (step a) eqn:E.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exists a; split; auto.
(* Goal: None *)
(* Goal: steps start s *)
(* Goal: steps start start *)
  unfold step in E. destruct (NS.pick (worklist a)) as [[n rem]|].
(* Goal: None *)
(* Goal: None *)
(* Goal: steps start s *)
(* Goal: steps start start *)
  destruct (code!n); discriminate.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  inv E. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply steps_right; eauto.
(* Goal: @reachable A code successors pc pc *)
  constructor.
Qed.

(** ** Monotonicity properties *)

(** We first show that the [aval] and [visited] parts of the state
evolve monotonically:
- at each step, the values of the [aval[n]] either remain the same or
  increase with respect to the [optge] ordering;
- every node visited in the past remains visited in the future.
*)

Lemma step_incr:
  forall n s1 s2, step s1 = inr s2 ->
  optge s2.(aval)!n s1.(aval)!n /\ (s1.(visited) n -> s2.(visited) n).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold step; intros.
(* Goal: and (optge (@PTree.get L.t n (aval s2)) (@PTree.get L.t n (aval s1))) (forall _ : visited s1 n, visited s2 n) *)
  destruct (NS.pick (worklist s1)) as [[p rem] | ]; try discriminate.
(* Goal: L.ge match @PTree.get L.t n (aval st) with | Some v => v | None => L.bot end match @PTree.get L.t n (aval st') with | Some v => v | None => L.bot end *)
  destruct (code!p) as [instr|]; inv H.
  + generalize (propagate_succ_list_charact
                     (transf p (abstr_value p s1))
                     (successors instr)
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                     {| aval := aval s1; worklist := rem; visited := visited s1 |}).
(* Goal: @In positive n (worklist (mkstate res rem)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
      simpl.
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
      set (s' := propagate_succ_list {| aval := aval s1; worklist := rem; visited := visited s1 |}
                    (transf p (abstr_value p s1)) (successors instr)).
(* Goal: None *)
      intros (A1 & A2 & A3 & A4 & A5 & A6 & A7 & A8 & A9).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
      auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + split. apply optge_refl. auto.
Qed.

Lemma steps_incr:
  forall n s1 s2, steps s1 s2 ->
  optge s2.(aval)!n s1.(aval)!n /\ (s1.(visited) n -> s2.(visited) n).
Proof.
(* Goal: forall (p q : positive) (_ : @reachable A code successors p q), @reachable (list positive) make_predecessors (fun l : list positive => l) q p *)
  induction 1.
- split. apply optge_refl. auto.
- destruct IHsteps. exploit (step_incr n); eauto. intros [P Q].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  split. eapply optge_trans; eauto. eauto.
Qed.

(** ** Correctness invariant *)

(** The following invariant is preserved at each iteration of Kildall's
  algorithm: for all visited program point [n], either
  [n] is in the worklist, or the inequations associated with [n]
  ([aval[s] >= transf n aval[n]] for all successors [s] of [n])
  hold.  In other terms, the worklist contains all nodes that were
  visited but do not yet satisfy their inequations.

  The second part of the invariant shows that nodes that already have
  an abstract value associated with them have been visited. *)

(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
Record good_state (st: state) : Prop := {
  gs_stable: forall n,
    st.(visited) n ->
    NS.In n st.(worklist) \/
    (forall i s,
     code!n = Some i -> In s (successors i) ->
     optge st.(aval)!s (Some (transf n (abstr_value n st))));
  gs_defined: forall n v,
    st.(aval)!n = Some v -> st.(visited) n
}.

(** We show that the [step] function preserves this invariant. *)

Lemma step_state_good:
  forall st pc rem instr,
  NS.pick st.(worklist) = Some (pc, rem) ->
  code!pc = Some instr ->
  good_state st ->
  good_state (propagate_succ_list (mkstate st.(aval) rem st.(visited))
                                  (transf pc (abstr_value pc st))
                                  (successors instr)).
Proof.
(* Goal: None *)
  intros until instr; intros PICK CODEAT [GOOD1 GOOD2].
(* Goal: good_state (mkstate (aval st) rem (visited st)) *)
  generalize (NS.pick_some _ _ _ PICK); intro PICK2.
(* Goal: good_state (propagate_succ_list (mkstate (aval st) rem (visited st)) (transf pc (abstr_value pc st)) (successors instr)) *)
  set (out := transf pc (abstr_value pc st)).
(* Goal: good_state (propagate_succ_list (mkstate (aval st) rem (visited st)) out (successors instr)) *)
  generalize (propagate_succ_list_charact out (successors instr) {| aval := aval st; worklist := rem; visited := visited st |}).
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  set (st' := propagate_succ_list {| aval := aval st; worklist := rem; visited := visited st |} out
                                  (successors instr)).
(* Goal: None *)
  simpl; intros (A1 & A2 & A3 & A4 & A5 & A6 & A7 & A8 & A9).
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  constructor; intros.
- (* stable *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  destruct (A8 n H); auto. destruct (A4 n); auto.
  replace (abstr_value n st') with (abstr_value n st)
  by (unfold abstr_value; rewrite H1; auto).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit GOOD1; eauto. intros [P|P].
+ (* n was on the worklist *)
(* Goal: None *)
  rewrite PICK2 in P; destruct P.
  * (* node n is our node pc *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    subst n. fold out. right; intros.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    assert (i = instr) by congruence. subst i.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply A1; auto.
  * (* n was already on the worklist *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    left. apply A5; auto.
+ (* n was stable before, still is *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  right; intros. apply optge_trans with st.(aval)!s; eauto.
- (* defined *)
(* Goal: visited st' n *)
  destruct st.(aval)!n as [v'|] eqn:ST.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + apply A7. eapply GOOD2; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + apply A9; auto. congruence.
Qed.

Lemma step_state_good_2:
  forall st pc rem,
  good_state st ->
  NS.pick (worklist st) = Some (pc, rem) ->
  code!pc = None ->
  good_state (mkstate st.(aval) rem st.(visited)).
Proof.
(* Goal: None *)
  intros until rem; intros [GOOD1 GOOD2] PICK CODE.
(* Goal: good_state (mkstate (aval st) rem (visited st)) *)
  generalize (NS.pick_some _ _ _ PICK); intro PICK2.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  constructor; simpl; intros.
- (* stable *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit GOOD1; eauto. intros [P | P].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  + rewrite PICK2 in P. destruct P; auto.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    subst n. right; intros. congruence.
  + right; exact P.
- (* defined *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply GOOD2; eauto.
Qed.

Lemma steps_state_good:
  forall st1 st2, steps st1 st2 -> good_state st1 -> good_state st2.
Proof.
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
  induction 1; intros.
- auto.
- unfold step in e.
(* Goal: good_state s3 *)
  destruct (NS.pick (worklist s2)) as [[n rem] | ] eqn:PICK; try discriminate.
(* Goal: good_state s3 *)
  destruct (code!n) as [instr|] eqn:CODE; inv e.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply step_state_good; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply step_state_good_2; eauto.
Qed.

(** We show that initial states satisfy the invariant. *)

Lemma start_state_good:
  forall enode eval, good_state (start_state enode eval).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros. unfold start_state; constructor; simpl; intros.
- subst n. rewrite NS.add_spec; auto.
- rewrite PTree.gsspec in H. rewrite PTree.gempty in H.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  destruct (peq n enode). auto. discriminate.
Qed.

Lemma start_state_nodeset_good:
  forall enodes, good_state (start_state_nodeset enodes).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros. unfold start_state_nodeset; constructor; simpl; intros.
- left. auto.
- rewrite PTree.gempty in H. congruence.
Qed.

Lemma start_state_allnodes_good:
  good_state start_state_allnodes.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold start_state_allnodes; constructor; simpl; intros.
- destruct H as [instr CODE]. left. eapply NS.all_nodes_spec; eauto.
- rewrite PTree.gempty in H. congruence.
Qed.

(** Reachability in final states. *)

Lemma reachable_visited:
  forall st, good_state st -> NS.pick st.(worklist) = None ->
  forall p q, reachable code successors p q -> st.(visited) p -> st.(visited) q.
Proof.
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
  intros st [GOOD1 GOOD2] PICK. induction 1; intros.
- auto.
- eapply IHreachable; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit GOOD1; eauto. intros [P | P].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eelim NS.pick_none; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit P; eauto. intros OGE; inv OGE. eapply GOOD2; eauto.
Qed.

(** ** Correctness of the solution returned by [fixpoint]. *)

(** As a consequence of the [good_state] invariant, the result of
  [fixpoint], if defined, is a solution of the dataflow inequations.
  This assumes that the transfer function maps [L.bot] to [L.bot]. *)

Theorem fixpoint_solution:
  forall ep ev res n instr s,
  fixpoint ep ev = Some res ->
  code!n = Some instr ->
  In s (successors instr) ->
  (forall n, L.eq (transf n L.bot) L.bot) ->
  L.ge res!!s (transf n res!!n).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit fixpoint_from_charact; eauto. intros (st & STEPS & PICK & RES).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit steps_state_good; eauto. apply start_state_good. intros [GOOD1 GOOD2].
(* Goal: @In positive n (worklist (mkstate res rem)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  rewrite RES; unfold PMap.get; simpl.
(* Goal: L.ge match @PTree.get L.t s (aval st) with | Some x => x | None => L.bot end (transf n match @PTree.get L.t n (aval st) with | Some x => x | None => L.bot end) *)
  destruct st.(aval)!n as [v|] eqn:STN.
- destruct (GOOD1 n) as [P|P]; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eelim NS.pick_none; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit P; eauto. unfold abstr_value; rewrite STN. intros OGE; inv OGE. auto.
- apply L.ge_trans with L.bot. apply L.ge_bot. apply L.ge_refl. apply L.eq_sym. eauto.
Qed.

(** Moreover, the result of [fixpoint], if defined, satisfies the additional
  constraint given on the entry point. *)

Theorem fixpoint_entry:
  forall ep ev res,
  fixpoint ep ev = Some res ->
  L.ge res!!ep ev.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit fixpoint_from_charact; eauto. intros (st & STEPS & PICK & RES).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (steps_incr ep); eauto. simpl. rewrite PTree.gss. intros [P Q].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  rewrite RES; unfold PMap.get; simpl. inv P; auto.
Qed.

(** For [fixpoint_allnodes], we show that the result is a solution
  without assuming [transf n L.bot = L.bot]. *)

Theorem fixpoint_allnodes_solution:
  forall res n instr s,
  fixpoint_allnodes = Some res ->
  code!n = Some instr ->
  In s (successors instr) ->
  L.ge res!!s (transf n res!!n).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint_allnodes; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit fixpoint_from_charact; eauto. intros (st & STEPS & PICK & RES).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit steps_state_good; eauto. apply start_state_allnodes_good. intros [GOOD1 GOOD2].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (steps_incr n); eauto. simpl. intros [U V].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (GOOD1 n). apply V. exists instr; auto. intros [P|P].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eelim NS.pick_none; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit P; eauto. intros OGE. rewrite RES; unfold PMap.get; simpl.
(* Goal: L.ge match @PTree.get L.t s (aval st) with | Some x => x | None => L.bot end (transf n match @PTree.get L.t n (aval st) with | Some x => x | None => L.bot end) *)
  inv OGE. assumption.
Qed.

(** For [fixpoint_nodeset], we show that the result is a solution
  at all program points that are reachable from the given entry points. *)

Theorem fixpoint_nodeset_solution:
  forall enodes res e n instr s,
  fixpoint_nodeset enodes = Some res ->
  NS.In e enodes ->
  reachable code successors e n ->
  code!n = Some instr ->
  In s (successors instr) ->
  L.ge res!!s (transf n res!!n).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint_nodeset; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit fixpoint_from_charact; eauto. intros (st & STEPS & PICK & RES).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit steps_state_good; eauto. apply start_state_nodeset_good. intros GOOD.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (steps_incr e); eauto. simpl. intros [U V].
(* Goal: L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res)) *)
  assert (st.(visited) n).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  { eapply reachable_visited; eauto. }
(* Goal: L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res)) *)
  destruct GOOD as [GOOD1 GOOD2].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (GOOD1 n); eauto. intros [P|P].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eelim NS.pick_none; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit P; eauto. intros OGE. rewrite RES; unfold PMap.get; simpl.
(* Goal: L.ge match @PTree.get L.t s (aval st) with | Some x => x | None => L.bot end (transf n match @PTree.get L.t n (aval st) with | Some x => x | None => L.bot end) *)
  inv OGE. assumption.
Qed.

(** ** Preservation of a property over solutions *)

Theorem fixpoint_invariant:
  forall ep ev
    (P: L.t -> Prop)
    (P_bot: P L.bot)
    (P_lub: forall x y, P x -> P y -> P (L.lub x y))
    (P_transf: forall pc instr x, code!pc = Some instr -> P x -> P (transf pc x))
    (P_entrypoint: P ev)
    res pc,
  fixpoint ep ev = Some res ->
  P res!!pc.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros.
(* Goal: P (@PMap.get L.t pc res) *)
  set (inv := fun st => forall x, P (abstr_value x st)).
(* Goal: P (@PMap.get L.t pc res) *)
  assert (inv (start_state ep ev)).
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    red; simpl; intros. unfold abstr_value, start_state; simpl.
(* Goal: iff (or (@eq positive k n) (@ex A (fun i : A => and (@eq (option A) (@PTree.get A n m) (@Some A i)) (@eq bool (sequential_node n i) false)))) (@ex A (fun i : A => and (@eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A i)) (@eq bool (sequential_node n i) false))) *)
    rewrite PTree.gsspec. rewrite PTree.gempty.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    destruct (peq x ep). auto. auto.
  }
  assert (forall st v n, inv st -> P v -> inv (propagate_succ st v n)).
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    unfold inv, propagate_succ. intros.
(* Goal: None *)
    destruct (aval st)!n as [oldl|] eqn:E.
(* Goal: None *)
(* Goal: None *)
    destruct (L.beq oldl (L.lub oldl v)).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    auto.
(* Goal: None *)
    unfold abstr_value. simpl. rewrite PTree.gsspec. destruct (peq x n).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply P_lub; auto. replace oldl with (abstr_value n st). auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    unfold abstr_value; rewrite E; auto.
(* Goal: P match @PTree.get L.t x (aval st) with | Some v => v | None => L.bot end *)
    apply H1.
(* Goal: None *)
    unfold abstr_value. simpl. rewrite PTree.gsspec. destruct (peq x n).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    auto.
(* Goal: P match @PTree.get L.t x (aval st) with | Some v => v | None => L.bot end *)
    apply H1.
  }
  assert (forall l st v, inv st -> P v -> inv (propagate_succ_list st v l)).
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    induction l; intros; simpl. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply IHl; auto.
  }
  assert (forall st1 st2, steps st1 st2 -> inv st1 -> inv st2).
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  {
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
    induction 1; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    auto.
(* Goal: inv s3 *)
    unfold step in e. destruct (NS.pick (worklist s2)) as [[n rem]|]; try discriminate.
(* Goal: inv s3 *)
    destruct (code!n) as [instr|] eqn:INSTR; inv e.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply H2. apply IHsteps; auto. eapply P_transf; eauto. apply IHsteps; auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply IHsteps; auto.
  }
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  unfold fixpoint in H. exploit fixpoint_from_charact; eauto.
(* Goal: None *)
  intros (st & STEPS & PICK & RES).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  replace (res!!pc) with (abstr_value pc st). eapply H3; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  rewrite RES; auto.
Qed.

End Kildall.

End Dataflow_Solver.

(** * Solving backward dataflow problems using Kildall's algorithm *)

(** A backward dataflow problem on a given flow graph is a forward
  dataflow program on the reversed flow graph, where predecessors replace
  successors.  We exploit this observation to cheaply derive a backward
  solver from the forward solver. *)

(** ** Construction of the reversed flow graph (the predecessor relation) *)

Definition successors_list (successors: PTree.t (list positive)) (pc: positive) : list positive :=
  match successors!pc with None => nil | Some l => l end.

Notation "a !!! b" := (successors_list a b) (at level 1).

Section Predecessor.

Context {A: Type}.
Variable code: PTree.t A.
Variable successors: A -> list positive.

Fixpoint add_successors (pred: PTree.t (list positive))
                        (from: positive) (tolist: list positive)
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                        {struct tolist} : PTree.t (list positive) :=
  match tolist with
  | nil => pred
  | to :: rem => add_successors (PTree.set to (from :: pred!!!to) pred) from rem
  end.

Lemma add_successors_correct:
  forall tolist from pred n s,
  In n pred!!!s \/ (n = from /\ In s tolist) ->
  In n (add_successors pred from tolist)!!!s.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  induction tolist; simpl; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  tauto.
(* Goal: @In positive n (successors_list (add_successors (@PTree.set (list positive) a (@cons positive from (successors_list pred a)) pred) from tolist) s) *)
  apply IHtolist.
(* Goal: iff (or (@eq positive k n) (@ex A (fun i : A => and (@eq (option A) (@PTree.get A n m) (@Some A i)) (@eq bool (sequential_node n i) false)))) (@ex A (fun i : A => and (@eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A i)) (@eq bool (sequential_node n i) false))) *)
  unfold successors_list at 1. rewrite PTree.gsspec. destruct (peq s a).
(* Goal: or (@In positive n (@cons positive from (successors_list pred s))) (and (@eq positive n from) (@In positive s tolist)) *)
(* Goal: or (@In positive n match @PTree.get (list positive) s pred with | Some l => l | None => @nil positive end) (and (@eq positive n from) (@In positive s tolist)) *)
  subst a. destruct H. auto with coqlib.
(* Goal: or (@In positive n (@cons positive from (successors_list pred s))) (and (@eq positive n from) (@In positive s tolist)) *)
(* Goal: or (@In positive n match @PTree.get (list positive) s pred with | Some l => l | None => @nil positive end) (and (@eq positive n from) (@In positive s tolist)) *)
  destruct H. subst n. auto with coqlib.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  fold (successors_list pred s). intuition congruence.
Qed.

Definition make_predecessors : PTree.t (list positive) :=
  PTree.fold (fun pred pc instr => add_successors pred pc (successors instr))
             code (PTree.empty (list positive)).

Lemma make_predecessors_correct_1:
  forall n instr s,
  code!n = Some instr -> In s (successors instr) ->
  In n make_predecessors!!!s.
Proof.
(* Goal: forall (n : positive) (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), @In positive n (successors_list make_predecessors s) *)
  intros until s.
  set (P := fun m p => m!n = Some instr -> In s (successors instr) ->
                       In n p!!!s).
(* Goal: forall (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), @In positive n (successors_list make_predecessors s) *)
  unfold make_predecessors.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  apply PTree_Properties.fold_rec with (P := P); unfold P; intros.
(* extensionality *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply H0; auto. rewrite H; auto.
(* base case *)
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  rewrite PTree.gempty in H; congruence.
(* inductive case *)
(* Goal: @In positive n (successors_list (add_successors a k (successors v)) s) *)
  apply add_successors_correct.
(* Goal: In n (PHeap.insert k a) *)
  rewrite PTree.gsspec in H2. destruct (peq n k).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  inv H2. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
Qed.

Lemma make_predecessors_correct_2:
  forall n instr s,
  code!n = Some instr -> In s (successors instr) ->
  exists l, make_predecessors!s = Some l /\ In n l.
Proof.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  intros. exploit make_predecessors_correct_1; eauto.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold successors_list. destruct (make_predecessors!s); simpl; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exists l; auto.
(* Goal: @eq bool false true *)
(* Goal: @eq bool match l with | nil => @proj_sumbool (@eq positive p n) (not (@eq positive p n)) (peq p n) | cons p l => true end true *)
  contradiction.
Qed.

Lemma reachable_predecessors:
  forall p q,
  reachable code successors p q ->
  reachable make_predecessors (fun l => l) q p.
Proof.
(* Goal: forall (p q : positive) (_ : @reachable A code successors p q), @reachable (list positive) make_predecessors (fun l : list positive => l) q p *)
  induction 1.
- constructor.
- exploit make_predecessors_correct_2; eauto. intros [l [P Q]].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply reachable_right; eauto.
Qed.

End Predecessor.

(** ** Solving backward dataflow problems *)

(** The interface to a backward dataflow solver is as follows. *)

Module Type BACKWARD_DATAFLOW_SOLVER.

  Declare Module L: SEMILATTICE.

  (** [fixpoint successors transf] is the solver.
    It returns either an error or a mapping from program points to
    values of type [L.t] representing the solution.  [successors]
    is a finite map returning the list of successors of the given program
    point. [transf] is the transfer function. *)

  Parameter fixpoint:
    forall {A: Type} (code: PTree.t A) (successors: A -> list positive)
           (transf: positive -> L.t -> L.t),
    option (PMap.t L.t).

  (** The [fixpoint_solution] theorem shows that the returned solution,
    if any, satisfies the backward dataflow inequations. *)

  Axiom fixpoint_solution:
    forall A (code: PTree.t A) successors transf res n instr s,
    fixpoint code successors transf = Some res ->
    code!n = Some instr -> In s (successors instr) ->
    (forall n a, code!n = None -> L.eq (transf n a) L.bot) ->
    L.ge res!!n (transf s res!!s).

  (** [fixpoint_allnodes] is a variant of [fixpoint], less algorithmically
    efficient, but correct without any hypothesis on the transfer function. *)

  Parameter fixpoint_allnodes:
    forall {A: Type} (code: PTree.t A) (successors: A -> list positive)
           (transf: positive -> L.t -> L.t),
    option (PMap.t L.t).

  Axiom fixpoint_allnodes_solution:
    forall A (code: PTree.t A) successors transf res n instr s,
    fixpoint_allnodes code successors transf = Some res ->
    code!n = Some instr -> In s (successors instr) ->
    L.ge res!!n (transf s res!!s).

End BACKWARD_DATAFLOW_SOLVER.

(** We construct a generic backward dataflow solver, working over any
  semi-lattice structure, by applying the forward dataflow solver
  with the predecessor relation instead of the successor relation. *)

Module Backward_Dataflow_Solver (LAT: SEMILATTICE) (NS: NODE_SET):
                   BACKWARD_DATAFLOW_SOLVER with Module L := LAT.

Module L := LAT.

Module DS := Dataflow_Solver L NS.

Section Kildall.

Context {A: Type}.
Variable code: PTree.t A.
Variable successors: A -> list positive.
Variable transf: positive -> L.t -> L.t.

(** Finding entry points for the reverse control-flow graph. *)

Section Exit_points.

(** Assuming that the nodes of the CFG [code] are numbered in reverse
  postorder (cf. pass [Renumber]), an edge from [n] to [s] is a
  normal edge if [s < n] and a back-edge otherwise.
  [sequential_node] returns [true] if the given node has at least one
  normal outgoing edge.  It returns [false] if the given node is an exit
  node (no outgoing edges) or the final node of a loop body
  (all outgoing edges are back-edges).  As we prove later, the set
  of all non-sequential node is an appropriate set of entry points
  for exploring the reverse CFG. *)

Definition sequential_node (pc: positive) (instr: A): bool :=
  existsb (fun s => match code!s with None => false | Some _ => plt s pc end)
          (successors instr).

Definition exit_points : NS.t :=
  PTree.fold
    (fun ep pc instr =>
       if sequential_node pc instr
       then ep
       else NS.add pc ep)
    code NS.empty.

Lemma exit_points_charact:
  forall n,
  NS.In n exit_points <-> exists i, code!n = Some i /\ sequential_node n i = false.
Proof.
(* Goal: None *)
  intros n. unfold exit_points. eapply PTree_Properties.fold_rec.
- (* extensionality *)
  intros. rewrite <- H. auto.
- (* base case *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  simpl. split; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eelim NS.empty_spec; eauto.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  destruct H as [i [P Q]]. rewrite PTree.gempty in P. congruence.
- (* inductive case *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros. destruct (sequential_node k v) eqn:SN.
  + rewrite H1. rewrite PTree.gsspec. destruct (peq n k).
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    subst. split; intros [i [P Q]]. congruence. inv P. congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    tauto.
  + rewrite NS.add_spec. rewrite H1. rewrite PTree.gsspec. destruct (peq n k).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    subst. split. intros. exists v; auto. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    split. intros [P | [i [P Q]]]. congruence. exists i; auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    intros [i [P Q]]. right; exists i; auto.
Qed.

Lemma reachable_exit_points:
  forall pc i,
  code!pc = Some i -> exists x, NS.In x exit_points /\ reachable code successors pc x.
Proof.
(* Goal: None *)
  intros pc0. pattern pc0. apply (well_founded_ind Plt_wf).
(* Goal: None *)
  intros pc HR i CODE.
(* Goal: None *)
  destruct (sequential_node pc i) eqn:SN.
- (* at least one successor that decreases the pc *)
(* Goal: None *)
  unfold sequential_node in SN. rewrite existsb_exists in SN.
(* Goal: @eq positive n enode *)
  destruct SN as [s [P Q]]. destruct (code!s) as [i'|] eqn:CS; try discriminate. InvBooleans.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (HR s); eauto. intros [x [U V]].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exists x; split; auto. eapply reachable_left; eauto.
- (* otherwise we are an exit point *)
(* Goal: and (@In positive a (worklist (propagate_successors bb succs l st1))) (@eq L.t (@PMap.get L.t a (aval (propagate_successors bb succs l st1))) l) *)
(* Goal: and (@In positive n (worklist (propagate_successors bb succs l st1))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st1))) l) *)
(* Goal: @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st1))) (@PMap.get L.t n (aval st)) *)
  exists pc; split.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  rewrite exit_points_charact. exists i; auto. constructor.
Qed.

(** The crucial property of exit points is that any nonempty node in the
  CFG is reverse-reachable from an exit point. *)

Lemma reachable_exit_points_predecessor:
  forall pc i,
  code!pc = Some i ->
  exists x, NS.In x exit_points /\ reachable (make_predecessors code successors) (fun l => l) x pc.
Proof.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  intros. exploit reachable_exit_points; eauto. intros [x [P Q]].
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exists x; split; auto. apply reachable_predecessors. auto.
Qed.

End Exit_points.

(** The efficient backward solver.  *)

Definition fixpoint :=
  DS.fixpoint_nodeset
    (make_predecessors code successors) (fun l => l)
    transf exit_points.

Theorem fixpoint_solution:
  forall res n instr s,
  fixpoint = Some res ->
  code!n = Some instr -> In s (successors instr) ->
  (forall n a, code!n = None -> L.eq (transf n a) L.bot) ->
  L.ge res!!n (transf s res!!s).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (make_predecessors_correct_2 code); eauto. intros [l [P Q]].
  destruct code!s as [instr'|] eqn:CS.
- exploit reachable_exit_points_predecessor. eexact CS. intros (ep & U & V).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  unfold fixpoint in H. eapply DS.fixpoint_nodeset_solution; eauto.
- apply L.ge_trans with L.bot. apply L.ge_bot.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply L.ge_refl. apply L.eq_sym. auto.
Qed.

(** The alternate solver that starts with all nodes of the CFG instead
  of just the exit points. *)

Definition fixpoint_allnodes :=
  DS.fixpoint_allnodes
    (make_predecessors code successors) (fun l => l)
    transf.

Theorem fixpoint_allnodes_solution:
  forall res n instr s,
  fixpoint_allnodes = Some res ->
  code!n = Some instr -> In s (successors instr) ->
  L.ge res!!n (transf s res!!s).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit (make_predecessors_correct_2 code); eauto. intros [l [P Q]].
  unfold fixpoint_allnodes in H.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply DS.fixpoint_allnodes_solution; eauto.
Qed.

End Kildall.

End Backward_Dataflow_Solver.

(** * Analysis on extended basic blocks *)

(** We now define an approximate solver for forward dataflow problems
  that proceeds by forward propagation over extended basic blocks.
  In other terms, program points with multiple predecessors are mapped
  to [L.top] (the greatest, or coarsest, approximation) and the other
  program points are mapped to [transf p X[p]] where [p] is their unique
  predecessor.

  This analysis applies to any type of approximations equipped with
  an ordering and a greatest element. *)

Module Type ORDERED_TYPE_WITH_TOP.

  Parameter t: Type.
  Parameter ge: t -> t -> Prop.
  Parameter top: t.
  Axiom top_ge: forall x, ge top x.
  Axiom refl_ge: forall x, ge x x.

End ORDERED_TYPE_WITH_TOP.

(** The interface of the solver is similar to that of Kildall's forward
  solver, with a slightly different statement of the invariant
  preservation property [fixpoint_invariant]. *)

Module Type BBLOCK_SOLVER.

  Declare Module L: ORDERED_TYPE_WITH_TOP.

  Parameter fixpoint:
    forall {A: Type} (code: PTree.t A) (successors: A -> list positive)
           (transf: positive -> L.t -> L.t)
           (entrypoint: positive),
    option (PMap.t L.t).

  Axiom fixpoint_solution:
    forall A (code: PTree.t A) successors transf entrypoint res n instr s,
    fixpoint code successors transf entrypoint = Some res ->
    code!n = Some instr -> In s (successors instr) ->
    L.ge res!!s (transf n res!!n).

  Axiom fixpoint_entry:
    forall A (code: PTree.t A) successors transf entrypoint res,
    fixpoint code successors transf entrypoint = Some res ->
    res!!entrypoint = L.top.

  Axiom fixpoint_invariant:
    forall A (code: PTree.t A) successors transf entrypoint
           (P: L.t -> Prop),
    P L.top ->
    (forall pc instr x, code!pc = Some instr -> P x -> P (transf pc x)) ->
    forall res pc,
    fixpoint code successors transf entrypoint = Some res ->
    P res!!pc.

End BBLOCK_SOLVER.

(** The implementation of the ``extended basic block'' solver is a
  functor parameterized by any ordered type with a top element. *)

Module BBlock_solver(LAT: ORDERED_TYPE_WITH_TOP):
                        BBLOCK_SOLVER with Module L := LAT.

Module L := LAT.

Section Solver.

Context {A: Type}.
Variable code: PTree.t A.
Variable successors: A -> list positive.
Variable transf: positive -> L.t -> L.t.
Variable entrypoint: positive.
Variable P: L.t -> Prop.
Hypothesis Ptop: P L.top.
Hypothesis Ptransf: forall pc instr x, code!pc = Some instr -> P x -> P (transf pc x).

Definition bbmap := positive -> bool.
Definition result := PMap.t L.t.

(** As in Kildall's solver, the state of the iteration has two components:
- A mapping from program points to values of type [L.t] representing
  the candidate solution found so far.
- A worklist of program points that remain to be considered.
*)

Record state : Type := mkstate
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  { aval: result; worklist: list positive }.

(** The ``extended basic block'' algorithm, in pseudo-code, is as follows:
<<
    worklist := the set of all points n having multiple predecessors
    aval  := the mapping n -> L.top

    while worklist is not empty, do
        extract a node n from worklist
        compute out = transf n aval[n]
        for each successor s of n:
            if s has only one predecessor (namely, n):
                aval[s] := out
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
                worklist := worklist union {s}
            end if
        end for
    end while
    return aval
>>
**)

Fixpoint propagate_successors
    (bb: bbmap) (succs: list positive) (l: L.t) (st: state)
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
    {struct succs} : state :=
  match succs with
  | nil => st
  | s1 :: sl =>
      if bb s1 then
        propagate_successors bb sl l st
      else
        propagate_successors bb sl l
          (mkstate (PMap.set s1 l st.(aval))
                   (s1 :: st.(worklist)))
  end.

Definition step (bb: bbmap) (st: state) : result + state :=
  match st.(worklist) with
  | nil => inl _ st.(aval)
  | pc :: rem =>
      match code!pc with
      | None =>
          inr _ (mkstate st.(aval) rem)
      | Some instr =>
          inr _ (propagate_successors
                   bb (successors instr)
                   (transf pc st.(aval)!!pc)
                   (mkstate st.(aval) rem))
      end
  end.

(** Recognition of program points that have more than one predecessor. *)

Definition is_basic_block_head
    (preds: PTree.t (list positive)) (pc: positive) : bool :=
  if peq pc entrypoint then true else
    match preds!!!pc with
    | nil => false
    | s :: nil => peq s pc
    | _ :: _ :: _ => true
    end.

Definition basic_block_map : bbmap :=
  is_basic_block_head (make_predecessors code successors).

Definition basic_block_list (bb: bbmap) : list positive :=
  PTree.fold (fun l pc instr => if bb pc then pc :: l else l)
             code nil.

(** The computation of the approximate solution. *)

Definition fixpoint : option result :=
  let bb := basic_block_map in
  PrimIter.iterate _ _ (step bb) (mkstate (PMap.init L.top) (basic_block_list bb)).

(** ** Properties of predecessors and multiple-predecessors nodes *)

Definition predecessors := make_predecessors code successors.

Lemma predecessors_correct:
  forall n instr s,
  code!n = Some instr -> In s (successors instr) -> In n predecessors!!!s.
Proof.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  intros. unfold predecessors. eapply make_predecessors_correct_1; eauto.
Qed.

Lemma multiple_predecessors:
  forall s n1 instr1 n2 instr2,
  code!n1 = Some instr1 -> In s (successors instr1) ->
  code!n2 = Some instr2 -> In s (successors instr2) ->
  n1 <> n2 ->
  basic_block_map s = true.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  assert (In n1 predecessors!!!s). eapply predecessors_correct; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  assert (In n2 predecessors!!!s). eapply predecessors_correct; eauto.
(* Goal: @eq bool (basic_block_map n) true *)
  unfold basic_block_map, is_basic_block_head.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  destruct (peq s entrypoint). auto.
(* Goal: @eq bool match successors_list (@make_predecessors A code successors) n with | nil => false | cons s (nil as l) => @proj_sumbool (@eq positive s n) (not (@eq positive s n)) (peq s n) | cons s (cons p l0 as l) => true end true *)
  fold predecessors.
(* Goal: @eq bool match successors_list predecessors s with | nil => false | cons s0 (nil as l) => @proj_sumbool (@eq positive s0 s) (not (@eq positive s0 s)) (peq s0 s) | cons s0 (cons p l0 as l) => true end true *)
  destruct (predecessors!!!s).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
(* Goal: @eq bool match l with | nil => @proj_sumbool (@eq positive p n) (not (@eq positive p n)) (peq p n) | cons p l => true end true *)
  destruct l.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  apply proj_sumbool_is_true. simpl in *. intuition congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
Qed.

Lemma no_self_loop:
  forall n instr,
  code!n = Some instr -> In n (successors instr) -> basic_block_map n = true.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros. unfold basic_block_map, is_basic_block_head.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  destruct (peq n entrypoint). auto.
(* Goal: @eq bool match successors_list (@make_predecessors A code successors) n with | nil => false | cons s (nil as l) => @proj_sumbool (@eq positive s n) (not (@eq positive s n)) (peq s n) | cons s (cons p l0 as l) => true end true *)
  fold predecessors.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  exploit predecessors_correct; eauto. intros.
(* Goal: @eq bool match successors_list predecessors n with | nil => false | cons s (nil as l) => @proj_sumbool (@eq positive s n) (not (@eq positive s n)) (peq s n) | cons s (cons p l0 as l) => true end true *)
  destruct (predecessors!!!n).
(* Goal: @eq bool false true *)
(* Goal: @eq bool match l with | nil => @proj_sumbool (@eq positive p n) (not (@eq positive p n)) (peq p n) | cons p l => true end true *)
  contradiction.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  destruct l. apply proj_sumbool_is_true. simpl in H1. tauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
Qed.

(** ** Correctness invariant *)

(** The invariant over the state is as follows:
- Points with several predecessors are mapped to [L.top]
- Points not in the worklist satisfy their inequations
  (as in Kildall's algorithm).
*)

Definition state_invariant (st: state) : Prop :=
  (forall n, basic_block_map n = true -> st.(aval)!!n = L.top)
/\
  (forall n,
   In n st.(worklist) \/
   (forall instr s, code!n = Some instr -> In s (successors instr) ->
               L.ge st.(aval)!!s (transf n st.(aval)!!n))).

Lemma propagate_successors_charact1:
  forall bb succs l st,
  incl st.(worklist)
       (propagate_successors bb succs l st).(worklist).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  induction succs; simpl; intros.
(* Goal: @incl positive (worklist st) (worklist st) *)
(* Goal: @incl positive (@cons positive a (worklist st)) (worklist (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st))))) *)
  apply incl_refl.
(* Goal: Pstate (if bb a then propagate_successors bb succs l st else propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))) *)
  case (bb a).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
(* Goal: @incl positive (worklist st) (worklist (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st))))) *)
  apply incl_tran with (a :: worklist st).
(* Goal: @incl positive (worklist st) (worklist st) *)
(* Goal: @incl positive (@cons positive a (worklist st)) (worklist (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st))))) *)
  apply incl_tl. apply incl_refl.
(* Goal: and (forall (_ : or (@eq positive a n) (@In positive n succs)) (_ : @eq bool (bb n) false), and (@In positive n (worklist (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) l)) (forall _ : or (not (or (@eq positive a n) (@In positive n succs))) (@eq bool (bb n) true), @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@PMap.get L.t n (aval st))) *)
  set (st1 := (mkstate (PMap.set a l (aval st)) (a :: worklist st))).
(* Goal: @incl positive (@cons positive a (worklist st)) (worklist (propagate_successors bb succs l st1)) *)
  change (a :: worklist st) with (worklist st1).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
Qed.

Lemma propagate_successors_charact2:
  forall bb succs l st n,
  let st' := propagate_successors bb succs l st in
  (In n succs -> bb n = false -> In n st'.(worklist) /\ st'.(aval)!!n = l)
/\ (~In n succs \/ bb n = true -> st'.(aval)!!n = st.(aval)!!n).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  induction succs; simpl; intros.
  (* Base case *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  split. tauto. auto.
  (* Inductive case *)
(* Goal: and (forall (_ : or (@eq positive a n) (@In positive n succs)) (_ : @eq bool (bb n) false), and (@In positive n (worklist (if bb a then propagate_successors bb succs l st else propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@eq L.t (@PMap.get L.t n (aval (if bb a then propagate_successors bb succs l st else propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) l)) (forall _ : or (not (or (@eq positive a n) (@In positive n succs))) (@eq bool (bb n) true), @eq L.t (@PMap.get L.t n (aval (if bb a then propagate_successors bb succs l st else propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@PMap.get L.t n (aval st))) *)
  caseEq (bb a); intro.
(* Goal: and (forall (_ : or (@eq positive a n) (@In positive n succs)) (_ : @eq bool (bb n) false), and (@In positive n (worklist (propagate_successors bb succs l st))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st))) l)) (forall _ : or (not (or (@eq positive a n) (@In positive n succs))) (@eq bool (bb n) true), @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st))) (@PMap.get L.t n (aval st))) *)
(* Goal: and (forall (_ : or (@eq positive a n) (@In positive n succs)) (_ : @eq bool (bb n) false), and (@In positive n (worklist (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) l)) (forall _ : or (not (or (@eq positive a n) (@In positive n succs))) (@eq bool (bb n) true), @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@PMap.get L.t n (aval st))) *)
  elim (IHsuccs l st n); intros U V.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  split; intros. apply U; auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  elim H0; intro. subst a. congruence. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply V. tauto.
(* Goal: and (forall (_ : or (@eq positive a n) (@In positive n succs)) (_ : @eq bool (bb n) false), and (@In positive n (worklist (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) l)) (forall _ : or (not (or (@eq positive a n) (@In positive n succs))) (@eq bool (bb n) true), @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l (mkstate (@PMap.set L.t a l (aval st)) (@cons positive a (worklist st)))))) (@PMap.get L.t n (aval st))) *)
  set (st1 := mkstate (PMap.set a l (aval st)) (a :: worklist st)).
(* Goal: and (forall (_ : or (@eq positive a n) (@In positive n succs)) (_ : @eq bool (bb n) false), and (@In positive n (worklist (propagate_successors bb succs l st1))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st1))) l)) (forall _ : or (not (or (@eq positive a n) (@In positive n succs))) (@eq bool (bb n) true), @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st1))) (@PMap.get L.t n (aval st))) *)
  elim (IHsuccs l st1 n); intros U V.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  split; intros.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  elim H0; intros.
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
  subst n. split.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply propagate_successors_charact1. simpl. tauto.
(* Goal: @eq L.t (@PMap.get L.t a (aval (propagate_successors bb succs l st1))) l *)
(* Goal: and (@In positive n (worklist (propagate_successors bb succs l st1))) (@eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st1))) l) *)
(* Goal: @eq L.t (@PMap.get L.t n (aval (propagate_successors bb succs l st1))) (@PMap.get L.t n (aval st)) *)
  case (In_dec peq a succs); intro.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  elim (U i H1); auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  rewrite V. unfold st1; simpl. apply PMap.gss. tauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply U; auto.
(* Goal: @In positive n (worklist (mkstate res rem)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  rewrite V. unfold st1; simpl. apply PMap.gso.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  red; intro; subst n. elim H0; intro. tauto. congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  tauto.
Qed.

Lemma propagate_successors_invariant:
  forall pc instr res rem,
  code!pc = Some instr ->
  state_invariant (mkstate res (pc :: rem)) ->
  state_invariant
    (propagate_successors basic_block_map (successors instr)
                          (transf pc res!!pc)
                          (mkstate res rem)).
Proof.
(* Goal: forall (pc : positive) (res : result) (rem : list positive) (_ : @eq (option A) (@PTree.get A pc code) (@None A)) (_ : state_invariant (mkstate res (@cons positive pc rem))), state_invariant (mkstate res rem) *)
  intros until rem. intros CODE [INV1 INV2]. simpl in INV1. simpl in INV2.
(* Goal: state_invariant (propagate_successors basic_block_map (successors instr) (transf pc (@PMap.get L.t pc res)) (mkstate res rem)) *)
  set (l := transf pc res!!pc).
  generalize (propagate_successors_charact1 basic_block_map
                (successors instr) l (mkstate res rem)).
  generalize (propagate_successors_charact2 basic_block_map
                (successors instr) l (mkstate res rem)).
  set (st1 := propagate_successors basic_block_map
                 (successors instr) l (mkstate res rem)).
(* Goal: forall (_ : forall n : positive, let st' := st1 in and (forall (_ : @In positive n (successors instr)) (_ : @eq bool (basic_block_map n) false), and (@In positive n (worklist st')) (@eq L.t (@PMap.get L.t n (aval st')) l)) (forall _ : or (not (@In positive n (successors instr))) (@eq bool (basic_block_map n) true), @eq L.t (@PMap.get L.t n (aval st')) (@PMap.get L.t n (aval (mkstate res rem))))) (_ : @incl positive (worklist (mkstate res rem)) (worklist st1)), state_invariant st1 *)
  intros U V. simpl in U.
  (* First part: BB entries remain at top *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  split; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  elim (U n); intros C D. rewrite D. simpl. apply INV1. auto. tauto.
  (* Second part: monotonicity *)
  (* Case 1: n = pc *)
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
  destruct (peq pc n). subst n.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  right; intros.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  assert (instr0 = instr) by congruence. subst instr0.
(* Goal: @eq L.t (@PMap.get L.t s (aval st1)) (@PMap.get L.t s res) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  elim (U s); intros C D.
(* Goal: L.ge (@PMap.get L.t s (aval st1)) (transf pc (@PMap.get L.t pc (aval st1))) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  replace (st1.(aval)!!pc) with res!!pc. fold l.
(* Goal: L.ge (@PMap.get L.t s (aval st1)) l *)
(* Goal: @eq L.t (@PMap.get L.t pc res) (@PMap.get L.t pc (aval st1)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  destruct (basic_block_map s) eqn:BB.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  rewrite D. simpl. rewrite INV1. apply L.top_ge. auto. tauto.
(* Goal: L.ge (@PMap.get L.t s (aval st1)) l *)
(* Goal: @eq L.t (@PMap.get L.t pc res) (@PMap.get L.t pc (aval st1)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  elim (C H0 (eq_refl _)). intros X Y. rewrite Y. apply L.refl_ge.
(* Goal: @eq L.t (@PMap.get L.t pc res) (@PMap.get L.t pc (aval st1)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  elim (U pc); intros E F. rewrite F. reflexivity.
(* Goal: or (not (@In positive pc (successors instr))) (@eq bool (basic_block_map pc) true) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  destruct (In_dec peq pc (successors instr)).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  right. eapply no_self_loop; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  left; auto.
  (* Case 2: n <> pc *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  elim (INV2 n); intro.
  (* Case 2.1: n was already in worklist, still is *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  left. apply V. simpl. tauto.
  (* Case 2.2: n was not in worklist *)
  assert (INV3: forall s instr', code!n = Some instr' -> In s (successors instr') -> st1.(aval)!!s = res!!s).
    (* Amazingly, successors of n do not change.  The only way
       they could change is if they were successors of pc as well,
       but that gives them two different predecessors, so
       they are basic block heads, and thus do not change! *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros. elim (U s); intros C D. rewrite D. reflexivity.
(* Goal: or (not (@In positive s (successors instr))) (@eq bool (basic_block_map s) true) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
    destruct (In_dec peq s (successors instr)).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    right. eapply multiple_predecessors with (n1 := pc) (n2 := n); eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    left; auto.
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  destruct (In_dec peq n (successors instr)).
  (* Case 2.2.1: n is a successor of pc. Either it is in the
     worklist or it did not change *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  destruct (basic_block_map n) eqn:BB.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  right; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  elim (U n); intros C D. rewrite D. erewrite INV3; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  tauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  left. elim (U n); intros C D. elim (C i BB); intros. auto.
  (* Case 2.2.2: n is not a successor of pc. It did not change. *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  right; intros.
(* Goal: L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1))) *)
  elim (U n); intros C D. rewrite D.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  erewrite INV3; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  tauto.
Qed.

Lemma propagate_successors_invariant_2:
  forall pc res rem,
  code!pc = None ->
  state_invariant (mkstate res (pc :: rem)) ->
  state_invariant (mkstate res rem).
Proof.
(* Goal: forall (pc : positive) (res : result) (rem : list positive) (_ : @eq (option A) (@PTree.get A pc code) (@None A)) (_ : state_invariant (mkstate res (@cons positive pc rem))), state_invariant (mkstate res rem) *)
  intros until rem. intros CODE [INV1 INV2]. simpl in INV1. simpl in INV2.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  split; simpl; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply INV1; auto.
(* Goal: or (@In positive n rem) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s res) (transf n (@PMap.get L.t n res))) *)
  destruct (INV2 n) as [[U | U] | U].
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  subst n. right; intros; congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
Qed.

Lemma initial_state_invariant:
  state_invariant (mkstate (PMap.init L.top) (basic_block_list basic_block_map)).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  split; simpl; intros.
(* Goal: @eq L.t (@PMap.get L.t n (@PMap.init L.t L.top)) L.top *)
(* Goal: or (@In positive n (basic_block_list basic_block_map)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (@PMap.init L.t L.top)) (transf n (@PMap.get L.t n (@PMap.init L.t L.top)))) *)
  apply PMap.gi.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  right. intros. repeat rewrite PMap.gi. apply L.top_ge.
Qed.

Lemma analyze_invariant:
  forall res,
  fixpoint = Some res ->
  state_invariant (mkstate res nil).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint; intros. pattern res.
  eapply (PrimIter.iterate_prop _ _ (step basic_block_map)
           state_invariant).

  intros st INV. destruct st as [stin stwrk].
(* Goal: @In positive n (worklist (mkstate res rem)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  unfold step. simpl. destruct stwrk as [ | pc rem ] eqn:WRK.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
  destruct (code!pc) as [instr|] eqn:CODE.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply propagate_successors_invariant; eauto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply propagate_successors_invariant_2; eauto.

(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eauto. apply initial_state_invariant.
Qed.

(** ** Correctness of the returned solution *)

Theorem fixpoint_solution:
  forall res n instr s,
  fixpoint = Some res ->
  code!n = Some instr -> In s (successors instr) ->
  L.ge res!!s (transf n res!!n).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros.
  assert (state_invariant (mkstate res nil)).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply analyze_invariant; eauto.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  elim H2; simpl; intros.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  elim (H4 n); intros.
(* Goal: @eq bool false true *)
(* Goal: @eq bool match l with | nil => @proj_sumbool (@eq positive p n) (not (@eq positive p n)) (peq p n) | cons p l => true end true *)
  contradiction.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eauto.
Qed.

Theorem fixpoint_entry:
  forall res,
  fixpoint = Some res ->
  res!!entrypoint = L.top.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  intros.
  assert (state_invariant (mkstate res nil)).
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eapply analyze_invariant; eauto.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  elim H0; simpl; intros.
(* Goal: P match @PTree.get L.t x (aval st) with | Some v => v | None => L.bot end *)
  apply H1. unfold basic_block_map, is_basic_block_head.
(* Goal: @eq bool match successors_list (@make_predecessors A code successors) n with | nil => false | cons s (nil as l) => @proj_sumbool (@eq positive s n) (not (@eq positive s n)) (peq s n) | cons s (cons p l0 as l) => true end true *)
  fold predecessors. apply peq_true.
Qed.

(** ** Preservation of a property over solutions *)

Definition Pstate (st: state) : Prop :=
  forall pc, P st.(aval)!!pc.

Lemma propagate_successors_P:
  forall bb l,
  P l ->
  forall succs st,
  Pstate st ->
  Pstate (propagate_successors bb succs l st).
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  induction succs; simpl; intros.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  case (bb a). auto.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  apply IHsuccs. red; simpl; intros.
(* Goal: P (@PMap.get L.t pc (@PMap.set L.t a l (aval st))) *)
  rewrite PMap.gsspec. case (peq pc a); intro.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto. apply H0.
Qed.

Theorem fixpoint_invariant:
  forall res pc, fixpoint = Some res -> P res!!pc.
Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
  unfold fixpoint; intros. pattern res.
  eapply (PrimIter.iterate_prop _ _ (step basic_block_map) Pstate).

  intros st PS. unfold step. destruct (st.(worklist)).
  apply PS.
  assert (PS2: Pstate (mkstate st.(aval) l)).
(* Goal: @In positive n (worklist (mkstate res rem)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
    red; intro; simpl. apply PS.
  destruct (code!p) as [instr|] eqn:CODE.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  apply propagate_successors_P. eauto. auto.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  auto.

(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
  eauto.
(* Goal: @In positive n (worklist (mkstate res rem)) *)
(* Goal: or (@In positive n (worklist st1)) (forall (instr : A) (s : positive) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)) (_ : @In positive s (successors instr)), L.ge (@PMap.get L.t s (aval st1)) (transf n (@PMap.get L.t n (aval st1)))) *)
  red; intro; simpl. rewrite PMap.gi. apply Ptop.
Qed.

End Solver.

End BBlock_solver.

(** ** Node sets *)

(** We now define implementations of the [NODE_SET] interface
  appropriate for forward and backward dataflow analysis.
  As mentioned earlier, we aim for a traversal of the CFG nodes
  in reverse postorder (for forward analysis) or postorder
  (for backward analysis).  We take advantage of the following
  fact, valid for all CFG generated by translation from Cminor:
  the enumeration [n-1], [n-2], ..., 3, 2, 1 where [n] is the
  top CFG node is a reverse postorder traversal.
  Therefore, for forward analysis, we will use an implementation
  of [NODE_SET] where the [pick] operation selects the
  greatest node in the working list.  For backward analysis,
  we will similarly pick the smallest node in the working list. *)

Require Import Heaps.

Module NodeSetForward <: NODE_SET.
  Definition t := PHeap.t.
  Definition empty := PHeap.empty.
  Definition add (n: positive) (s: t) : t := PHeap.insert n s.
  Definition pick (s: t) :=
    match PHeap.findMax s with
    | Some n => Some(n, PHeap.deleteMax s)
    | None => None
    end.
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  Definition all_nodes {A: Type} (code: PTree.t A) :=
    PTree.fold (fun s pc instr => PHeap.insert pc s) code PHeap.empty.
  Definition In := PHeap.In.

  Lemma empty_spec:
    forall n, ~In n empty.
  Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros. apply PHeap.In_empty.
  Qed.

  Lemma add_spec:
    forall n n' s, In n' (add n s) <-> n = n' \/ In n' s.
  Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros. rewrite PHeap.In_insert. unfold In. intuition.
  Qed.

  Lemma pick_none:
    forall s n, pick s = None -> ~In n s.
  Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros until n; unfold pick. caseEq (PHeap.findMax s); intros.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply PHeap.findMax_empty. auto.
  Qed.

  Lemma pick_some:
    forall s n s', pick s = Some(n, s') ->
    forall n', In n' s <-> n = n' \/ In n' s'.
  Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros until s'; unfold pick. caseEq (PHeap.findMax s); intros.
(* Goal: iff (In n' s) (or (@eq positive n n') (In n' s')) *)
(* Goal: iff (In n' s) (or (@eq positive n n') (In n' s')) *)
    inv H0.
(* Goal: forall _ : iff (PHeap.In n' s) (or (@eq Ordered.OrderedPositive.t n' n) (PHeap.In n' (PHeap.deleteMax s))), iff (PHeap.In n' s) (or (@eq positive n n') (PHeap.In n' (PHeap.deleteMax s))) *)
(* Goal: iff (In n' s) (or (@eq positive n n') (In n' s')) *)
    generalize (PHeap.In_deleteMax s n n' H). unfold In. intuition.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    congruence.
  Qed.

  Lemma all_nodes_spec:
    forall A (code: PTree.t A) n instr,
    code!n = Some instr -> In n (all_nodes code).
  Proof.
(* Goal: forall (A : Type) (code : PTree.t A) (n : positive) (instr : A) (_ : @eq (option A) (@PTree.get A n code) (@Some A instr)), In n (@all_nodes A code) *)
    intros A code n instr.
    apply PTree_Properties.fold_rec with
      (P := fun m set => m!n = Some instr -> In n set).
    (* extensionality *)
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    intros. apply H0. rewrite H. auto.
    (* base case *)
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    rewrite PTree.gempty. congruence.
    (* inductive case *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros. rewrite PTree.gsspec in H2. rewrite add_spec.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    destruct (peq n k). auto. eauto.
  Qed.
End NodeSetForward.

Module NodeSetBackward <: NODE_SET.
  Definition t := PHeap.t.
  Definition empty := PHeap.empty.
  Definition add (n: positive) (s: t) : t := PHeap.insert n s.
  Definition pick (s: t) :=
    match PHeap.findMin s with
    | Some n => Some(n, PHeap.deleteMin s)
    | None => None
    end.
(* Goal: forall (st1 st2 : state) (_ : steps st1 st2) (_ : inv st1), inv st2 *)
(* Goal: P (@PMap.get L.t pc res) *)
  Definition all_nodes {A: Type} (code: PTree.t A) :=
    PTree.fold (fun s pc instr => PHeap.insert pc s) code PHeap.empty.
  Definition In := PHeap.In.

  Lemma empty_spec:
    forall n, ~In n empty.
  Proof NodeSetForward.empty_spec.

  Lemma add_spec:
    forall n n' s, In n' (add n s) <-> n = n' \/ In n' s.
  Proof NodeSetForward.add_spec.

  Lemma pick_none:
    forall s n, pick s = None -> ~In n s.
  Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros until n; unfold pick. caseEq (PHeap.findMin s); intros.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    congruence.
(* Goal: or (@eq positive k n) (In n a) *)
(* Goal: or (@eq positive k n) (In n a) *)
    apply PHeap.findMin_empty. auto.
  Qed.

  Lemma pick_some:
    forall s n s', pick s = Some(n, s') ->
    forall n', In n' s <-> n = n' \/ In n' s'.
  Proof.
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    intros until s'; unfold pick. caseEq (PHeap.findMin s); intros.
(* Goal: iff (In n' s) (or (@eq positive n n') (In n' s')) *)
(* Goal: iff (In n' s) (or (@eq positive n n') (In n' s')) *)
    inv H0.
(* Goal: forall _ : iff (PHeap.In n' s) (or (@eq Ordered.OrderedPositive.t n' n) (PHeap.In n' (PHeap.deleteMax s))), iff (PHeap.In n' s) (or (@eq positive n n') (PHeap.In n' (PHeap.deleteMax s))) *)
(* Goal: iff (In n' s) (or (@eq positive n n') (In n' s')) *)
    generalize (PHeap.In_deleteMin s n n' H). unfold In. intuition.
(* Goal: forall _ : @eq (option A) (@None A) (@Some A instr), In n PHeap.empty *)
(* Goal: forall (m : PTree.t A) (a : PHeap.t) (k : PTree.elt) (v : A) (_ : @eq (option A) (@PTree.get A k m) (@None A)) (_ : @eq (option A) (@PTree.get A k code) (@Some A v)) (_ : forall _ : @eq (option A) (@PTree.get A n m) (@Some A instr), In n a) (_ : @eq (option A) (@PTree.get A n (@PTree.set A k v m)) (@Some A instr)), In n (PHeap.insert k a) *)
    congruence.
  Qed.

  Lemma all_nodes_spec:
    forall A (code: PTree.t A) n instr,
    code!n = Some instr -> In n (all_nodes code).
  Proof NodeSetForward.all_nodes_spec.
End NodeSetBackward.

