mirror of
https://github.com/irmen/prog8.git
synced 2024-12-25 08:29:25 +00:00
add some more comparison expression optimizations to compare against 0 if possible
This commit is contained in:
parent
f48d6ca9f8
commit
5d362047e2
@ -19,13 +19,7 @@ import kotlin.math.pow
|
|||||||
|
|
||||||
Investigate what optimizations binaryen has, also see https://egorbo.com/peephole-optimizations.html
|
Investigate what optimizations binaryen has, also see https://egorbo.com/peephole-optimizations.html
|
||||||
|
|
||||||
some of these may already be present:
|
Add some const foldings (that may already be done as well):
|
||||||
|
|
||||||
X ^ -1 => ~x
|
|
||||||
X >= 1 => X > 0
|
|
||||||
X < 1 => X <= 0
|
|
||||||
|
|
||||||
and some const foldings (that may already be done as well):
|
|
||||||
X + С1 == C2 => X == C2 - C1
|
X + С1 == C2 => X == C2 - C1
|
||||||
((X + C1) + C2) => (X + (C1 + C2))
|
((X + C1) + C2) => (X + (C1 + C2))
|
||||||
((X + C1) + (Y + C2)) => ((X + Y) + (C1 + C2))
|
((X + C1) + (Y + C2)) => ((X + Y) + (C1 + C2))
|
||||||
@ -191,6 +185,12 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(leftDt!=DataType.FLOAT && expr.operator == ">=" && rightVal?.number == 1.0) {
|
||||||
|
// for integers: x >= 1 --> x > 0
|
||||||
|
expr.operator = ">"
|
||||||
|
return listOf(IAstModification.ReplaceNode(expr.right, NumericLiteralValue.optimalInteger(0, expr.right.position), expr))
|
||||||
|
}
|
||||||
|
|
||||||
if(expr.operator == ">=" && rightVal?.number == 0.0) {
|
if(expr.operator == ">=" && rightVal?.number == 0.0) {
|
||||||
if (leftDt == DataType.UBYTE || leftDt == DataType.UWORD) {
|
if (leftDt == DataType.UBYTE || leftDt == DataType.UWORD) {
|
||||||
// unsigned >= 0 --> true
|
// unsigned >= 0 --> true
|
||||||
@ -198,6 +198,12 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(leftDt!=DataType.FLOAT && expr.operator == "<" && rightVal?.number == 1.0) {
|
||||||
|
// for integers: x < 1 --> x <= 0
|
||||||
|
expr.operator = "<="
|
||||||
|
return listOf(IAstModification.ReplaceNode(expr.right, NumericLiteralValue.optimalInteger(0, expr.right.position), expr))
|
||||||
|
}
|
||||||
|
|
||||||
if(expr.operator == "<" && rightVal?.number == 0.0) {
|
if(expr.operator == "<" && rightVal?.number == 0.0) {
|
||||||
if (leftDt == DataType.UBYTE || leftDt == DataType.UWORD) {
|
if (leftDt == DataType.UBYTE || leftDt == DataType.UWORD) {
|
||||||
// unsigned < 0 --> false
|
// unsigned < 0 --> false
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
%import textio
|
%import textio
|
||||||
|
%import floats
|
||||||
|
|
||||||
main {
|
main {
|
||||||
|
|
||||||
@ -7,20 +8,24 @@ main {
|
|||||||
ubyte xx = 1.234
|
ubyte xx = 1.234
|
||||||
ubyte yy = 2.234
|
ubyte yy = 2.234
|
||||||
uword aw
|
uword aw
|
||||||
|
byte bb
|
||||||
|
float fl
|
||||||
|
|
||||||
; TODO: bitwise operations with a negative constant number -> replace the number by its positive 2 complement
|
; TODO: bitwise operations with a negative constant number -> replace the number by its positive 2 complement
|
||||||
|
|
||||||
aw = xx ^ 65535
|
if aw<1
|
||||||
aw = ~xx
|
yy++
|
||||||
yy = xx ^ 255
|
if aw<=0
|
||||||
yy = ~xx
|
yy++
|
||||||
|
|
||||||
; *(&X) => X
|
if yy<1
|
||||||
; X % 1 => 0
|
yy++
|
||||||
; X / 1 => X
|
if yy<=0
|
||||||
; X ^ -1 => ~x
|
yy++
|
||||||
; X >= 1 => X > 0
|
if bb<1
|
||||||
; X < 1 => X <= 0
|
yy++
|
||||||
|
if bb<=0
|
||||||
|
yy++
|
||||||
|
|
||||||
txt.print_ub(yy)
|
txt.print_ub(yy)
|
||||||
txt.print_uw(aw)
|
txt.print_uw(aw)
|
||||||
|
Loading…
Reference in New Issue
Block a user