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