some more obvious optimizations for X+X and X-X

This commit is contained in:
Irmen de Jong 2020-03-31 23:52:46 +02:00
parent 267adb4612
commit b73da4ed02
4 changed files with 16 additions and 26 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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
}
}