From ead8aa780024fe12ecc51e7f154583695feef629 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 29 Jun 2024 17:22:57 +0200 Subject: [PATCH] asm optimization: bxx+jmp -> opposite bxx --- .../src/prog8/codegen/cpu6502/AsmOptimizer.kt | 83 +++++++++++++++---- docs/source/todo.rst | 11 --- 2 files changed, 66 insertions(+), 28 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt index 487ae797a..c820e49ab 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt @@ -446,23 +446,6 @@ private fun optimizeStoreLoadSame( if (firstLoc == secondLoc) mods.add(Modification(lines[2].index, true, null)) } - - // phy + ldy + pla -> tya + ldy - // phx + ldx + pla -> txa + ldx - // pha + lda + pla -> nop - if(first=="phy" && second.startsWith("ldy ") && third=="pla") { - mods.add(Modification(lines[3].index, true, null)) - mods.add(Modification(lines[1].index, false, " tya")) - } - else if(first=="phx" && second.startsWith("ldx ") && third=="pla") { - mods.add(Modification(lines[3].index, true, null)) - mods.add(Modification(lines[1].index, false, " txa")) - } - else if(first=="pha" && second.startsWith("lda ") && third=="pla") { - mods.add(Modification(lines[1].index, true, null)) - mods.add(Modification(lines[2].index, true, null)) - mods.add(Modification(lines[3].index, true, null)) - } } return mods } @@ -525,6 +508,7 @@ private fun optimizeJsrRtsAndOtherCombinations(linesByFour: Sequence bne Addr + if((" jmp" in second || "\tjmp " in second) && haslabel(third)) { + if((" beq " in first || "\tbeq " in first) && sameLabel(first, second, third)) { + val branch = second.replace("jmp", "bne") + mods.add(Modification(lines[0].index, true, null)) + mods.add(Modification(lines[1].index, false, branch)) + } + else if((" bne " in first || "\tbne " in first) && sameLabel(first, second, third)) { + val branch = second.replace("jmp", "beq") + mods.add(Modification(lines[0].index, true, null)) + mods.add(Modification(lines[1].index, false, branch)) + } + else if((" bcc " in first || "\tbcc " in first) && sameLabel(first, second, third)){ + val branch = second.replace("jmp", "bcs") + mods.add(Modification(lines[0].index, true, null)) + mods.add(Modification(lines[1].index, false, branch)) + } + else if((" bcs " in first || "\tbcs " in first) && sameLabel(first, second, third)) { + val branch = second.replace("jmp", "bcc") + mods.add(Modification(lines[0].index, true, null)) + mods.add(Modification(lines[1].index, false, branch)) + } + else if((" bpl " in first || "\tbpl " in first) && sameLabel(first, second, third)) { + val branch = second.replace("jmp", "bmi") + mods.add(Modification(lines[0].index, true, null)) + mods.add(Modification(lines[1].index, false, branch)) + } + else if((" bmi " in first || "\tbmi " in first) && sameLabel(first, second, third)) { + val branch = second.replace("jmp", "bpl") + mods.add(Modification(lines[0].index, true, null)) + mods.add(Modification(lines[1].index, false, branch)) + } + } } return mods } @@ -631,7 +657,30 @@ private fun optimizeUselessPushPopStack(linesByFour: Sequence tya + ldy + // phx + ldx + pla -> txa + ldx + // pha + lda + pla -> nop + if(first=="phy" && second.startsWith("ldy ") && third=="pla") { + mods.add(Modification(lines[3].index, true, null)) + mods.add(Modification(lines[1].index, false, " tya")) + } + else if(first=="phx" && second.startsWith("ldx ") && third=="pla") { + mods.add(Modification(lines[3].index, true, null)) + mods.add(Modification(lines[1].index, false, " txa")) + } + else if(first=="pha" && second.startsWith("lda ") && third=="pla") { + mods.add(Modification(lines[1].index, true, null)) + mods.add(Modification(lines[2].index, true, null)) + mods.add(Modification(lines[3].index, true, null)) + } } + + return mods } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 69f5e5e25..e8a3abc6f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -6,17 +6,6 @@ causes compiler error for virtual: just calling txt.cls() gives compile error un https://github.com/irmen/prog8/issues/136 (string.find register order issue) -if-optimization: - if row == NUMQUEENS { - print_solution() - return - } - compiles into this, where the bne+jmp could be a single beq instead: - cmp #8 - bne label_asm_21_afterif - jmp p8b_main.p8s_print_solution -label_asm_21_afterif: - optimize signed byte/word division by powers of 2 (and shift right?), it's now using divmod routine. (also % ?) see inplacemodificationByteVariableWithLiteralval() and inplacemodificationSomeWordWithLiteralval()