reorder const for all associative operators

This commit is contained in:
Irmen de Jong 2020-08-22 17:44:32 +02:00
parent f40bcc219f
commit ff54d6abd7
2 changed files with 17 additions and 2 deletions

View File

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

View File

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