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 }]
val newState = s.withExp(name, prim, replacedArgs)
lazy val shrunkBody = shrink(body, newState)
// Dead code elim
if (s.dead(name) && !impure(prim)) {
shrunkBody
} else {
val allLitOpt = replacedArgs.map(_.asLiteral)
val isAllLit = allLitOpt.forall(_.isDefined)
lazy val asLit = allLitOpt.map(_.get)
if (isAllLit && vEvaluator.isDefinedAt((prim, asLit))) {
val newNewState = newState.withASubst(name, vEvaluator((prim, asLit)))
val newBody = shrink(body, newNewState)
newBody
if (false && s.eInvEnv.isDefinedAt((prim, replacedArgs))) {
val preComputedAtom = s.eInvEnv((prim, replacedArgs))
shrink(body, newState.withASubst(name, preComputedAtom))
} 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