6
6
#include " backends/p4tools/common/lib/table_utils.h"
7
7
#include " backends/p4tools/modules/flay/core/lib/return_macros.h"
8
8
#include " backends/p4tools/modules/flay/options.h"
9
+ #include " ir/indexed_vector.h"
9
10
#include " ir/node.h"
10
11
#include " ir/vector.h"
11
12
#include " lib/error.h"
@@ -83,7 +84,7 @@ const IR::Node *ElimDeadCode::preorder(IR::SwitchStatement *switchStmt) {
83
84
}
84
85
continue ;
85
86
}
86
- printInfo (" ---DEAD_CODE--- %1% can be deleted." , switchCase->label );
87
+ printInfo (" ---DEAD_CODE--- SwitchCase %1% can be deleted." , switchCase->label );
87
88
_eliminatedNodes.emplace_back (switchCase, nullptr );
88
89
// We are removing a statement that had previous fall-through labels.
89
90
if (previousFallThrough && !filteredSwitchCases.empty () &&
@@ -150,6 +151,54 @@ const IR::Node *ElimDeadCode::preorder(IR::Member *member) {
150
151
return result;
151
152
}
152
153
154
+ const IR::Node *ElimDeadCode::preorder (IR::P4Table *table) {
155
+ IR::IndexedVector<IR::ActionListElement> actionListVector;
156
+
157
+ ASSIGN_OR_RETURN_WITH_MESSAGE (const auto &defaultAction, table->getDefaultAction (), table,
158
+ ::P4::error (" Table %1% does not have a default action." , table));
159
+ ASSIGN_OR_RETURN_WITH_MESSAGE (
160
+ const auto &defaultActionCall, defaultAction.to <IR::MethodCallExpression>(), table,
161
+ ::P4::error (" %1% is not a method call expression." , defaultAction));
162
+
163
+ const auto *actionList = table->getActionList ();
164
+ for (const auto *action : actionList->actionList ) {
165
+ // Do not remove actions which have a default only annotation.
166
+ // Do not remove the default action.
167
+ if (action->getAnnotation (IR::Annotation::defaultOnlyAnnotation) != nullptr ||
168
+ defaultActionCall.method ->toString () ==
169
+ action->expression ->checkedTo <IR::MethodCallExpression>()->method ->toString ()) {
170
+ actionListVector.push_back (action);
171
+ continue ;
172
+ }
173
+ auto reachabilityOpt = _reachabilityMap.get ().isNodeReachable (action);
174
+ if (!reachabilityOpt.has_value ()) {
175
+ actionListVector.push_back (action);
176
+ continue ;
177
+ }
178
+ if (reachabilityOpt.value ()) {
179
+ printInfo (" ---DEAD_CODE--- ActionListElement %1% will always be executed." , action);
180
+ actionListVector.clear ();
181
+ actionListVector.push_back (action);
182
+ break ;
183
+ }
184
+ _eliminatedNodes.emplace_back (action, nullptr );
185
+ printInfo (" ---DEAD_CODE--- ActionListElement %1% can be deleted." , action);
186
+ }
187
+ auto *newActionList = new IR::ActionList (actionListVector);
188
+ IR::TableProperties properties;
189
+ for (const auto *property : table->properties ->properties ) {
190
+ if (property->name == IR::TableProperties::actionsPropertyName) {
191
+ auto *newProp = property->clone ();
192
+ newProp->value = newActionList;
193
+ properties.push_back (newProp);
194
+ } else {
195
+ properties.push_back (property);
196
+ }
197
+ }
198
+ table->properties = new IR::TableProperties (properties);
199
+ return table;
200
+ }
201
+
153
202
const IR::Node *ElimDeadCode::preorder (IR::MethodCallStatement *stmt) {
154
203
const auto *call = stmt->methodCall ->method ->to <IR::Member>();
155
204
RETURN_IF_FALSE (call != nullptr && call->member == IR::IApply::applyMethodName, stmt);
@@ -169,7 +218,7 @@ const IR::Node *ElimDeadCode::preorder(IR::MethodCallStatement *stmt) {
169
218
::P4::error (" Table %1% does not have a default action." , tableDecl));
170
219
ASSIGN_OR_RETURN_WITH_MESSAGE (
171
220
const auto &defaultActionCall, defaultAction.to <IR::MethodCallExpression>(), stmt,
172
- ::P4::error (" %1% is not a method call expression." , table.getDefaultAction() ));
221
+ ::P4::error (" %1% is not a method call expression." , defaultAction ));
173
222
for (const auto *action : tableActionList) {
174
223
// Do not remove the default action.
175
224
if (defaultActionCall.method ->toString () ==
0 commit comments