@@ -60,24 +60,42 @@ const IR::Node *ElimDeadCode::preorder(IR::IfStatement *stmt) {
60
60
61
61
const IR::Node *ElimDeadCode::preorder (IR::SwitchStatement *switchStmt) {
62
62
IR::Vector<IR::SwitchCase> filteredSwitchCases;
63
+ bool previousFallThrough = false ;
63
64
for (const auto *switchCase : switchStmt->cases ) {
64
65
if (switchCase->label ->is <IR::DefaultExpression>()) {
65
66
filteredSwitchCases.push_back (switchCase);
66
67
break ;
67
68
}
68
69
auto isreachabilityOpt = _reachabilityMap.get ().isNodeReachable (switchCase);
69
70
if (!isreachabilityOpt.has_value ()) {
71
+ printInfo (" ---DEAD_CODE--- SwitchCase %1% can be executed." , switchCase->label );
70
72
filteredSwitchCases.push_back (switchCase);
73
+ previousFallThrough = switchCase->statement == nullptr ;
71
74
continue ;
72
75
}
73
76
auto reachability = isreachabilityOpt.value ();
74
77
if (reachability) {
75
78
filteredSwitchCases.push_back (switchCase);
76
- printInfo (" ---DEAD_CODE--- %1% is always true." , switchCase->label );
77
- break ;
79
+ printInfo (" ---DEAD_CODE--- SwitchCase %1% is always true." , switchCase->label );
80
+ previousFallThrough = switchCase->statement == nullptr ;
81
+ if (switchCase->statement != nullptr ) {
82
+ break ;
83
+ }
84
+ continue ;
78
85
}
79
86
printInfo (" ---DEAD_CODE--- %1% can be deleted." , switchCase->label );
80
87
_eliminatedNodes.emplace_back (switchCase, nullptr );
88
+ // We are removing a statement that had previous fall-through labels.
89
+ if (previousFallThrough && !filteredSwitchCases.empty () &&
90
+ switchCase->statement != nullptr ) {
91
+ auto *previous = filteredSwitchCases.back ()->clone ();
92
+ printInfo (" ---DEAD_CODE--- Merging statements of %1% into %2%." , switchCase->label ,
93
+ previous->label );
94
+ previous->statement = switchCase->statement ;
95
+ filteredSwitchCases.pop_back ();
96
+ filteredSwitchCases.push_back (previous);
97
+ }
98
+ previousFallThrough = switchCase->statement == nullptr ;
81
99
}
82
100
if (filteredSwitchCases.empty ()) {
83
101
return new IR::EmptyStatement (switchStmt->getSourceInfo ());
0 commit comments