P4C
The P4 Compiler
Loading...
Searching...
No Matches
remove_noop_gateway.h
1
19#ifndef BF_P4C_MAU_REMOVE_NOOP_GATEWAY_H_
20#define BF_P4C_MAU_REMOVE_NOOP_GATEWAY_H_
21
22#include "mau_visitor.h"
23
24using namespace P4;
25
26/* "Noop" gateways that don't actually test anything and just trigger another TableSeq
27 * may be introduced by MulitpleApply::MergeTails; they don't actually do anything, but
28 * are needed to allow table placement to place the tables properly. We may be able to
29 * eliminate them after table placement, which this pass attempts to do */
31 const IR::MAU::TableSeq *preorder(IR::MAU::TableSeq *seq) override {
32 if (seq->size() < 1) return seq;
33 auto *last = seq->back();
34 if (!last->conditional_gateway_only() || last->gateway_rows.size() != 1 ||
35 last->next.size() != 1) {
36 // not a noop gateway
37 return seq;
38 }
39 if (seq->size() == 1) {
40 // drop the noop gateway, going directly to the target TableSeq
41 return last->next.begin()->second;
42 }
43 auto *prev = seq->tables.at(seq->size() - 2);
44 if (!prev->next.empty()) {
45 // can't move the dependent tables to the previous table
46 return seq;
47 }
48 seq->tables.pop_back(); // toss the gateway;
49 auto *clone = prev->clone(); // clone the last table
50 clone->next["$default"_cs] = last->next.begin()->second;
51 // move the dependent sequence to the last table as default next
52 seq->tables.back() = clone;
53 return seq;
54 }
55};
56
57#endif /* BF_P4C_MAU_REMOVE_NOOP_GATEWAY_H_ */
Definition mau_visitor.h:55
Definition remove_noop_gateway.h:30
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24