From ff54d6abd700ea03b947cb65061eb5e1d4bbf754 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 22 Aug 2020 17:44:32 +0200 Subject: [PATCH] reorder const for all associative operators --- .../prog8/optimizer/ConstantFoldingOptimizer.kt | 5 +++-- examples/test.p8 | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt index 2ec8005a5..b088c64c3 100644 --- a/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -415,8 +415,9 @@ internal class ConstantFoldingOptimizer(private val program: Program) : AstWalke // todo: this implements only a small set of possible reorderings at this time if(expr.operator==subExpr.operator) { // both operators are the same. - // If + or *, we can simply shuffle the const operands around to optimize. - if(expr.operator=="+" || expr.operator=="*") { + + // If associative, we can simply shuffle the const operands around to optimize. + if(expr.operator in associativeOperators) { return if(leftIsConst) { if(subleftIsConst) ShuffleOperands(expr, null, subExpr, subExpr.right, null, null, expr.left) diff --git a/examples/test.p8 b/examples/test.p8 index 79f914e21..67d3ca972 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,6 +7,20 @@ main { sub start() { + ubyte ub + uword uw + word w + byte b + float f + + + ub = 4 | ub | 2 + ub = ub | 2 | 7 + ub = 4 | 2 | ub + + ub = 4 + ub + 2 + ub = ub + 2 + 7 + ub = 4 + 2 + ub } }