From 7aec627f6b43f550c34ac947017edc5b7365dfe2 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 8 Sep 2024 22:26:01 +0200 Subject: [PATCH] add optimization if x==0 or x==1 -> if x<2 --- compiler/res/prog8lib/cx16/sprites.p8 | 2 +- .../compiler/astprocessing/VariousCleanups.kt | 18 ++++++++-- docs/source/todo.rst | 3 -- examples/test.p8 | 36 ++++--------------- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/compiler/res/prog8lib/cx16/sprites.p8 b/compiler/res/prog8lib/cx16/sprites.p8 index d89f16b20..b85ae74ac 100644 --- a/compiler/res/prog8lib/cx16/sprites.p8 +++ b/compiler/res/prog8lib/cx16/sprites.p8 @@ -23,7 +23,7 @@ sprites { ubyte databank, uword dataaddr, ubyte width_flag, ubyte height_flag, ubyte colors_flag, ubyte palette_offset) { - hide(spritenum) + pos(spritenum, -64, -64) ; move sprite off-screen initially cx16.VERA_DC_VIDEO |= %01000000 ; enable sprites globally dataaddr >>= 5 dataaddr |= (databank as uword)<<11 diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index abc545a6d..8eca2c019 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -135,9 +135,23 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter, } if(isMultiComparisonRecurse(leftBinExpr1)) { - // replace it! - val valueCopies = values.sortedBy { it.number }.map { it.copy() } val elementType = needle.inferType(program).getOrElse { throw FatalAstException("invalid needle dt") } + if(values.size==2 || values.size==3 && elementType in IntegerDatatypes) { + val numbers = values.map{it.number}.toSet() + if(numbers == setOf(0.0, 1.0)) { + // we can replace x==0 or x==1 with x<2 + val compare = BinaryExpression(needle, "<", NumericLiteral(elementType, 2.0, expr.position), expr.position) + return listOf(IAstModification.ReplaceNode(expr, compare, parent)) + } + if(numbers == setOf(0.0, 1.0, 2.0)) { + // we can replace x==0 or x==1 or x==2 with x<3 + val compare = BinaryExpression(needle, "<", NumericLiteral(elementType, 3.0, expr.position), expr.position) + return listOf(IAstModification.ReplaceNode(expr, compare, parent)) + } + } + if(values.size<4) + return noModifications // replacement only worthwhile for 4 or more values + val valueCopies = values.sortedBy { it.number }.map { it.copy() } val arrayType = ElementToArrayTypes.getValue(elementType) val valuesArray = ArrayLiteral(InferredTypes.InferredType.known(arrayType), valueCopies.toTypedArray(), expr.position) val containment = ContainmentCheck(needle, valuesArray, expr.position) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index b44c9a529..181a4fc9c 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,9 +1,6 @@ TODO ==== -Main branch: possible optimization for: if x < 2/ x<1 -> if x==0 or x==1 likewise for > 253/ >254 - - Improve register load order in subroutine call args assignments: in certain situations, the "wrong" order of evaluation of function call arguments is done which results in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!) diff --git a/examples/test.p8 b/examples/test.p8 index 6660a3ba2..55b0288f4 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,37 +4,13 @@ main { sub start() { - ubyte @shared x - uword @shared w + byte @shared x + word @shared w - x=2 - if x==2 or cx16.r0L==42 - txt.print("yep0\n") + if x==0 or x==1 or x==2 + x++ - if x==1 or x==2 - txt.print("yep1\n") - - x = 9 - if x==1 or x==2 - txt.print("yep2-shouldn't-see-this\n") ; TODO fix this in IR/VM - - if x in 1 to 4 - txt.print("yep3-shouldn't-see-this\n") - if x in 4 to 10 - txt.print("yep4\n") - - - w=2222 - if w==1111 or w==2222 - txt.print("w-yep1\n") - - w = 4999 - if w==1111 or w==2222 - txt.print("w-yep2-shouldn't-see-this\n") ; TODO fix this in IR/VM - - if w in 1111 to 4444 - txt.print("w-yep3-shouldn't-see-this\n") - if w in 4444 to 5555 - txt.print("w-yep4\n") + if w==0 or w==1 or w==2 + x++ } }