P4C
The P4 Compiler
Loading...
Searching...
No Matches
local_copyprop.h
1/* Copyright 2013-present Barefoot Networks, Inc.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14*/
15
16#ifndef MIDEND_LOCAL_COPYPROP_H_
17#define MIDEND_LOCAL_COPYPROP_H_
18
19#include "frontends/common/resolveReferences/referenceMap.h"
20#include "frontends/p4/typeChecking/typeChecker.h"
21#include "has_side_effects.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
53 ReferenceMap *refMap;
54 TypeMap *typeMap;
55 bool working = false;
56 struct VarInfo {
57 bool local = false;
58 bool live = false;
59 const IR::Expression *val = nullptr;
60 };
61 struct TableInfo {
62 std::set<cstring> keyreads, actions;
63 int apply_count = 0;
64 std::map<cstring, const IR::Expression *> key_remap;
65 };
66 struct FuncInfo {
67 std::set<cstring> reads, writes;
68 int apply_count = 0;
69
74 bool is_first_write_insert = false;
75 };
76 std::map<cstring, VarInfo> available;
77 std::map<cstring, TableInfo> &tables;
78 std::map<cstring, FuncInfo> &actions;
79 std::map<cstring, FuncInfo> &methods;
80 std::map<cstring, FuncInfo> &states;
81 TableInfo *inferForTable = nullptr;
82 FuncInfo *inferForFunc = nullptr;
83 bool need_key_rewrite = false;
84 std::function<bool(const Context *, const IR::Expression *)> policy;
85 bool elimUnusedTables = false;
86 int uid = -1;
87 static int uid_ctr;
88
89 DoLocalCopyPropagation *clone() const override {
90 auto *rv = new DoLocalCopyPropagation(*this);
91 rv->uid = ++uid_ctr;
92 LOG8("flow_clone(" << uid << ") = " << rv->uid);
93 return rv;
94 }
95 void flow_merge(Visitor &) override;
96 void flow_copy(ControlFlowVisitor &) override;
97 bool operator==(const ControlFlowVisitor &) const override;
98 bool name_overlap(cstring, cstring);
99 void forOverlapAvail(cstring, std::function<void(cstring, VarInfo *)>);
100 void dropValuesUsing(cstring);
101 bool hasSideEffects(const IR::Expression *e) {
102 return bool(::P4::hasSideEffects(refMap, typeMap, e));
103 }
104 bool isHeaderUnionIsValid(const IR::Expression *e);
105
106 class LoopPrepass : public Inspector {
108 void postorder(const IR::AssignmentStatement *) override;
109 void postorder(const IR::MethodCallExpression *) override;
110 void apply_table(TableInfo *tbl);
111 void apply_function(FuncInfo *tbl);
112
113 public:
114 explicit LoopPrepass(DoLocalCopyPropagation &s) : self(s) {}
115 };
116
117 void visit_local_decl(const IR::Declaration_Variable *);
118 const IR::Node *postorder(IR::Declaration_Variable *) override;
119 IR::Expression *preorder(IR::Expression *m) override;
120 const IR::Expression *copyprop_name(cstring name, const Util::SourceInfo &srcInfo);
121 const IR::Expression *postorder(IR::PathExpression *) override;
122 const IR::Expression *preorder(IR::Member *) override;
123 const IR::Expression *preorder(IR::ArrayIndex *) override;
124 IR::Statement *preorder(IR::Statement *) override;
125 IR::AssignmentStatement *preorder(IR::AssignmentStatement *) override;
126 IR::AssignmentStatement *postorder(IR::AssignmentStatement *) override;
127 IR::IfStatement *postorder(IR::IfStatement *) override;
128 IR::ForStatement *preorder(IR::ForStatement *) override;
129 IR::ForInStatement *preorder(IR::ForInStatement *) override;
130 IR::MethodCallExpression *postorder(IR::MethodCallExpression *) override;
131 IR::P4Action *preorder(IR::P4Action *) override;
132 IR::P4Action *postorder(IR::P4Action *) override;
133 IR::Function *preorder(IR::Function *) override;
134 IR::Function *postorder(IR::Function *) override;
135 IR::P4Control *preorder(IR::P4Control *) override;
136 void apply_table(TableInfo *tbl);
137 void apply_function(FuncInfo *tbl);
138 IR::P4Table *preorder(IR::P4Table *) override;
139 IR::P4Table *postorder(IR::P4Table *) override;
140 const IR::P4Parser *postorder(IR::P4Parser *) override;
141 IR::ParserState *preorder(IR::ParserState *) override;
142 IR::ParserState *postorder(IR::ParserState *) override;
143 Visitor::profile_t init_apply(const IR::Node *node) override;
144 class ElimDead;
145 class RewriteTableKeys;
146
148
149 public:
151 std::function<bool(const Context *, const IR::Expression *)> policy,
152 bool eut)
153 : refMap(refMap),
154 typeMap(typeMap),
155 tables(*new std::map<cstring, TableInfo>),
156 actions(*new std::map<cstring, FuncInfo>),
157 methods(*new std::map<cstring, FuncInfo>),
158 states(*new std::map<cstring, FuncInfo>),
159 policy(policy),
160 elimUnusedTables(eut) {}
161};
162
164 public:
166 ReferenceMap *refMap, TypeMap *typeMap, TypeChecking *typeChecking = nullptr,
167 std::function<bool(const Context *, const IR::Expression *)> policy =
168 [](const Context *, const IR::Expression *) -> bool { return true; },
169 bool elimUnusedTables = false) {
170 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap, true);
171 passes.push_back(typeChecking);
172 passes.push_back(new DoLocalCopyPropagation(refMap, typeMap, policy, elimUnusedTables));
173 }
174 LocalCopyPropagation(ReferenceMap *refMap, TypeMap *typeMap,
175 std::function<bool(const Context *, const IR::Expression *)> policy)
176 : LocalCopyPropagation(refMap, typeMap, nullptr, policy) {}
177};
178
179} // namespace P4
180
181#endif /* MIDEND_LOCAL_COPYPROP_H_ */
Definition visitor.h:463
Definition local_copyprop.cpp:69
Definition local_copyprop.cpp:142
Definition local_copyprop.h:52
Definition node.h:95
Definition visitor.h:400
Definition local_copyprop.h:163
Definition visitor.h:788
Definition 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 source_file.h:124
Definition visitor.h:78
Definition visitor.h:75
Definition cstring.h:85
Definition has_side_effects.h:28
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition visitor.h:47