From 31491c62c5785feab57cf206c44a5ca4c805d61e Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 20 Nov 2021 22:33:13 +0100 Subject: [PATCH] add some more const folding patterns --- .../optimizer/ConstantFoldingOptimizer.kt | 34 ++++++++++++++++- examples/test.p8 | 38 ++++++++----------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt index 16c06bb25..ca24b08eb 100644 --- a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -79,7 +79,7 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() { return noModifications } - /** + /* * Try to constfold a binary expression. * Compile-time constant sub expressions will be evaluated on the spot. * For instance, "9 * (4 + 2)" will be optimized into the integer literal 54. @@ -186,13 +186,43 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() { } } + val evaluator = ConstExprEvaluator() + // const fold when both operands are a const if(leftconst != null && rightconst != null) { - val evaluator = ConstExprEvaluator() val result = evaluator.evaluate(leftconst, expr.operator, rightconst) modifications += IAstModification.ReplaceNode(expr, result, parent) } + + val leftBinExpr = expr.left as? BinaryExpression + val rightBinExpr = expr.right as? BinaryExpression + if(expr.operator=="+" || expr.operator=="-") { + if(leftBinExpr!=null && rightBinExpr!=null) { + val c1 = leftBinExpr.right.constValue(program) + val c2 = rightBinExpr.right.constValue(program) + if(leftBinExpr.operator=="+" && rightBinExpr.operator=="+") { + if (c1 != null && c2 != null) { + // (X + C1) (Y + C2) => (X Y) + (C1 C2) + val c3 = evaluator.evaluate(c1, expr.operator, c2) + val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position) + val newExpr = BinaryExpression(xwithy, "+", c3, expr.position) + modifications += IAstModification.ReplaceNode(expr, newExpr, parent) + } + } + else if(leftBinExpr.operator=="*" && rightBinExpr.operator=="*"){ + if (c1 != null && c2 != null && c1==c2) { + //(X * C) (Y * C) => (X Y) * C + val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position) + val newExpr = BinaryExpression(xwithy, "*", c1, expr.position) + modifications += IAstModification.ReplaceNode(expr, newExpr, parent) + } + } + } + } + + + return modifications } diff --git a/examples/test.p8 b/examples/test.p8 index dd764f0d2..8730b07b9 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,19 +6,11 @@ main { ubyte xx = 1 ubyte yy = 2 + byte b1 + byte b2 ;; TODO add these constant folders: -; -;; (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) - -;; (X * C) + (Y * C) => (X + Y) * C -;; (X * C) - (Y * C) => (X - Y) * C -;; ---> together: (X * C) (Y * C) => (X Y) * C - -; -;; (X - C1) + (Y - C2) => (X + Y) - (C1 + C2) +;; (X - C1) (Y - C2) => (X Y) - (C1 C2) ;; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2) ; @@ -43,22 +35,22 @@ main { txt.print_ub(yy) ; 40 txt.nl() - xx=100 - yy=8 - yy = (xx+5)-(yy+10) - txt.print_ub(yy) ; 87 + b1=100 + b2=8 + b2 = (b1+5)-(b2+10) + txt.print_b(b2) ; 87 txt.nl() - xx=50 - yy=40 - yy = (xx-5)+(yy-10) - txt.print_ub(yy) ; 75 + b1=50 + b2=40 + b2 = (b1-5)+(b2-10) + txt.print_b(b2) ; 75 txt.nl() - xx=50 - yy=20 - yy = (xx-5)-(yy-10) - txt.print_ub(yy) ; 35 + b1=50 + b2=20 + b2 = (b1-5)-(b2-10) + txt.print_b(b2) ; 35 txt.nl() repeat {