mirror of
https://github.com/irmen/prog8.git
synced 2025-02-04 02:30:19 +00:00
added a few more comparison expression optimizations
This commit is contained in:
parent
448d176c24
commit
1436480eab
@ -187,12 +187,37 @@ class ExpressionSimplifier(private val program: Program,
|
||||
}
|
||||
}
|
||||
|
||||
// X <= Y-1 ---> X<Y , X >= Y+1 ---> X>Y
|
||||
if(leftDt in IntegerDatatypes && rightDt in IntegerDatatypes) {
|
||||
val rightExpr = expr.right as? BinaryExpression
|
||||
if(rightExpr!=null && rightExpr.right.constValue(program)?.number==1.0) {
|
||||
if (expr.operator == "<=" && rightExpr.operator == "-") {
|
||||
expr.operator = "<"
|
||||
return listOf(IAstModification.ReplaceNode(rightExpr, rightExpr.left, expr))
|
||||
} else if (expr.operator == ">=" && rightExpr.operator == "+") {
|
||||
expr.operator = ">"
|
||||
return listOf(IAstModification.ReplaceNode(rightExpr, rightExpr.left, expr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(leftDt!=DataType.FLOAT && expr.operator == ">=" && rightVal?.number == 1.0) {
|
||||
// for integers: x >= 1 --> x > 0
|
||||
expr.operator = ">"
|
||||
return listOf(IAstModification.ReplaceNode(expr.right, NumericLiteral.optimalInteger(0, expr.right.position), expr))
|
||||
}
|
||||
|
||||
// for signed integers: X <= -1 => X<0 , X > -1 => X>=0
|
||||
if(leftDt in SignedDatatypes && leftDt!=DataType.FLOAT && rightVal?.number==-1.0) {
|
||||
if(expr.operator=="<=") {
|
||||
expr.operator = "<"
|
||||
return listOf(IAstModification.ReplaceNode(expr.right, NumericLiteral(rightDt, 0.0, expr.right.position), expr))
|
||||
} else if(expr.operator==">") {
|
||||
expr.operator = ">="
|
||||
return listOf(IAstModification.ReplaceNode(expr.right, NumericLiteral(rightDt, 0.0, expr.right.position), expr))
|
||||
}
|
||||
}
|
||||
|
||||
if (leftDt == DataType.UBYTE || leftDt == DataType.UWORD) {
|
||||
if(expr.operator == ">=" && rightVal?.number == 0.0) {
|
||||
// unsigned >= 0 --> true
|
||||
|
@ -3,13 +3,6 @@ TODO
|
||||
|
||||
For next minor release
|
||||
^^^^^^^^^^^^^^^^^^^^^^
|
||||
- add optimizations for integer X <= Y-1 ---> X<Y , X >= Y+1 ---> X>Y
|
||||
- add optimizations for integer:
|
||||
X >= 1 => X > 0 (signed and unsigned)
|
||||
X < 1 => X <= 0 (signed) or X==0 (unsigned)
|
||||
X <= -1 => X >= 0 (signed only)
|
||||
X > -1 => X >= 0 (signed only)
|
||||
|
||||
...
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user