Skip to content

Commit bdac88a

Browse files
committed
Better dead-code-elimination inside PatternMatcher.
If the head of a `SeqPlan` cannot fall through, the tail is unreachable and can be removed.
1 parent 63b2610 commit bdac88a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,12 @@ object PatternMatcher {
457457
apply(initializer(plan.sym))
458458
plan
459459
}
460+
override def apply(plan: SeqPlan): Plan = {
461+
apply(plan.head)
462+
if (canFallThrough(plan.head))
463+
apply(plan.tail)
464+
plan
465+
}
460466
}
461467
refCounter(plan)
462468
refCounter.count
@@ -564,8 +570,10 @@ object PatternMatcher {
564570
new MergeTests()(plan)
565571
}
566572

567-
/** Inline let-bound trees that are referenced only once.
568-
* Drop all variables that are not referenced anymore after this.
573+
/** Inline let-bound trees that are referenced only once and eliminate dead code.
574+
*
575+
* - Drop all variables that are not referenced anymore after inlining.
576+
* - Drop the `tail` of `SeqPlan`s whose `head` cannot fall through.
569577
*/
570578
private def inlineVars(plan: Plan): Plan = {
571579
val refCount = varRefCount(plan)
@@ -597,6 +605,18 @@ object PatternMatcher {
597605
plan
598606
}
599607
}
608+
override def apply(plan: SeqPlan): Plan = {
609+
val newHead = apply(plan.head)
610+
if (!canFallThrough(newHead)) {
611+
// If the head cannot fall through, the tail is dead code
612+
newHead
613+
}
614+
else {
615+
plan.head = newHead
616+
plan.tail = apply(plan.tail)
617+
plan
618+
}
619+
}
600620
}
601621
Inliner(plan)
602622
}

0 commit comments

Comments
 (0)