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/resolveReferences.h"
20#include "frontends/p4/typeChecking/typeChecker.h"
21#include "has_side_effects.h"
22#include "ir/ir.h"
23#include "ir/visitor.h"
24
25namespace P4 {
26
57 TypeMap *typeMap;
58 bool working = false;
59 struct VarInfo {
60 bool local = false;
61 bool live = false;
62 const IR::Expression *val = nullptr;
63 };
64 struct TableInfo {
65 std::set<cstring> keyreads, actions;
66 int apply_count = 0;
67 std::map<cstring, const IR::Expression *> key_remap;
68 };
69 struct FuncInfo {
70 std::set<cstring> reads, writes;
71 int apply_count = 0;
72
77 bool is_first_write_insert = false;
78 };
79 std::map<cstring, VarInfo> available;
80 std::map<cstring, TableInfo> &tables;
81 std::map<cstring, FuncInfo> &actions;
82 std::map<cstring, FuncInfo> &methods;
83 std::map<cstring, FuncInfo> &states;
84 TableInfo *inferForTable = nullptr;
85 FuncInfo *inferForFunc = nullptr;
86 bool need_key_rewrite = false;
87 std::function<bool(const Context *, const IR::Expression *)> policy;
88 bool elimUnusedTables = false;
89 int uid = -1;
90 static int uid_ctr;
91
92 DoLocalCopyPropagation *clone() const override {
93 auto *rv = new DoLocalCopyPropagation(*this);
94 rv->uid = ++uid_ctr;
95 LOG8("flow_clone(" << uid << ") = " << rv->uid);
96 return rv;
97 }
98 void flow_merge(Visitor &) override;
99 void flow_copy(ControlFlowVisitor &) override;
100 bool operator==(const ControlFlowVisitor &) const override;
101 bool name_overlap(cstring, cstring);
102 void forOverlapAvail(cstring, std::function<void(cstring, VarInfo *)>);
103 void dropValuesUsing(cstring);
104 bool hasSideEffects(const IR::Expression *e, const Visitor::Context *ctxt) {
105 return bool(::P4::hasSideEffects(typeMap, e, ctxt));
106 }
107 bool isHeaderUnionIsValid(const IR::Expression *e);
108
109 class LoopPrepass : public Inspector {
111 void postorder(const IR::AssignmentStatement *) override;
112 void postorder(const IR::MethodCallExpression *) override;
113 void apply_table(TableInfo *tbl);
114 void apply_function(FuncInfo *tbl);
115
116 public:
117 explicit LoopPrepass(DoLocalCopyPropagation &s) : self(s) {}
118 };
119
120 void visit_local_decl(const IR::Declaration_Variable *);
121 const IR::Node *postorder(IR::Declaration_Variable *) override;
122 IR::Expression *preorder(IR::Expression *m) override;
123 const IR::Expression *copyprop_name(cstring name, const Util::SourceInfo &srcInfo);
124 const IR::Expression *postorder(IR::PathExpression *) override;
125 const IR::Expression *preorder(IR::Member *) override;
126 const IR::Expression *preorder(IR::ArrayIndex *) override;
127 IR::Statement *preorder(IR::Statement *) override;
128 IR::AssignmentStatement *preorder(IR::AssignmentStatement *) override;
129 IR::AssignmentStatement *postorder(IR::AssignmentStatement *) override;
130 IR::IfStatement *postorder(IR::IfStatement *) override;
131 IR::ForStatement *preorder(IR::ForStatement *) override;
132 IR::ForInStatement *preorder(IR::ForInStatement *) override;
133 IR::MethodCallExpression *postorder(IR::MethodCallExpression *) override;
134 IR::P4Action *preorder(IR::P4Action *) override;
135 IR::P4Action *postorder(IR::P4Action *) override;
136 IR::Function *preorder(IR::Function *) override;
137 IR::Function *postorder(IR::Function *) override;
138 IR::P4Control *preorder(IR::P4Control *) override;
139 void apply_table(TableInfo *tbl);
140 void apply_function(FuncInfo *tbl);
141 IR::P4Table *preorder(IR::P4Table *) override;
142 IR::P4Table *postorder(IR::P4Table *) override;
143 const IR::P4Parser *postorder(IR::P4Parser *) override;
144 IR::ParserState *preorder(IR::ParserState *) override;
145 IR::ParserState *postorder(IR::ParserState *) override;
146 Visitor::profile_t init_apply(const IR::Node *node) override;
147 class ElimDead;
148 class RewriteTableKeys;
149
151
152 public:
154 std::function<bool(const Context *, const IR::Expression *)> policy,
155 bool eut)
156 : typeMap(typeMap),
157 tables(*new std::map<cstring, TableInfo>),
158 actions(*new std::map<cstring, FuncInfo>),
159 methods(*new std::map<cstring, FuncInfo>),
160 states(*new std::map<cstring, FuncInfo>),
161 policy(policy),
162 elimUnusedTables(eut) {}
163};
164
166 public:
168 TypeMap *typeMap, TypeChecking *typeChecking = nullptr,
169 std::function<bool(const Context *, const IR::Expression *)> policy =
170 [](const Context *, const IR::Expression *) -> bool { return true; },
171 bool elimUnusedTables = false) {
172 if (!typeChecking) typeChecking = new TypeChecking(nullptr, typeMap, true);
173 passes.push_back(typeChecking);
174 passes.push_back(new DoLocalCopyPropagation(typeMap, policy, elimUnusedTables));
175 }
177 std::function<bool(const Context *, const IR::Expression *)> policy)
178 : LocalCopyPropagation(typeMap, nullptr, policy) {}
179};
180
181} // namespace P4
182
183#endif /* MIDEND_LOCAL_COPYPROP_H_ */
Definition visitor.h:463
Definition local_copyprop.cpp:69
Definition local_copyprop.cpp:143
Definition local_copyprop.h:56
Definition node.h:94
Definition visitor.h:400
Definition local_copyprop.h:165
Definition visitor.h:788
Definition ir/pass_manager.h:40
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition source_file.h:131
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