detect some optimization of GOTO jumps

This commit is contained in:
Volker Berlin 2019-09-25 19:24:01 +02:00
parent c29d19178a
commit 40375316e2
2 changed files with 24 additions and 0 deletions

View File

@ -186,6 +186,14 @@ class BranchManger {
convertToLoop( parsedBlock, conditionStart, conditionEnd );
allParsedOperations.remove( n );
((IfParsedBlock)nextBlock).negateCompare();
// if conditions that point at the end of the loop for optimization must now point at start.
for( n = b - 1; n >= 0; n-- ) {
ParsedBlock prevBlock = allParsedOperations.get( n );
if( prevBlock.op == JavaBlockOperator.IF && prevBlock.endPosition == conditionStart ) {
prevBlock.endPosition = parsedBlock.startPosition;
}
}
break;
}
if( nextBlock.op == JavaBlockOperator.GOTO && nextBlock.endPosition == nextPos && n > 1 ) { // Eclipse loop with wide goto_w

View File

@ -70,6 +70,7 @@ public class ControlFlowOperators extends AbstractBaseTest {
addParam( list, script, "ifAndOr4" );
addParam( list, script, "ifAndOr6" );
addParam( list, script, "ifAndOr8" );
addParam( list, script, "ifWithoutElseAndLoop" );
}
rule.setTestParameters( list );
return list;
@ -449,5 +450,20 @@ public class ControlFlowOperators extends AbstractBaseTest {
}
return result;
}
private static int ifWithoutElseAndLoop;
@Export
static int ifWithoutElseAndLoop() {
int n = 1;
// because some compiler (Eclipse) move the loop condition to the end of the loop. Then there can be an optimization that the if jump to the end of the loop.
if( ifWithoutElseAndLoop != 1 ) {
ifWithoutElseAndLoop = 1;
}
while( n < 10 ) {
ifWithoutElseAndLoop *= n++;
}
return ifWithoutElseAndLoop;
}
}
}