P4C
The P4 Compiler
Loading...
Searching...
No Matches
controlFlowGraph.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_BMV2_COMMON_CONTROLFLOWGRAPH_H_
18#define BACKENDS_BMV2_COMMON_CONTROLFLOWGRAPH_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/typeMap.h"
22#include "ir/ir.h"
23#include "lib/castable.h"
24#include "lib/ordered_set.h"
25
26namespace P4::BMV2 {
27
30class CFG final : public IHasDbPrint {
31 public:
32 class Edge;
33 class Node;
34
35 class EdgeSet final : public IHasDbPrint {
36 public:
38
39 EdgeSet() = default;
40 explicit EdgeSet(CFG::Edge *edge) { edges.emplace(edge); }
41 explicit EdgeSet(const EdgeSet *other) { mergeWith(other); }
42
43 void mergeWith(const EdgeSet *other) {
44 edges.insert(other->edges.begin(), other->edges.end());
45 }
46 void dbprint(std::ostream &out) const;
47 void emplace(CFG::Edge *edge) { edges.emplace(edge); }
48 size_t size() const { return edges.size(); }
52 bool checkSame(const EdgeSet &other) const;
57 bool isDestination(const CFG::Node *destination) const;
58 };
59
60 class Node : public IHasDbPrint, public ICastable {
61 protected:
62 friend class CFG;
63
64 static unsigned crtId;
65 EdgeSet predecessors;
66 explicit Node(cstring name) : id(crtId++), name(name) {}
67 Node() : id(crtId++), name("node_" + Util::toString(id)) {}
68 virtual ~Node() {}
69
70 public:
71 const unsigned id;
72 const cstring name;
73 EdgeSet successors;
74
75 void dbprint(std::ostream &out) const override;
76 void addPredecessors(const EdgeSet *set);
77 void computeSuccessors();
78 cstring toString() const { return name; }
79
80 DECLARE_TYPEINFO(Node);
81 };
82
83 public:
84 class TableNode final : public Node {
85 public:
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) {
90 CHECK_NULL(table);
91 CHECK_NULL(invocation);
92 }
93
94 DECLARE_TYPEINFO(TableNode, Node);
95 };
96
97 class IfNode final : public Node {
98 public:
99 const IR::IfStatement *statement;
100 explicit IfNode(const IR::IfStatement *statement) : statement(statement) {
101 CHECK_NULL(statement);
102 }
103
104 DECLARE_TYPEINFO(IfNode, Node);
105 };
106
107 class DummyNode final : public Node {
108 public:
109 explicit DummyNode(cstring name) : Node(name) {}
110
111 DECLARE_TYPEINFO(DummyNode, Node);
112 };
113
114 protected:
115 enum class EdgeType { Unconditional, True, False, Label };
116
117 public:
119 class Edge final {
120 protected:
121 EdgeType type;
122 Edge(Node *node, EdgeType type, cstring label) : type(type), endpoint(node), label(label) {}
123
124 public:
127 cstring label; // only present if type == Label
128
129 explicit Edge(Node *node) : type(EdgeType::Unconditional), endpoint(node) {
130 CHECK_NULL(node);
131 }
132 Edge(Node *node, bool b) : type(b ? EdgeType::True : EdgeType::False), endpoint(node) {
133 CHECK_NULL(node);
134 }
135 Edge(Node *node, cstring label) : type(EdgeType::Label), endpoint(node), label(label) {
136 CHECK_NULL(node);
137 }
138 void dbprint(std::ostream &out) const;
139 Edge *clone(Node *node) const { return new Edge(node, type, label); }
140 Node *getNode() { return endpoint; }
141 bool getBool() {
142 BUG_CHECK(isBool(), "Edge is not Boolean");
143 return type == EdgeType::True;
144 }
145 bool isBool() const { return type == EdgeType::True || type == EdgeType::False; }
146 bool isUnconditional() const { return type == EdgeType::Unconditional; }
147 };
148
149 public:
150 Node *entryPoint;
151 Node *exitPoint;
152 const IR::P4Control *container;
153 ordered_set<Node *> allNodes;
154
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);
159 return result;
160 }
161 Node *makeNode(const IR::IfStatement *statement) {
162 auto result = new IfNode(statement);
163 allNodes.emplace(result);
164 return result;
165 }
166 Node *makeNode(cstring name) {
167 auto result = new DummyNode(name);
168 allNodes.emplace(result);
169 return result;
170 }
171 void build(const IR::P4Control *cc, P4::ReferenceMap *refMap, P4::TypeMap *typeMap);
172 void setEntry(Node *entry) {
173 BUG_CHECK(entryPoint == nullptr, "Entry already set");
174 entryPoint = entry;
175 }
176 void dbprint(std::ostream &out, Node *node, std::set<Node *> &done) const; // helper
177 void dbprint(std::ostream &out) const;
178 void computeSuccessors() {
179 for (auto n : allNodes) n->computeSuccessors();
180 }
183 bool checkImplementable() const;
184
185 private:
186 bool dfs(Node *node, std::set<Node *> &visited, std::set<const IR::P4Table *> &stack) const;
192 bool checkMergeable(std::set<TableNode *> nodes) const;
193};
194
195} // namespace P4::BMV2
196
197#endif /* BACKENDS_BMV2_COMMON_CONTROLFLOWGRAPH_H_ */
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 castable.h:36
Definition stringify.h:33
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition cstring.h:85
Definition ordered_set.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21