add optimization if x==0 or x==1 -> if x<2

This commit is contained in:
Irmen de Jong 2024-09-08 22:26:01 +02:00
parent 59a2fec176
commit 7aec627f6b
4 changed files with 23 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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