Commit 95e9f764 authored by Sapphie's avatar Sapphie
Browse files

Implement same argument reduction, as well as CSE (currently disabled)

parent aa62f3c2
...@@ -107,18 +107,32 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }] ...@@ -107,18 +107,32 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
val newState = s.withExp(name, prim, replacedArgs) val newState = s.withExp(name, prim, replacedArgs)
lazy val shrunkBody = shrink(body, newState) lazy val shrunkBody = shrink(body, newState)
// Dead code elim
if (s.dead(name) && !impure(prim)) { if (s.dead(name) && !impure(prim)) {
shrunkBody shrunkBody
} else { } else {
val allLitOpt = replacedArgs.map(_.asLiteral) val allLitOpt = replacedArgs.map(_.asLiteral)
val isAllLit = allLitOpt.forall(_.isDefined) val isAllLit = allLitOpt.forall(_.isDefined)
lazy val asLit = allLitOpt.map(_.get) lazy val asLit = allLitOpt.map(_.get)
if (isAllLit && vEvaluator.isDefinedAt((prim, asLit))) { if (false && s.eInvEnv.isDefinedAt((prim, replacedArgs))) {
val newNewState = newState.withASubst(name, vEvaluator((prim, asLit))) val preComputedAtom = s.eInvEnv((prim, replacedArgs))
val newBody = shrink(body, newNewState) shrink(body, newState.withASubst(name, preComputedAtom))
newBody
} else { } else {
LetP(name, prim, replacedArgs, shrunkBody) // Constant folding
if (isAllLit && vEvaluator.isDefinedAt((prim, asLit))) {
val newNewState = newState.withASubst(name, vEvaluator((prim, asLit)))
val newBody = shrink(body, newNewState)
newBody
} else {
lazy val x = replacedArgs(0)
lazy val y = replacedArgs(1)
if (replacedArgs.length == 2 && x == y && sameArgReduce.isDefinedAt((prim, x))) {
val newNewState = newState.withASubst(name, sameArgReduce((prim, x)))
shrink(body, newNewState)
} else {
LetP(name, prim, replacedArgs, shrunkBody)
}
}
} }
} }
} }
......
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