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