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 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:
121 class Edge final {
122 protected:
123 EdgeType type;
124 Edge(Node *node, EdgeType type, cstring label) : type(type), endpoint(node), label(label) {}
125
126 public:
131 cstring label; // only present if type == Label
132
133 explicit Edge(Node *node) : type(EdgeType::Unconditional), endpoint(node) {
134 CHECK_NULL(node);
135 }
136 Edge(Node *node, bool b) : type(b ? EdgeType::True : EdgeType::False), endpoint(node) {
137 CHECK_NULL(node);
138 }
139 Edge(Node *node, cstring label) : type(EdgeType::Label), endpoint(node), label(label) {
140 CHECK_NULL(node);
141 }
142 void dbprint(std::ostream &out) const;
143 Edge *clone(Node *node) const { return new Edge(node, type, label); }
144 Node *getNode() { return endpoint; }
145 bool getBool() {
146 BUG_CHECK(isBool(), "Edge is not Boolean");
147 return type == EdgeType::True;
148 }
149 bool isBool() const { return type == EdgeType::True || type == EdgeType::False; }
150 bool isUnconditional() const { return type == EdgeType::Unconditional; }
151 };
152
153 public:
154 Node *entryPoint;
155 Node *exitPoint;
156 const IR::P4Control *container;
157 ordered_set<Node *> allNodes;
158
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);
163 return result;
164 }
165 Node *makeNode(const IR::IfStatement *statement) {
166 auto result = new IfNode(statement);
167 allNodes.emplace(result);
168 return result;
169 }
170 Node *makeNode(cstring name) {
171 auto result = new DummyNode(name);
172 allNodes.emplace(result);
173 return result;
174 }
175 void build(const IR::P4Control *cc, P4::ReferenceMap *refMap, P4::TypeMap *typeMap);
176 void setEntry(Node *entry) {
177 BUG_CHECK(entryPoint == nullptr, "Entry already set");
178 entryPoint = entry;
179 }
180 void dbprint(std::ostream &out, Node *node, std::set<Node *> &done) const; // helper
181 void dbprint(std::ostream &out) const;
182 void computeSuccessors() {
183 for (auto n : allNodes) n->computeSuccessors();
184 }
187 bool checkImplementable() const;
188
189 private:
190 bool dfs(Node *node, std::set<Node *> &visited, std::set<const IR::P4Table *> &stack) const;
196 bool checkMergeable(std::set<TableNode *> nodes) const;
197};
198
199} // namespace BMV2
200
201#endif /* BACKENDS_BMV2_COMMON_CONTROLFLOWGRAPH_H_ */
Definition controlFlowGraph.h:107
Definition controlFlowGraph.h:121
Node * endpoint
Definition controlFlowGraph.h:130
Definition controlFlowGraph.h:35
bool isDestination(const CFG::Node *destination) const
Definition controlFlowGraph.cpp:100
bool checkSame(const EdgeSet &other) const
Definition controlFlowGraph.cpp:112
Definition controlFlowGraph.h:97
Definition controlFlowGraph.h:60
Definition controlFlowGraph.h:84
Definition controlFlowGraph.h:30
bool checkImplementable() const
Definition controlFlowGraph.cpp:145
Definition sharedActionSelectorCheck.h:40
Definition castable.h:34
Definition source_file.h:38
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition cstring.h:72
Definition ordered_set.h:30