P4C
The P4 Compiler
Loading...
Searching...
No Matches
deparse_graph.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_PARDE_CLOT_DEPARSE_GRAPH_H_
20#define BACKENDS_TOFINO_BF_P4C_PARDE_CLOT_DEPARSE_GRAPH_H_
21
22#include "bf-p4c/lib/boost_graph.h"
23#include "bf-p4c/phv/phv_fields.h"
24
25// Declare a type and boost property for DeparseGraph vertices.
26namespace boost {
27enum vertex_deparse_graph_t { vertex_deparse_graph };
28BOOST_INSTALL_PROPERTY(vertex, deparse_graph);
29} // namespace boost
30
35 public:
36 struct NodeInfo {
38 const IR::Constant *constant;
39
42
43 bool isConstant() const { return constant; }
44 bool isField() const { return field; }
45
46 // This seems to be required by Boost, but breaks the representation invariant that at
47 // least one of @constant and @field is non-null. :(
48 NodeInfo() : constant(nullptr), field(nullptr) {}
49
50 explicit NodeInfo(const IR::Constant *constant) : constant(constant), field(nullptr) {
51 BUG_CHECK(constant, "Created a deparse graph node with a null constant");
52 }
53
54 explicit NodeInfo(const PHV::Field *field) : constant(nullptr), field(field) {
55 BUG_CHECK(field, "Created a deparse graph node with a null field");
56 }
57 };
58
59 private:
60 typedef boost::adjacency_list<boost::vecS, boost::vecS,
61 boost::bidirectionalS, // Directed edges.
62 NodeInfo // Label vertices with NodeInfo.
63 >
64 Graph;
65
66 public:
67 using Node = typename Graph::vertex_descriptor;
68
71 Node addField(const PHV::Field *f);
72
74 Node addConst(const IR::Constant *c);
75
77 bool addEdge(Node src, Node dst);
78
80 bool canReach(const PHV::Field *f1, const PHV::Field *f2) const;
81
83 const std::vector<NodeInfo> nodesBetween(const PHV::Field *f1, const PHV::Field *f2) const;
84
85 void clear();
86
87 private:
89 Graph g;
90
93
95 std::map<const PHV::Field *, Node> fieldToVertex;
96
97 mutable Reachability<Graph> reachability;
98
99 public:
100 DeparseGraph() : reachability(g) {}
101};
102
103#endif /* BACKENDS_TOFINO_BF_P4C_PARDE_CLOT_DEPARSE_GRAPH_H_ */
Definition deparse_graph.h:34
bool canReach(const PHV::Field *f1, const PHV::Field *f2) const
Determines whether f1 might be deparsed before f2.
Definition deparse_graph.cpp:53
const std::vector< NodeInfo > nodesBetween(const PHV::Field *f1, const PHV::Field *f2) const
Definition deparse_graph.cpp:63
Node addField(const PHV::Field *f)
Definition deparse_graph.cpp:21
bool addEdge(Node src, Node dst)
Adds an edge from src to dst.
Definition deparse_graph.cpp:34
Node addConst(const IR::Constant *c)
Definition deparse_graph.cpp:29
Definition ordered_set.h:32
Definition phv_fields.h:154
Definition boost_graph.h:39
Definition table_flow_graph.h:33
Definition deparse_graph.h:36
const PHV::Field * field
Non-null if this node represents the deparsing of a field.
Definition deparse_graph.h:41
const IR::Constant * constant
Non-null if this node represents the deparsing of a constant.
Definition deparse_graph.h:38