JS: improve dead code elimination to handle switch case bodies

This commit is contained in:
Alexey Andreev
2016-11-09 16:30:05 +03:00
parent 22aea1cf12
commit 0015f7518e
4 changed files with 105 additions and 3 deletions
@@ -56,10 +56,14 @@ internal class DeadCodeElimination(private val root: JsStatement) {
override fun visitBlock(x: JsBlock) {
canContinue = true
for ((index, statement) in x.statements.withIndex()) {
visitStatements(x.statements)
}
private fun visitStatements(statements: MutableList<JsStatement>) {
for ((index, statement) in statements.withIndex()) {
accept(statement)
if (!canContinue) {
val removedStatements = x.statements.subList(index + 1, x.statements.size)
val removedStatements = statements.subList(index + 1, statements.size)
if (removedStatements.isNotEmpty()) {
hasChanges = true
removedStatements.clear()
@@ -174,7 +178,7 @@ internal class DeadCodeElimination(private val root: JsStatement) {
for (caseBlock in x.cases) {
canContinue = true
caseBlock.statements.forEach { accept(it) }
visitStatements(caseBlock.statements)
if (!canContinue && localBreakExists) {
canContinue = true
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.test.optimizer
import org.junit.Test
class DeadCodeEliminationTest : BasicOptimizerTest("dead-code-elimination") {
@Test fun switchCases() = box()
}
@@ -0,0 +1,35 @@
function test(x) {
var log = "";
switch (x) {
case 0:
log += "0;";
break;
case 1:
return "one";
case 2:
log += "2;";
case 3:
log += "3;";
break;
default:
if (x == 4) {
log += "four;";
break;
}
else {
return "default";
}
}
return log;
}
function box() {
if (test(0) != "0;") return "fail1";
if (test(1) != "one") return "fail2";
if (test(2) != "2;3;") return "fail3";
if (test(3) != "3;") return "fail4";
if (test(4) != "four;") return "fail5";
if (test(5) != "default") return "fail5";
return "OK";
}
@@ -0,0 +1,40 @@
function test(x) {
var log = "";
switch (x) {
case 0:
log += "0;";
break;
log += "00;";
break;
case 1:
return "one";
return "uno";
case 2:
log += "2;";
case 3:
log += "3;";
break;
default:
if (x == 4) {
log += "four;";
break;
}
else {
return "default";
}
log += "!";
return "!" + log;
}
return log;
}
function box() {
if (test(0) != "0;") return "fail1";
if (test(1) != "one") return "fail2";
if (test(2) != "2;3;") return "fail3";
if (test(3) != "3;") return "fail4";
if (test(4) != "four;") return "fail5";
if (test(5) != "default") return "fail5";
return "OK";
}