44 edges.insert(
other->edges.begin(),
other->edges.end());
46 void dbprint(std::ostream &out)
const;
48 size_t size()
const {
return edges.size(); }
64 static unsigned crtId;
66 explicit Node(
cstring name) : id(crtId++), name(name) {}
67 Node() : id(crtId++), name(
"node_" + Util::toString(
id)) {}
75 void dbprint(std::ostream &out)
const override;
76 void addPredecessors(
const EdgeSet *set);
77 void computeSuccessors();
78 cstring toString()
const {
return name; }
80 DECLARE_TYPEINFO(
Node);
86 const IR::P4Table *table;
87 const IR::Expression *invocation;
88 explicit TableNode(
const IR::P4Table *table,
const IR::Expression *invocation)
89 :
Node(table->controlPlaneName()), table(table), invocation(invocation) {
91 CHECK_NULL(invocation);
99 const IR::IfStatement *statement;
100 explicit IfNode(
const IR::IfStatement *statement) : statement(statement) {
101 CHECK_NULL(statement);
115 enum class EdgeType { Unconditional, True, False, Label };
136 Edge(
Node *node,
bool b) : type(b ? EdgeType::True : EdgeType::False),
endpoint(node) {
139 Edge(Node *node,
cstring label) : type(EdgeType::Label),
endpoint(node), label(label) {
142 void dbprint(std::ostream &out)
const;
143 Edge *clone(Node *node)
const {
return new Edge(node, type, label); }
144 Node *getNode() {
return endpoint; }
146 BUG_CHECK(isBool(),
"Edge is not Boolean");
147 return type == EdgeType::True;
149 bool isBool()
const {
return type == EdgeType::True || type == EdgeType::False; }
150 bool isUnconditional()
const {
return type == EdgeType::Unconditional; }
156 const IR::P4Control *container;
159 CFG() : entryPoint(nullptr), exitPoint(nullptr), container(nullptr) {}
160 Node *makeNode(
const IR::P4Table *table,
const IR::Expression *invocation) {
161 auto result =
new TableNode(table, invocation);
162 allNodes.emplace(result);
165 Node *makeNode(
const IR::IfStatement *statement) {
166 auto result =
new IfNode(statement);
167 allNodes.emplace(result);
171 auto result =
new DummyNode(name);
172 allNodes.emplace(result);
176 void setEntry(Node *entry) {
177 BUG_CHECK(entryPoint ==
nullptr,
"Entry already set");
180 void dbprint(std::ostream &out, Node *node, std::set<Node *> &done)
const;
181 void dbprint(std::ostream &out)
const;
182 void computeSuccessors() {
183 for (
auto n : allNodes) n->computeSuccessors();
190 bool dfs(Node *node, std::set<Node *> &visited, std::set<const IR::P4Table *> &stack)
const;
196 bool checkMergeable(std::set<TableNode *> nodes)
const;