fix invalid const optimization with multiplication

This commit is contained in:
Irmen de Jong 2024-01-06 13:59:13 +01:00
parent 8e6b91cb9e
commit 806654fc44

View File

@ -233,7 +233,10 @@ class ConstantFoldingOptimizer(private val program: Program, private val errors:
} }
else if(leftBinExpr.operator=="*" && rightBinExpr.operator=="*"){ else if(leftBinExpr.operator=="*" && rightBinExpr.operator=="*"){
if (c1 != null && c2 != null && c1==c2) { if (c1 != null && c2 != null && c1==c2) {
//(X * C) <plusmin> (Y * C) => (X <plusmin> Y) * C //(X * C) <plusmin> (Y * C) => (X <plusmin> Y) * C (only if types of X and Y are the same!)
val xDt = leftBinExpr.left.inferType(program)
val yDt = rightBinExpr.left.inferType(program)
if(xDt==yDt) {
val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position) val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position)
val newExpr = BinaryExpression(xwithy, "*", c1, expr.position) val newExpr = BinaryExpression(xwithy, "*", c1, expr.position)
modifications += IAstModification.ReplaceNode(expr, newExpr, parent) modifications += IAstModification.ReplaceNode(expr, newExpr, parent)
@ -241,6 +244,7 @@ class ConstantFoldingOptimizer(private val program: Program, private val errors:
} }
} }
} }
}
if(rightconst!=null && (expr.operator=="<<" || expr.operator==">>")) { if(rightconst!=null && (expr.operator=="<<" || expr.operator==">>")) {
val dt = expr.left.inferType(program) val dt = expr.left.inferType(program)