Commit 7d5658e7 authored by Sapphie's avatar Sapphie
Browse files

Implement LetP and LetC

parent 545d8b91
......@@ -56,31 +56,66 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
shrink(tree, State(census(tree)))
private def shrink(tree: Tree, s: State): Tree = tree match {
case AppF(funAtom, retC, args) =>
funAtom match {
case AtomN(n) if s.fEnv.contains(n) && s.appliedOnce(n) =>
// Inline
val fun = s.fEnv(n)
val newState = s.withASubst(fun.args, args)
.withCSubst(fun.retC, retC)
val newBody = shrink(fun.body, newState)
newBody
case _ => tree
}
case LetF(funs, body) =>
val undeadFuns = funs.filter(fun => !s.dead(fun.name))
val undeadShrunkFuns = undeadFuns.map { fun =>
Fun(fun.name, fun.retC, fun.args, shrink(fun.body, s))}
val (funsAppliedOnce, restFuns) = undeadShrunkFuns
.partition(fun => s.appliedOnce(fun.name))
val nonInlined = undeadShrunkFuns
.filter(fun => !s.appliedOnce(fun.name))
val newState = s.withFuns(undeadShrunkFuns)
val newBody = shrink(body, newState)
if (restFuns.isEmpty) {
if (nonInlined.isEmpty) {
newBody
} else {
LetF(restFuns, newBody)
}
case AppF(fun, retC, args) =>
fun match {
case AtomN(n) if s.fEnv.contains(n) && s.appliedOnce(n) =>
// Inline
val fun = s.fEnv(n)
val newState = s.withASubst(fun.args, args)
.withCSubst(fun.retC, retC)
val newBody = shrink(fun.body, newState)
case LetP(name, prim, args, body) =>
if (s.dead(name) && !impure(prim)) {
shrink(body, s)
} else {
val replacedArgs = args map { a => s.aSubst.getOrElse(a, a)}
val primArgPair = (prim, replacedArgs)
if (!unstable(prim) && s.eInvEnv.contains(primArgPair)) {
val newS = s.withASubst(name, s.eInvEnv(primArgPair))
val newBody = shrink(body, newS)
newBody
case _ => tree
} else {
val newS = s.withExp(name, prim, replacedArgs)
LetP(name, prim, replacedArgs, shrink(body, newS))
}
}
case LetC(cnts, body) =>
val undeadConts = cnts.filter(cnt => !s.dead(cnt.name))
val undeadShrunkConts = undeadConts.map { cnt =>
val newBody = shrink(cnt.body, s)
Cnt(cnt.name, cnt.args, newBody)
}
val nonInlinedConts = undeadShrunkConts.filter { cnt =>
!s.appliedOnce(cnt.name)
}
val newBody = shrink(body, s.withCnts(undeadShrunkConts))
if (conts.isEmpty) {
newBody
} else {
LetC(undeadShrunkConts, newBody)
}
}
......@@ -233,7 +268,8 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
private[this] implicit def l3IntToLit(i: L3Int): Literal = IntLit(i)
private[this] implicit def intToLit(i: Int): Literal = IntLit(L3Int(i))
protected val impure: ValuePrimitive => Boolean = ???
protected val impure: ValuePrimitive => Boolean =
Set(ByteRead, ByteWrite, BlockSet)
protected val unstable: ValuePrimitive => Boolean = ???
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment