From b73da4ed02f08610e98d9b343286d5c5710629a2 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 31 Mar 2020 23:52:46 +0200 Subject: [PATCH] some more obvious optimizations for X+X and X-X --- compiler/res/prog8lib/prog8lib.asm | 2 -- compiler/src/prog8/compiler/Main.kt | 3 +++ .../prog8/optimizer/ExpressionSimplifier.kt | 13 ++++++++++ examples/test.p8 | 24 ------------------- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/compiler/res/prog8lib/prog8lib.asm b/compiler/res/prog8lib/prog8lib.asm index 06c194880..f69e10787 100644 --- a/compiler/res/prog8lib/prog8lib.asm +++ b/compiler/res/prog8lib/prog8lib.asm @@ -1772,7 +1772,6 @@ _loop_hi ldy _index_first ror2_mem_ub .proc ; -- in-place 8-bit ror of byte at memory location on stack - ; TODO use self modifying code here inx lda c64.ESTACK_LO,x sta c64.SCRATCH_ZPWORD1 @@ -1789,7 +1788,6 @@ ror2_mem_ub .proc rol2_mem_ub .proc ; -- in-place 8-bit rol of byte at memory location on stack - ; TODO use self modifying code here inx lda c64.ESTACK_LO,x sta c64.SCRATCH_ZPWORD1 diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index 95a47dc58..5442c6c66 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -165,6 +165,9 @@ private fun optimizeAst(programAst: Program, errors: ErrorReporter) { if (optsDone1 + optsDone2 == 0) break } + // because simplified statements and expressions could give rise to more constants that can be folded away: + programAst.constantFold(errors) + errors.handle() } private fun postprocessAst(programAst: Program, errors: ErrorReporter, compilerOptions: CompilationOptions) { diff --git a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt index ea491f206..0238b7e26 100644 --- a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt @@ -258,6 +258,14 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() } private fun optimizeAdd(expr: BinaryExpression, leftVal: NumericLiteralValue?, rightVal: NumericLiteralValue?): Expression? { + if(expr.left.isSameAs(expr.right)) { + // optimize X+X into X *2 + expr.operator = "*" + expr.right = NumericLiteralValue.optimalInteger(2, expr.right.position) + expr.right.linkParents(expr) + return expr + } + if (leftVal == null && rightVal == null) return null @@ -278,6 +286,11 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() } private fun optimizeSub(expr: BinaryExpression, leftVal: NumericLiteralValue?, rightVal: NumericLiteralValue?): Expression? { + if(expr.left.isSameAs(expr.right)) { + // optimize X-X into 0 + return NumericLiteralValue.optimalInteger(0, expr.position) + } + if (leftVal == null && rightVal == null) return null diff --git a/examples/test.p8 b/examples/test.p8 index 0dc0be01b..742945455 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,33 +1,9 @@ %import c64lib %import c64utils -%zeropage dontuse main { sub start() { - ubyte ub - byte bb - word ww - uword uw - uword auw - word aww - byte ab - ubyte aub - - ; TODO optimize all of these: - ab = bb+bb ; TODO bb * 2? (bb<<1) - ab = bb+bb+bb - aww = ww+ww - aww = ww+ww+ww - - aub = ub+ub - aub = ub+ub+ub - auw = uw+uw - auw = uw+uw+uw - - - A = A+A - Y=Y+Y+Y } }