17#ifndef BACKENDS_BMV2_COMMON_CONTROLFLOWGRAPH_H_
18#define BACKENDS_BMV2_COMMON_CONTROLFLOWGRAPH_H_
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/typeMap.h"
23#include "lib/castable.h"
24#include "lib/ordered_set.h"
43 void mergeWith(
const EdgeSet *other) {
44 edges.insert(other->edges.begin(), other->edges.end());
46 void dbprint(std::ostream &out)
const;
47 void emplace(
CFG::Edge *edge) { edges.emplace(edge); }
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 };
132 Edge(
Node *node,
bool b) : type(b ? EdgeType::True : EdgeType::False),
endpoint(node) {
135 Edge(Node *node,
cstring label) : type(EdgeType::Label),
endpoint(node), label(label) {
138 void dbprint(std::ostream &out)
const;
139 Edge *clone(Node *node)
const {
return new Edge(node, type, label); }
140 Node *getNode() {
return endpoint; }
142 BUG_CHECK(isBool(),
"Edge is not Boolean");
143 return type == EdgeType::True;
145 bool isBool()
const {
return type == EdgeType::True || type == EdgeType::False; }
146 bool isUnconditional()
const {
return type == EdgeType::Unconditional; }
152 const IR::P4Control *container;
153 ordered_set<Node *> allNodes;
155 CFG() : entryPoint(nullptr), exitPoint(nullptr), container(nullptr) {}
156 Node *makeNode(
const IR::P4Table *table,
const IR::Expression *invocation) {
157 auto result =
new TableNode(table, invocation);
158 allNodes.emplace(result);
161 Node *makeNode(
const IR::IfStatement *statement) {
162 auto result =
new IfNode(statement);
163 allNodes.emplace(result);
166 Node *makeNode(cstring name) {
167 auto result =
new DummyNode(name);
168 allNodes.emplace(result);
172 void setEntry(Node *entry) {
173 BUG_CHECK(entryPoint ==
nullptr,
"Entry already set");
176 void dbprint(std::ostream &out, Node *node, std::set<Node *> &done)
const;
177 void dbprint(std::ostream &out)
const;
178 void computeSuccessors() {
179 for (
auto n : allNodes) n->computeSuccessors();
186 bool dfs(Node *node, std::set<Node *> &visited, std::set<const IR::P4Table *> &stack)
const;
192 bool checkMergeable(std::set<TableNode *> nodes)
const;
Definition controlFlowGraph.h:107
A CFG Edge; can be an in-edge or out-edge.
Definition controlFlowGraph.h:119
Node * endpoint
The destination node of the edge. The source node is not known by the edge.
Definition controlFlowGraph.h:126
Definition controlFlowGraph.h:35
bool checkSame(const EdgeSet &other) const
Definition controlFlowGraph.cpp:115
bool isDestination(const CFG::Node *destination) const
Definition controlFlowGraph.cpp:103
Definition controlFlowGraph.h:97
Definition controlFlowGraph.h:60
Definition controlFlowGraph.h:84
Definition controlFlowGraph.h:30
bool checkImplementable() const
Definition controlFlowGraph.cpp:148
Definition stringify.h:33
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition ordered_set.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21