P4C
The P4 Compiler
Loading...
Searching...
No Matches
global_copyprop.h
1#ifndef MIDEND_GLOBAL_COPYPROP_H_
2#define MIDEND_GLOBAL_COPYPROP_H_
3
4#include "frontends/common/resolveReferences/referenceMap.h"
5#include "frontends/p4/typeChecking/typeChecker.h"
6#include "ir/ir.h"
7
8namespace P4 {
56
63class FindVariableValues final : public Inspector {
64 ReferenceMap *refMap;
65 TypeMap *typeMap;
66 // Container for storing constant values for variables, is used as a representation of
67 // the current state of the variables in the program. Keys are variable names and values
68 // are pointers to 'Expression' nodes that represent a literal value for that variable.
69 std::map<cstring, const IR::Expression *> &vars;
70 // Container used to store information needed for later propagating by the Transformer pass.
71 // Keys for outer map are pointers to action nodes whose bodies need to be rewritten by the
72 // Transformer pass and values are maps that store literal values for variables.
73 // This inner map uses the name of the variable as a key and the pointer to the 'Expression'
74 // node, that represents a literal, as a value.
75 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *actions;
76 // Flag for controlling which IR nodes this pass operates on
77 bool working = false;
78
79 bool preorder(const IR::IfStatement *) override;
80 bool preorder(const IR::SwitchStatement *) override;
81 bool preorder(const IR::ForStatement *) override;
82 bool preorder(const IR::ForInStatement *) override;
83 void postorder(const IR::P4Control *) override;
84 bool preorder(const IR::P4Control *) override;
85 bool preorder(const IR::P4Table *) override;
86 bool preorder(const IR::P4Action *) override;
87 bool preorder(const IR::AssignmentStatement *) override;
88 bool preorder(const IR::OpAssignmentStatement *) override;
89 void postorder(const IR::MethodCallExpression *) override;
90
91 public:
92 FindVariableValues(
93 ReferenceMap *refMap, TypeMap *typeMap,
94 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *acts)
95 : refMap(refMap),
96 typeMap(typeMap),
97 vars(*new std::map<cstring, const IR::Expression *>),
98 actions(acts) {}
99};
100
106class DoGlobalCopyPropagation final : public Transform {
107 ReferenceMap *refMap;
108 TypeMap *typeMap;
109 // Container for storing constant values for variables, used as a representation of
110 // the current state of the variables in the program. Keys are variable names and values
111 // are pointers to 'Expression' nodes that represent a literal value for that variable.
112 std::map<cstring, const IR::Expression *> *vars = nullptr;
113 // Container used to store information needed for propagating.
114 // Keys for outer map are pointers to action nodes whose bodies need to be rewritten by this
115 // pass and values represent maps that store literal values for variables. This inner map uses
116 // the name of the variable as a key and the pointer to the 'Expression' node, that represents a
117 // literal, as a value.
118 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *actions;
119 // Flag for controlling which IR nodes this pass operates on
120 bool performRewrite = false;
121
122 public:
123 explicit DoGlobalCopyPropagation(
124 ReferenceMap *rM, TypeMap *tM,
125 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *acts)
126 : refMap(rM), typeMap(tM), actions(acts) {}
127
128 // Returns the stored value for the used variable
129 const IR::Expression *copyprop_name(cstring name);
130
131 IR::IfStatement *preorder(IR::IfStatement *) override;
132 IR::ForStatement *preorder(IR::ForStatement *) override;
133 IR::ForInStatement *preorder(IR::ForInStatement *) override;
134 IR::BaseAssignmentStatement *preorder(IR::BaseAssignmentStatement *) override;
135 IR::Statement *preorder(IR::AssignmentStatement *) override;
136 const IR::Expression *postorder(IR::PathExpression *) override;
137 const IR::Expression *preorder(IR::ArrayIndex *) override;
138 const IR::Expression *preorder(IR::Member *) override;
139 const IR::P4Action *preorder(IR::P4Action *) override;
140 const IR::P4Action *postorder(IR::P4Action *) override;
141 IR::MethodCallExpression *postorder(IR::MethodCallExpression *) override;
142};
143
144class GlobalCopyPropagation : public PassManager {
145 public:
146 GlobalCopyPropagation(ReferenceMap *rM, TypeMap *tM) {
147 passes.push_back(new TypeChecking(rM, tM, true));
148 auto acts = new std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *>;
149 passes.push_back(new FindVariableValues(rM, tM, acts));
150 passes.push_back(new DoGlobalCopyPropagation(rM, tM, acts));
151 setName("GlobalCopyPropagation");
152 }
153};
154
155} // namespace P4
156
157#endif /* MIDEND_GLOBAL_COPYPROP_H_ */
Definition global_copyprop.h:106
Definition global_copyprop.h:63
Definition node.h:94
Definition visitor.h:413
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition visitor.h:437
Definition typeChecker.h:55
Definition typeMap.h:41
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24