Equations
- Lean.IR.UnreachableBranches.instReprValue = { reprPrec := [anonymous] }
Equations
- Lean.IR.UnreachableBranches.instToStringValue = { toString := fun v => toString (Lean.format v) }
Equations
- Lean.IR.UnreachableBranches.Value.instToStringValue = { toString := (fun f => Lean.Format.pretty f) ∘ Lean.IR.UnreachableBranches.Value.format }
def
Lean.IR.UnreachableBranches.Value.truncate
(env : Lean.Environment)
(v : Lean.IR.UnreachableBranches.Value)
(s : Lean.NameSet)
:
Equations
- Lean.IR.UnreachableBranches.Value.truncate env v s = (fun go => go v s Lean.IR.UnreachableBranches.Value.truncateMaxDepth) (Lean.IR.UnreachableBranches.Value.truncate.go env)
partial def
Lean.IR.UnreachableBranches.Value.truncate.go
(env : Lean.Environment)
(v : Lean.IR.UnreachableBranches.Value)
(s : Lean.NameSet)
(depth : Nat)
:
def
Lean.IR.UnreachableBranches.Value.widening
(env : Lean.Environment)
(v₁ : Lean.IR.UnreachableBranches.Value)
(v₂ : Lean.IR.UnreachableBranches.Value)
:
Equations
@[inline]
def
Lean.IR.UnreachableBranches.addFunctionSummary
(env : Lean.Environment)
(fid : Lean.IR.FunId)
(v : Lean.IR.UnreachableBranches.Value)
:
Equations
def
Lean.IR.UnreachableBranches.getFunctionSummary?
(env : Lean.Environment)
(fid : Lean.IR.FunId)
:
@[inline]
- currFnIdx : Nat
- decls : Array Lean.IR.Decl
- env : Lean.Environment
- lctx : Lean.IR.LocalContext
- assignments : Array Lean.IR.UnreachableBranches.Assignment
- funVals : Std.PArray Lean.IR.UnreachableBranches.Value
@[inline]
Equations
- Lean.IR.UnreachableBranches.findVarValue x = do let ctx ← read let s ← get let assignment : Lean.IR.UnreachableBranches.Assignment := Array.getOp s.assignments ctx.currFnIdx pure (Std.HashMap.findD assignment x Lean.IR.UnreachableBranches.Value.bot)
Equations
- Lean.IR.UnreachableBranches.findArgValue arg = match arg with | Lean.IR.Arg.var x => Lean.IR.UnreachableBranches.findVarValue x | x => pure Lean.IR.UnreachableBranches.Value.top
def
Lean.IR.UnreachableBranches.updateVarAssignment
(x : Lean.IR.VarId)
(v : Lean.IR.UnreachableBranches.Value)
:
Equations
- Lean.IR.UnreachableBranches.updateVarAssignment x v = do let v' ← Lean.IR.UnreachableBranches.findVarValue x let ctx ← read modify fun s => { assignments := Array.modify s.assignments ctx.currFnIdx fun a => Std.HashMap.insert a x (Lean.IR.UnreachableBranches.Value.merge v v'), funVals := s.funVals }
Equations
- Lean.IR.UnreachableBranches.resetVarAssignment x = do let ctx ← read modify fun s => { assignments := Array.modify s.assignments ctx.currFnIdx fun a => Std.HashMap.insert a x Lean.IR.UnreachableBranches.Value.bot, funVals := s.funVals }
Equations
- Lean.IR.UnreachableBranches.interpExpr x = match x with | Lean.IR.Expr.ctor i ys => do let a ← Array.mapM (fun y => Lean.IR.UnreachableBranches.findArgValue y) ys pure (Lean.IR.UnreachableBranches.Value.ctor i a) | Lean.IR.Expr.proj i x => do let a ← Lean.IR.UnreachableBranches.findVarValue x pure (Lean.IR.UnreachableBranches.projValue a i) | Lean.IR.Expr.fap fid ys => do let ctx ← read match Lean.IR.UnreachableBranches.getFunctionSummary? ctx.env fid with | some v => pure v | none => do let s ← get match Array.findIdx? ctx.decls fun decl => Lean.IR.Decl.name decl == fid with | some idx => pure (Std.PersistentArray.getOp s.funVals idx) | none => pure Lean.IR.UnreachableBranches.Value.top | x => pure Lean.IR.UnreachableBranches.Value.top
Equations
- Lean.IR.UnreachableBranches.updateCurrFnSummary v = do let ctx ← read let currFnIdx : Nat := ctx.currFnIdx modify fun s => { assignments := s.assignments, funVals := Std.PersistentArray.modify s.funVals currFnIdx fun v' => Lean.IR.UnreachableBranches.Value.widening ctx.env v v' }
def
Lean.IR.UnreachableBranches.updateJPParamsAssignment
(ys : Array Lean.IR.Param)
(xs : Array Lean.IR.Arg)
:
Equations
- Lean.IR.UnreachableBranches.updateJPParamsAssignment ys xs = do let ctx ← read let currFnIdx : Nat := ctx.currFnIdx Nat.foldM (fun i r => let y := Array.getOp ys i; let x := Array.getOp xs i; do let yVal ← Lean.IR.UnreachableBranches.findVarValue y.x let xVal ← Lean.IR.UnreachableBranches.findArgValue x let newVal : Lean.IR.UnreachableBranches.Value := Lean.IR.UnreachableBranches.Value.merge yVal xVal if (newVal == yVal) = true then pure r else do modify fun s => { assignments := Array.modify s.assignments currFnIdx fun a => Std.HashMap.insert a y.x newVal, funVals := s.funVals } pure true) false (Array.size ys)
Equations
- Lean.IR.UnreachableBranches.inferStep = do let ctx ← read modify fun s => { assignments := Array.map (fun x => ∅) ctx.decls, funVals := s.funVals } Nat.foldM (fun idx modified => match Array.getOp ctx.decls idx with | Lean.IR.Decl.fdecl x ys x_1 b x_2 => do let s ← get let currVals : Lean.IR.UnreachableBranches.Value := Std.PersistentArray.getOp s.funVals idx withReader (fun ctx => { currFnIdx := idx, decls := ctx.decls, env := ctx.env, lctx := ctx.lctx }) do Array.forM (fun y => Lean.IR.UnreachableBranches.updateVarAssignment y.x Lean.IR.UnreachableBranches.Value.top) ys 0 (Array.size ys) Lean.IR.UnreachableBranches.interpFnBody b let s ← get let newVals : Lean.IR.UnreachableBranches.Value := Std.PersistentArray.getOp s.funVals idx pure (modified || currVals != newVals) | Lean.IR.Decl.extern x x_1 x_2 x_3 => pure modified) false (Array.size ctx.decls)
partial def
Lean.IR.UnreachableBranches.elimDeadAux
(assignment : Lean.IR.UnreachableBranches.Assignment)
:
def
Lean.IR.UnreachableBranches.elimDead
(assignment : Lean.IR.UnreachableBranches.Assignment)
(d : Lean.IR.Decl)
:
Equations
- Lean.IR.UnreachableBranches.elimDead assignment d = match d with | Lean.IR.Decl.fdecl x x_1 x_2 b x_3 => Lean.IR.Decl.updateBody! d (Lean.IR.UnreachableBranches.elimDeadAux assignment b) | other => other
Equations
- Lean.IR.elimDeadBranches decls = do let s ← get let env : Lean.Environment := s.env let assignments : Array Lean.IR.UnreachableBranches.Assignment := Array.map (fun x => ∅) decls let funVals : Std.PArray Lean.IR.UnreachableBranches.Value := Std.mkPArray (Array.size decls) Lean.IR.UnreachableBranches.Value.bot let ctx : Lean.IR.UnreachableBranches.InterpContext := { currFnIdx := 0, decls := decls, env := env, lctx := ∅ } let s : Lean.IR.UnreachableBranches.InterpState := { assignments := assignments, funVals := funVals } let x : Id (Unit × Lean.IR.UnreachableBranches.InterpState) := StateT.run (Lean.IR.UnreachableBranches.inferMain ctx) s match x with | (x, s) => let funVals := s.funVals; let assignments := s.assignments; do modify fun s => let env := Nat.fold (fun i env => Lean.IR.UnreachableBranches.addFunctionSummary env (Lean.IR.Decl.name (Array.getOp decls i)) (Std.PersistentArray.getOp funVals i)) (Array.size decls) s.env; { env := env, log := s.log } pure (Array.mapIdx decls fun i decl => Lean.IR.UnreachableBranches.elimDead (Array.getOp assignments i.val) decl)