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,44 +113,38 @@ 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 {
// Constant folding
case Seq(AtomL(l1), AtomL(l2)) if vEvaluator.isDefinedAt((prim, asLit)) =>
shrink(body, s.withASubst(name, vEvaluator((prim, asLit))))
// Same argument reduction
case Seq(a1, a2) if a1 == a2 && sameArgReduce.isDefinedAt(prim, a1) =>
val reduced = sameArgReduce((prim, a1))
shrink(body, s.withASubst(name, sameArgReduce((prim, reduced))))
// Left Neutral
case Seq(AtomL(l1), a2) if leftNeutral((l1, prim)) =>
shrink(body, s.withASubst(name, a2))
// Left Absorbing
case Seq(AtomL(l1), a2) if leftAbsorbing((l1, prim)) =>
shrink(body, s.withASubst(name, l1))
// Right Neutral
case Seq(a1, AtomL(l2)) if rightNeutral((prim, l2)) =>
shrink(body, s.withASubst(name, a1))
// 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
}
}
}
} 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))))
// Same argument reduction
case Seq(a1, a2) if a1 == a2 && sameArgReduce.isDefinedAt(prim, a1) =>
val reduced = sameArgReduce((prim, a1))
shrink(body, s.withASubst(name, sameArgReduce((prim, reduced))))
// Left Neutral
case Seq(AtomL(l1), a2) if leftNeutral((l1, prim)) =>
shrink(body, s.withASubst(name, a2))
// Left Absorbing
case Seq(AtomL(l1), a2) if leftAbsorbing((l1, prim)) =>
shrink(body, s.withASubst(name, l1))
// Right Neutral
case Seq(a1, AtomL(l2)) if rightNeutral((prim, l2)) =>
shrink(body, s.withASubst(name, a1))
// Right Absorbing
case Seq(a1, AtomL(l2)) if rightAbsorbing((prim, l2)) =>
shrink(body, s.withASubst(name, l2))
case _ => noOp
}
}
}
case LetC(cnts, body) => {
val undeadConts = cnts.filterNot(cnt => s.dead(cnt.name))
val undeadShrunkConts = undeadConts.map { cnt =>
......@@ -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