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 {
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 void postorder(const IR::MethodCallExpression *) override;
89
90 public:
92 ReferenceMap *refMap, TypeMap *typeMap,
93 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *acts)
94 : refMap(refMap),
95 typeMap(typeMap),
96 vars(*new std::map<cstring, const IR::Expression *>),
97 actions(acts) {}
98};
99
105class DoGlobalCopyPropagation final : public Transform {
106 ReferenceMap *refMap;
107 TypeMap *typeMap;
108 // Container for storing constant values for variables, used as a representation of
109 // the current state of the variables in the program. Keys are variable names and values
110 // are pointers to 'Expression' nodes that represent a literal value for that variable.
111 std::map<cstring, const IR::Expression *> *vars = nullptr;
112 // Container used to store information needed for propagating.
113 // Keys for outer map are pointers to action nodes whose bodies need to be rewritten by this
114 // pass and values represent maps that store literal values for variables. This inner map uses
115 // the name of the variable as a key and the pointer to the 'Expression' node, that represents a
116 // literal, as a value.
117 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *actions;
118 // Flag for controlling which IR nodes this pass operates on
119 bool performRewrite = false;
120
121 public:
123 ReferenceMap *rM, TypeMap *tM,
124 std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *> *acts)
125 : refMap(rM), typeMap(tM), actions(acts) {}
126
127 // Returns the stored value for the used variable
128 const IR::Expression *copyprop_name(cstring name);
129
130 IR::IfStatement *preorder(IR::IfStatement *) override;
131 IR::ForStatement *preorder(IR::ForStatement *) override;
132 IR::ForInStatement *preorder(IR::ForInStatement *) override;
133 const IR::Expression *postorder(IR::PathExpression *) override;
134 const IR::Expression *preorder(IR::ArrayIndex *) override;
135 const IR::Expression *preorder(IR::Member *) override;
136 const IR::Node *preorder(IR::AssignmentStatement *) override;
137 const IR::P4Action *preorder(IR::P4Action *) override;
138 const IR::P4Action *postorder(IR::P4Action *) override;
139 IR::MethodCallExpression *postorder(IR::MethodCallExpression *) override;
140};
141
143 public:
145 passes.push_back(new TypeChecking(rM, tM, true));
146 auto acts = new std::map<const IR::Node *, std::map<cstring, const IR::Expression *> *>;
147 passes.push_back(new FindVariableValues(rM, tM, acts));
148 passes.push_back(new DoGlobalCopyPropagation(rM, tM, acts));
149 setName("GlobalCopyPropagation");
150 }
151};
152
153} // namespace P4
154
155#endif /* MIDEND_GLOBAL_COPYPROP_H_ */
Definition global_copyprop.h:105
Definition global_copyprop.h:63
Definition global_copyprop.h:142
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
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