Commit 527efc2b authored by Sapphie's avatar Sapphie
Browse files

Add optimisation details

parent 33e844bf
...@@ -122,6 +122,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }] ...@@ -122,6 +122,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
shrunkBody shrunkBody
} else { } else {
val allLitOpt = replacedArgs.map(_.asLiteral) val allLitOpt = replacedArgs.map(_.asLiteral)
val areAllLit: Boolean = allLitOpt.map(_.isDefined).foldLeft(true)((x, y) => x && y)
lazy val asLit = allLitOpt.map(_.get) lazy val asLit = allLitOpt.map(_.get)
if (!unstable(prim) && !impure(prim) && s.eInvEnv.contains((prim, replacedArgs))) { if (!unstable(prim) && !impure(prim) && s.eInvEnv.contains((prim, replacedArgs))) {
...@@ -131,7 +132,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }] ...@@ -131,7 +132,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
shrink(body, s.withASubst(name, replacedArgs(0))) shrink(body, s.withASubst(name, replacedArgs(0)))
} else replacedArgs match { } else replacedArgs match {
// Constant folding // Constant folding
case Seq(AtomL(l1), AtomL(l2)) if vEvaluator.isDefinedAt((prim, asLit)) => case _ if areAllLit && vEvaluator.isDefinedAt((prim, asLit)) =>
shrink(body, s.withASubst(name, vEvaluator((prim, asLit)))) shrink(body, s.withASubst(name, vEvaluator((prim, asLit))))
// Same argument reduction // Same argument reduction
case Seq(a1, a2) if a1 == a2 && sameArgReduce.isDefinedAt(prim, a1) => case Seq(a1, a2) if a1 == a2 && sameArgReduce.isDefinedAt(prim, a1) =>
...@@ -472,7 +473,7 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule) ...@@ -472,7 +473,7 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
protected val leftNeutral: Set[(Literal, ValuePrimitive)] = Set( protected val leftNeutral: Set[(Literal, ValuePrimitive)] = Set(
(IntLit(L3Int(0)), IntAdd), (IntLit(L3Int(0)), IntAdd),
(IntLit(L3Int(1)), IntMul), (IntLit(L3Int(1)), IntMul),
(IntLit(L3Int((-1 << 1) >> 1)), IntBitwiseAnd), (IntLit(L3Int(~0)), IntBitwiseAnd),
(IntLit(L3Int(0)), IntBitwiseOr), (IntLit(L3Int(0)), IntBitwiseOr),
(IntLit(L3Int(0)), IntBitwiseXOr) (IntLit(L3Int(0)), IntBitwiseXOr)
) )
...@@ -483,7 +484,7 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule) ...@@ -483,7 +484,7 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
(IntDiv, IntLit(L3Int(1))), (IntDiv, IntLit(L3Int(1))),
(IntShiftLeft, IntLit(L3Int(0))), (IntShiftLeft, IntLit(L3Int(0))),
(IntShiftRight, IntLit(L3Int(0))), (IntShiftRight, IntLit(L3Int(0))),
(IntBitwiseAnd, IntLit(L3Int((-1 << 1) >> 1))), (IntBitwiseAnd, IntLit(L3Int(~0))),
(IntBitwiseOr, IntLit(L3Int(0))), (IntBitwiseOr, IntLit(L3Int(0))),
(IntBitwiseXOr, IntLit(L3Int(0))), (IntBitwiseXOr, IntLit(L3Int(0))),
) )
...@@ -492,15 +493,16 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule) ...@@ -492,15 +493,16 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
(IntLit(L3Int(0)), IntMul), (IntLit(L3Int(0)), IntMul),
(IntLit(L3Int(0)), IntMod), (IntLit(L3Int(0)), IntMod),
(IntLit(L3Int(0)), IntBitwiseAnd), (IntLit(L3Int(0)), IntBitwiseAnd),
(IntLit(L3Int((-1 << 1) >> 1)), IntBitwiseOr), (IntLit(L3Int(~0)), IntBitwiseOr),
(IntLit(L3Int(0)), IntShiftLeft), (IntLit(L3Int(0)), IntShiftLeft),
(IntLit(L3Int(0)), IntShiftRight) (IntLit(L3Int(0)), IntShiftRight),
(IntLit(L3Int(0)), IntDiv)
) )
protected val rightAbsorbing: Set[(ValuePrimitive, Literal)] = Set( protected val rightAbsorbing: Set[(ValuePrimitive, Literal)] = Set(
(IntMul, IntLit(L3Int(0))), (IntMul, IntLit(L3Int(0))),
(IntBitwiseAnd, IntLit(L3Int(0))), (IntBitwiseAnd, IntLit(L3Int(0))),
(IntBitwiseOr, IntLit(L3Int((-1 << 1) >> 1))) (IntBitwiseOr, IntLit(L3Int(~0)))
) )
protected val sameArgReduce: PartialFunction[(ValuePrimitive, Atom), Atom] = { protected val sameArgReduce: PartialFunction[(ValuePrimitive, Atom), Atom] = {
...@@ -529,6 +531,8 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule) ...@@ -529,6 +531,8 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
case IntShiftLeft => IntLit(x << y) case IntShiftLeft => IntLit(x << y)
case IntShiftRight => IntLit(x >> y) case IntShiftRight => IntLit(x >> y)
} }
case (IntToChar, Seq(IntLit(i))) => CharLit(i.toInt.toChar)
case (CharToInt, Seq(CharLit(c))) => IntLit(L3Int(c.toInt))
} }
protected val cEvaluator: PartialFunction[(TestPrimitive, Seq[Literal]), protected val cEvaluator: PartialFunction[(TestPrimitive, Seq[Literal]),
...@@ -536,6 +540,15 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule) ...@@ -536,6 +540,15 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
case (IntLe, Seq(IntLit(x), IntLit(y))) => x <= y case (IntLe, Seq(IntLit(x), IntLit(y))) => x <= y
case (IntLt, Seq(IntLit(x), IntLit(y))) => x < y case (IntLt, Seq(IntLit(x), IntLit(y))) => x < y
case (Eq, Seq(l1, l2)) => l1 == l2 case (Eq, Seq(l1, l2)) => l1 == l2
case (IntP, Seq(IntLit(_))) => true
case (CharP, Seq(CharLit(_))) => true
case (BoolP, Seq(BooleanLit(_))) => true
case (UnitP, Seq(UnitLit)) => true
case (BlockP, Seq(_)) => false
case (IntP, Seq(_)) => false
case (CharP, Seq(_)) => false
case (BoolP, Seq(_)) => false
case (UnitP, Seq(_)) => false
} }
} }
......
...@@ -11,6 +11,7 @@ import CPSTreeChecker._ // Implicits required for CPS tree checking ...@@ -11,6 +11,7 @@ import CPSTreeChecker._ // Implicits required for CPS tree checking
object Main { object Main {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
val stats = new Statistics()
val backEnd: Tree => TerminalPhaseResult = ( val backEnd: Tree => TerminalPhaseResult = (
// CL3Interpreter // CL3Interpreter
CL3ToCPSTranslator CL3ToCPSTranslator
...@@ -21,17 +22,18 @@ object Main { ...@@ -21,17 +22,18 @@ object Main {
andThen CPSValueRepresenter andThen CPSValueRepresenter
andThen CPSHoister andThen CPSHoister
// andThen treePrinter("---------- After value representation") // andThen treePrinter("---------- After value representation")
andThen CPSOptimizerLow // andThen CPSOptimizerLow
// andThen treePrinter("---------- After low optimization") // andThen treePrinter("---------- After low optimization")
// andThen CPSInterpreterLow andThen treeChecker
andThen CPSConstantNamer andThen (new CPSInterpreterLow(stats.log _))
andThen CPSRegisterAllocator // andThen CPSConstantNamer
andThen CPSToASMTranslator // andThen CPSRegisterAllocator
// andThen CPSToASMTranslator
// andThen seqPrinter("---------- After translation to assembly") // andThen seqPrinter("---------- After translation to assembly")
andThen ASMLabelResolver // andThen ASMLabelResolver
// andThen ASMInterpreter // andThen ASMInterpreter
andThen ASMFileWriter(Option(System.getProperty("l3.out-asm-file")) // andThen ASMFileWriter(Option(System.getProperty("l3.out-asm-file"))
.getOrElse("out.l3a")) // .getOrElse("out.l3a"))
) )
val basePath = Paths.get(".").toAbsolutePath val basePath = Paths.get(".").toAbsolutePath
...@@ -42,6 +44,7 @@ object Main { ...@@ -42,6 +44,7 @@ object Main {
.flatMap(backEnd) match { .flatMap(backEnd) match {
case Right((retCode, maybeMsg)) => case Right((retCode, maybeMsg)) =>
maybeMsg foreach println maybeMsg foreach println
println(stats)
sys.exit(retCode) sys.exit(retCode)
case Left(errMsg) => case Left(errMsg) =>
println(s"Error: $errMsg") println(s"Error: $errMsg")
......
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