Commit 79a9e1d0 authored by Luca Bataillard's avatar Luca Bataillard
Browse files

add identity inlining (one test fails)

parent e86031e1
......@@ -9,7 +9,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
protected def rewrite(tree: Tree): Tree = {
val simplifiedTree = fixedPoint(tree)(shrink)
val maxSize = size(simplifiedTree) * 3 / 2
fixedPoint(simplifiedTree, 8) { t => inline(t, maxSize) }
fixedPoint(simplifiedTree, 2) { t => inline(t, maxSize) }
}
private case class Count(applied: Int = 0, asValue: Int = 0)
......@@ -113,13 +113,14 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
shrunkBody
} else {
val allLitOpt = replacedArgs.map(_.asLiteral)
val isAllLit = allLitOpt.forall(_.isDefined)
lazy val asLit = allLitOpt.map(_.get)
if (!unstable(prim) && !impure(prim) && s.eInvEnv.isDefinedAt((prim, replacedArgs))) {
if (!unstable(prim) && !impure(prim) && s.eInvEnv.contains((prim, replacedArgs))) {
val preComputedAtom = s.eInvEnv((prim, replacedArgs))
shrink(body, newState.withASubst(name, preComputedAtom))
} else {
replacedArgs match {
} else if (prim == identity) {
shrink(body, s.withASubst(name, replacedArgs(0)))
} else replacedArgs match {
// Constant folding
case Seq(AtomL(l1), AtomL(l2)) if vEvaluator.isDefinedAt((prim, asLit)) =>
shrink(body, s.withASubst(name, vEvaluator((prim, asLit))))
......@@ -139,14 +140,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
// Right Absorbing
case Seq(a1, AtomL(l2)) if rightAbsorbing((prim, l2)) =>
shrink(body, s.withASubst(name, l2))
case _ => {
if (false && prim == identity) {
shrink(body, s.withASubst(name, replacedArgs(0)))
} else {
noOp
}
}
}
case _ => noOp
}
}
}
......@@ -274,9 +268,11 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
funName match {
case AtomN(n) if s.fEnv.contains(n) => {
val oldFun = s.fEnv(n)
val notUsedAsValue = s.census.get(oldFun.name).fold(false)(count => count.asValue == 0)
val nonRecursive = notUsedAsValue && !census(oldFun.body).contains(oldFun.name)
/* TODO ask about stopping inlining of recursive functions */
if (!census(oldFun.body).contains(oldFun.name) && sameLen(oldFun.args, args)) {
if (nonRecursive && sameLen(oldFun.args, args)) {
val fun = copyF(oldFun, s.aSubst, s.cSubst)
val newState = s.withASubst(fun.args, args).withCSubst(fun.retC, retC)
inlineT(fun.body)(newState)
......
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