Commit bfbb4735 authored by Sapphie's avatar Sapphie
Browse files

Add removal of block-set for unused blocks

parent 0f48f05e
......@@ -12,7 +12,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
fixedPoint(simplifiedTree, 8) { t => inline(t, maxSize) }
}
private case class Count(applied: Int = 0, asValue: Int = 0)
private case class Count(applied: Int = 0, asValue: Int = 0, writtenTo: Int = 0)
private case class State(
census: Map[Name, Count],
......@@ -27,8 +27,11 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
eInvEnv.map(_.swap)
def dead(s: Name): Boolean =
! census.contains(s)
def onlyWrittenTo(s: Name): Boolean =
census.get(s).filter(c => c.writtenTo == c.asValue).isDefined
def appliedOnce(s: Name): Boolean =
census.get(s).contains(Count(applied = 1, asValue = 0))
census.get(s).filter(c => c.asValue == 0 && c.applied == 1).isDefined
def withASubst(from: Atom, to: Atom): State =
copy(aSubst = aSubst + (from -> aSubst(to)))
......@@ -159,6 +162,8 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
if (isKnownConstant) {
val newBody = shrink(body, newState.withBlockVal(b, idx, v))
LetP(name, prim, replacedArgs, newBody)
} else if (b.asName.filter(s.onlyWrittenTo).isDefined) {
shrink(body, s)
} else {
noOp
}
......@@ -372,8 +377,16 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
atom.asName.foreach(incValUseN(_))
def addToCensus(tree: Tree): Unit = (tree: @unchecked) match {
case LetP(_, _, args, body) =>
args foreach incValUseA; addToCensus(body)
case LetP(name, prim, args, body) =>
args foreach incValUseA;
lazy val block = args(0)
if (prim == blockSet && block.asName.isDefined) {
val blockName = block.asName.get
val writeCount = census(blockName)
census(blockName) = writeCount.copy(writtenTo = writeCount.writtenTo + 1)
// rhs.remove(blockName).foreach(addToCensus)
}
addToCensus(body)
case LetC(cnts, body) =>
rhs ++= (cnts map { c => (c.name, c.body) }); addToCensus(body)
case LetF(funs, body) =>
......
package l3
import java.io.PrintWriter
import java.io.{PrintWriter, File}
import java.nio.file.{ Files, Paths }
import l3.SymbolicCL3TreeModule.Tree
......@@ -46,19 +46,29 @@ object Main {
}
}
private lazy val outFileWriter =
new PrintWriter(new File("output.txt"))
private lazy val outPrintWriter =
new PrintWriter(System.out, true)
private def treeChecker[T <: CPSTreeModule](implicit c: CPSTreeChecker[T]) =
passThrough(c)
private def treePrinter[T](msg: String)(implicit f: Formatter[T]): T => T =
private def treeFilePrinter[T](msg: String, o: PrintWriter)(implicit f: Formatter[T]): T => T =
passThrough { tree =>
outPrintWriter.println(msg)
f.toDoc(tree).writeTo(80, outPrintWriter)
outPrintWriter.println()
o.println(msg)
f.toDoc(tree).writeTo(80, o)
o.println()
}
private def treePrinter[T](msg: String)(implicit f: Formatter[T]): T => T =
treeFilePrinter(msg, outPrintWriter)
private def treeDumper[T](msg: String)(implicit f: Formatter[T]): T => T =
treeFilePrinter(msg, outFileWriter)
private def seqPrinter[T](msg: String): Seq[T] => Seq[T] =
passThrough { program =>
outPrintWriter.println(msg)
......
(let ((p (@block-alloc-20 1))) (let ((lol (@block-set! p 0 0))) (halt 0)))
\ No newline at end of file
(let ((x 3))
(let ((y (@- x 3)) (z (@- x 3)))
(let ((w (@+ y z)))
(halt w))))
\ No newline at end of file
(halt (@- 1 1))
\ No newline at end of file
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