Skip to content

Commit 0015f75

Browse files
Alexey AndreevAlexey Andreev
Alexey Andreev
authored and
Alexey Andreev
committed
JS: improve dead code elimination to handle switch case bodies
1 parent 22aea1c commit 0015f75

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/DeadCodeElimination.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ internal class DeadCodeElimination(private val root: JsStatement) {
5656

5757
override fun visitBlock(x: JsBlock) {
5858
canContinue = true
59-
for ((index, statement) in x.statements.withIndex()) {
59+
visitStatements(x.statements)
60+
}
61+
62+
private fun visitStatements(statements: MutableList<JsStatement>) {
63+
for ((index, statement) in statements.withIndex()) {
6064
accept(statement)
6165
if (!canContinue) {
62-
val removedStatements = x.statements.subList(index + 1, x.statements.size)
66+
val removedStatements = statements.subList(index + 1, statements.size)
6367
if (removedStatements.isNotEmpty()) {
6468
hasChanges = true
6569
removedStatements.clear()
@@ -174,7 +178,7 @@ internal class DeadCodeElimination(private val root: JsStatement) {
174178

175179
for (caseBlock in x.cases) {
176180
canContinue = true
177-
caseBlock.statements.forEach { accept(it) }
181+
visitStatements(caseBlock.statements)
178182

179183
if (!canContinue && localBreakExists) {
180184
canContinue = true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.js.test.optimizer
18+
19+
import org.junit.Test
20+
21+
class DeadCodeEliminationTest : BasicOptimizerTest("dead-code-elimination") {
22+
@Test fun switchCases() = box()
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function test(x) {
2+
var log = "";
3+
switch (x) {
4+
case 0:
5+
log += "0;";
6+
break;
7+
case 1:
8+
return "one";
9+
case 2:
10+
log += "2;";
11+
case 3:
12+
log += "3;";
13+
break;
14+
default:
15+
if (x == 4) {
16+
log += "four;";
17+
break;
18+
}
19+
else {
20+
return "default";
21+
}
22+
}
23+
return log;
24+
}
25+
26+
function box() {
27+
if (test(0) != "0;") return "fail1";
28+
if (test(1) != "one") return "fail2";
29+
if (test(2) != "2;3;") return "fail3";
30+
if (test(3) != "3;") return "fail4";
31+
if (test(4) != "four;") return "fail5";
32+
if (test(5) != "default") return "fail5";
33+
34+
return "OK";
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function test(x) {
2+
var log = "";
3+
switch (x) {
4+
case 0:
5+
log += "0;";
6+
break;
7+
log += "00;";
8+
break;
9+
case 1:
10+
return "one";
11+
return "uno";
12+
case 2:
13+
log += "2;";
14+
case 3:
15+
log += "3;";
16+
break;
17+
default:
18+
if (x == 4) {
19+
log += "four;";
20+
break;
21+
}
22+
else {
23+
return "default";
24+
}
25+
log += "!";
26+
return "!" + log;
27+
}
28+
return log;
29+
}
30+
31+
function box() {
32+
if (test(0) != "0;") return "fail1";
33+
if (test(1) != "one") return "fail2";
34+
if (test(2) != "2;3;") return "fail3";
35+
if (test(3) != "3;") return "fail4";
36+
if (test(4) != "four;") return "fail5";
37+
if (test(5) != "default") return "fail5";
38+
39+
return "OK";
40+
}

0 commit comments

Comments
 (0)