File: .lake/packages/lean4/src/lean/Lean/Compiler/IR/ExpandResetReuse.lean

full_name: Lean.IR.ExpandResetReuse.ProjMap
code: /-- Mapping from variable to projections -/
abbrev ProjMap  
goal: Type

full_name: Lean.IR.ExpandResetReuse.CollectProjMap.Collector
code: abbrev Collector 
goal: Type

full_name: Lean.IR.ExpandResetReuse.CollectProjMap.collectVDecl
code: @[inline] def collectVDecl (x : VarId) (v : Expr) : Collector 
goal: Lean.IR.ExpandResetReuse.CollectProjMap.Collector

full_name: Lean.IR.ExpandResetReuse.CollectProjMap.collectFnBody
code: partial def collectFnBody : FnBody → Collector
  
goal: Lean.IR.FnBody → Lean.IR.ExpandResetReuse.CollectProjMap.Collector

full_name: Lean.IR.ExpandResetReuse.mkProjMap
code: /-- Create a mapping from variables to projections.
   This function assumes variable ids have been normalized -/
def mkProjMap (d : Decl) : ProjMap 
goal: Lean.IR.ExpandResetReuse.ProjMap

full_name: Lean.IR.ExpandResetReuse.Context.projMap
code: structure Context where
  projMap : ProjMap
goal: Lean.IR.ExpandResetReuse.ProjMap

full_name: Lean.IR.ExpandResetReuse.Context
code: structure Context where
  projMap : ProjMap
goal: Type

full_name: Lean.IR.ExpandResetReuse.consumed
code: /-- Return true iff `x` is consumed in all branches of the current block.
   Here consumption means the block contains a `dec x` or `reuse x ...`. -/
partial def consumed (x : VarId) : FnBody → Bool
  
goal: Lean.IR.FnBody → Bool

full_name: Lean.IR.ExpandResetReuse.Mask
code: abbrev Mask 
goal: Type

full_name: Lean.IR.ExpandResetReuse.eraseProjIncForAux
code: /-- Auxiliary function for eraseProjIncFor -/
partial def eraseProjIncForAux (y : VarId) (bs : Array FnBody) (mask : Mask) (keep : Array FnBody) : Array FnBody × Mask 
goal: Array Lean.IR.FnBody × Lean.IR.ExpandResetReuse.Mask

full_name: Lean.IR.ExpandResetReuse.eraseProjIncFor
code: /-- Try to erase `inc` instructions on projections of `y` occurring in the tail of `bs`.
   Return the updated `bs` and a bit mask specifying which `inc`s have been removed. -/
def eraseProjIncFor (n : Nat) (y : VarId) (bs : Array FnBody) : Array FnBody × Mask 
goal: Array Lean.IR.FnBody × Lean.IR.ExpandResetReuse.Mask

full_name: Lean.IR.ExpandResetReuse.reuseToCtor
code: /-- Replace `reuse x ctor ...` with `ctor ...`, and remove `dec x` -/
partial def reuseToCtor (x : VarId) : FnBody → FnBody
  
goal: Lean.IR.FnBody → Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.mkSlowPath
code: /--
replace
```
x := reset y; b
```
with
```
inc z_1; ...; inc z_i; dec y; b'
```
where `z_i`'s are the variables in `mask`,
and `b'` is `b` where we removed `dec x` and replaced `reuse x ctor_i ...` with `ctor_i ...`.
-/
def mkSlowPath (x y : VarId) (mask : Mask) (b : FnBody) : FnBody 
goal: Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.M
code: abbrev M 
goal: Type

full_name: Lean.IR.ExpandResetReuse.mkFresh
code: def mkFresh : M VarId 
goal: Lean.IR.ExpandResetReuse.M Lean.IR.VarId

full_name: Lean.IR.ExpandResetReuse.releaseUnreadFields
code: def releaseUnreadFields (y : VarId) (mask : Mask) (b : FnBody) : M FnBody 
goal: Lean.IR.ExpandResetReuse.M Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.setFields
code: def setFields (y : VarId) (zs : Array Arg) (b : FnBody) : FnBody 
goal: Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.isSelfSet
code: /-- Given `set x[i] := y`, return true iff `y := proj[i] x` -/
def isSelfSet (ctx : Context) (x : VarId) (i : Nat) (y : Arg) : Bool 
goal: Bool

full_name: Lean.IR.ExpandResetReuse.isSelfUSet
code: /-- Given `uset x[i] := y`, return true iff `y := uproj[i] x` -/
def isSelfUSet (ctx : Context) (x : VarId) (i : Nat) (y : VarId) : Bool 
goal: Bool

full_name: Lean.IR.ExpandResetReuse.isSelfSSet
code: /-- Given `sset x[n, i] := y`, return true iff `y := sproj[n, i] x` -/
def isSelfSSet (ctx : Context) (x : VarId) (n : Nat) (i : Nat) (y : VarId) : Bool 
goal: Bool

full_name: Lean.IR.ExpandResetReuse.removeSelfSet
code: /-- Remove unnecessary `set/uset/sset` operations -/
partial def removeSelfSet (ctx : Context) : FnBody → FnBody
  
goal: Lean.IR.FnBody → Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.reuseToSet
code: partial def reuseToSet (ctx : Context) (x y : VarId) : FnBody → FnBody
  
goal: Lean.IR.FnBody → Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.mkFastPath
code: /--
replace
```
x := reset y; b
```
with
```
let f_i_1 := proj[i_1] y;
...
let f_i_k := proj[i_k] y;
b'
```
where `i_j`s are the field indexes
that the code did not touch immediately before the reset.
That is `mask[j] == none`.
`b'` is `b` where `y` `dec x` is replaced with `del y`,
and `z := reuse x ctor_i ws; F` is replaced with
`set x i ws[i]` operations, and we replace `z` with `x` in `F`
-/
def mkFastPath (x y : VarId) (mask : Mask) (b : FnBody) : M FnBody 
goal: Lean.IR.ExpandResetReuse.M Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.expand
code: partial def expand (mainFn : FnBody → Array FnBody → M FnBody)
    (bs : Array FnBody) (x : VarId) (n : Nat) (y : VarId) (b : FnBody) : M FnBody 
goal: Lean.IR.ExpandResetReuse.M Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.searchAndExpand
code: partial def searchAndExpand : FnBody → Array FnBody → M FnBody
  
goal: Lean.IR.FnBody → Array Lean.IR.FnBody → Lean.IR.ExpandResetReuse.M Lean.IR.FnBody

full_name: Lean.IR.ExpandResetReuse.main
code: def main (d : Decl) : Decl 
goal: Lean.IR.Decl

full_name: Lean.IR.Decl.expandResetReuse
code: /-- (Try to) expand `reset` and `reuse` instructions. -/
def Decl.expandResetReuse (d : Decl) : Decl 
goal: Lean.IR.Decl