From 6af3209d4db81252729dbd891448b7351c5f8d34 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 17 Nov 2021 00:57:00 +0100 Subject: [PATCH] add more const foldings --- .../optimizer/ConstantFoldingOptimizer.kt | 27 ++++++++++ .../prog8/optimizer/ExpressionSimplifier.kt | 6 --- docs/source/todo.rst | 5 +- examples/test.p8 | 49 +++++++++++++------ 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt index b83aabc9d..16c06bb25 100644 --- a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -101,6 +101,33 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() { val rightconst = expr.right.constValue(program) val modifications = mutableListOf() + if(expr.operator=="==" && rightconst!=null) { + val leftExpr = expr.left as? BinaryExpression + if(leftExpr!=null) { + val leftRightConst = leftExpr.right.constValue(program) + if(leftRightConst!=null) { + when (leftExpr.operator) { + "+" -> { + // X + С1 == C2 --> X == C2 - C1 + val newRightConst = NumericLiteralValue(rightconst.type, rightconst.number - leftRightConst.number, rightconst.position) + return listOf( + IAstModification.ReplaceNode(leftExpr, leftExpr.left, expr), + IAstModification.ReplaceNode(expr.right, newRightConst, expr) + ) + } + "-" -> { + // X - С1 == C2 --> X == C2 + C1 + val newRightConst = NumericLiteralValue(rightconst.type, rightconst.number + leftRightConst.number, rightconst.position) + return listOf( + IAstModification.ReplaceNode(leftExpr, leftExpr.left, expr), + IAstModification.ReplaceNode(expr.right, newRightConst, expr) + ) + } + } + } + } + } + if(expr.operator == "**" && leftconst!=null) { // optimize various simple cases of ** : // optimize away 1 ** x into just 1 and 0 ** x into just 0 diff --git a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt index 9577a4b7d..b924dc9b6 100644 --- a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt @@ -19,12 +19,6 @@ import kotlin.math.pow Investigate what optimizations binaryen has, also see https://egorbo.com/peephole-optimizations.html -Add some const foldings (that may already be done as well): -X + С1 == C2 => X == C2 - C1 -((X + C1) + C2) => (X + (C1 + C2)) -((X + C1) + (Y + C2)) => ((X + Y) + (C1 + C2)) - - */ diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 5b1485073..d48d34f67 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,10 @@ TODO For next compiler release (7.4) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -... +BUG: Fix C-64sound issue in petaxian (regression since 7.3, sound on c64 build works fine on older versions) +BUG: ubyte z1 + ubyte z2 + z2=z1+z2+5 CRASHES COMPILER Blocked by an official Commander-x16 v39 release diff --git a/examples/test.p8 b/examples/test.p8 index 3590901c9..8ccdd4c83 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -13,22 +13,43 @@ main { ; TODO: bitwise operations with a negative constant number -> replace the number by its positive 2 complement - if aw<1 - yy++ - if aw<=0 - yy++ +; (X + C1) + (Y + C2) => (X + Y) + (C1 + C2) +; (X + C1) - (Y + C2) => (X - Y) + (C1 - C2) +; ---> together: (X + C1) (Y + C2) => (X Y) + (C1 C2) - if yy<1 - yy++ - if yy<=0 - yy++ - if bb<1 - yy++ - if bb<=0 - yy++ - txt.print_ub(yy) - txt.print_uw(aw) +; (X - C1) + (Y - C2) => (X + Y) - (C1 + C2) +; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2) + xx=6 + yy=8 + + + yy = (xx+5)+(yy+10) + ; yy = (xx+yy)+(5+10) ; TODO crashes compiler + txt.print_ub(yy) ; 29 + txt.nl() + + xx=100 + yy=8 + ;yy = (xx+5)-(yy+10) + yy = (xx-yy)+(5-10) as ubyte + txt.print_ub(yy) ; 87 + txt.nl() + + xx=50 + yy=40 + yy = (xx-5)+(yy-10) + txt.print_ub(yy) ; 75 + txt.nl() + + xx=50 + yy=20 + yy = (xx-5)-(yy-10) + txt.print_ub(yy) ; 35 + txt.nl() + + repeat { + } } }