optimize redundant typecasts more

This commit is contained in:
Irmen de Jong 2019-07-10 01:52:04 +02:00
parent c9b16dcbd9
commit 12cb7d7abe
2 changed files with 12 additions and 3 deletions

View File

@ -38,7 +38,7 @@ enum class DataType {
infix fun isAssignableTo(targetTypes: Set<DataType>) = targetTypes.any { this isAssignableTo it }
infix fun biggerThan(other: DataType) =
infix fun largerThan(other: DataType) =
when(this) {
in ByteDatatypes -> false
in WordDatatypes -> other in ByteDatatypes

View File

@ -49,6 +49,15 @@ internal class SimplifyExpressions(private val program: Program) : IAstModifying
return tc.expression
}
}
// if the previous typecast was casting to a 'bigger' type, just ignore that one
val subTc = tc.expression as? TypecastExpression
if(subTc!=null && subTc.type largerThan tc.type) {
subTc.type = tc.type
subTc.parent = tc.parent
optimizationsDone++
return subTc
}
return super.visit(tc)
}
optimizationsDone++
@ -385,7 +394,7 @@ internal class SimplifyExpressions(private val program: Program) : IAstModifying
}
if(leftConstVal==null && rightConstVal!=null) {
if(leftDt biggerThan rightDt) {
if(leftDt largerThan rightDt) {
val (adjusted, newValue) = adjust(rightConstVal, leftDt)
if (adjusted) {
expr.right = newValue
@ -395,7 +404,7 @@ internal class SimplifyExpressions(private val program: Program) : IAstModifying
}
return false
} else if(leftConstVal!=null && rightConstVal==null) {
if(rightDt biggerThan leftDt) {
if(rightDt largerThan leftDt) {
val (adjusted, newValue) = adjust(leftConstVal, rightDt)
if (adjusted) {
expr.left = newValue