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
52class DoLocalCopyPropagation : public ControlFlowVisitor, Transform, P4WriteContext {
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
87 DoLocalCopyPropagation *clone() const override { return new DoLocalCopyPropagation(*this); }
88 void flow_merge(Visitor &) override;
89 void flow_copy(ControlFlowVisitor &) override;
90 bool name_overlap(cstring, cstring);
91 void forOverlapAvail(cstring, std::function<void(cstring, VarInfo *)>);
92 void dropValuesUsing(cstring);
93 bool hasSideEffects(const IR::Expression *e) {
94 return bool(::hasSideEffects(refMap, typeMap, e));
95 }
96 bool isHeaderUnionIsValid(const IR::Expression *e);
97
98 void visit_local_decl(const IR::Declaration_Variable *);
99 const IR::Node *postorder(IR::Declaration_Variable *) override;
100 IR::Expression *preorder(IR::Expression *m) override;
101 const IR::Expression *copyprop_name(cstring name, const Util::SourceInfo &srcInfo);
102 const IR::Expression *postorder(IR::PathExpression *) override;
103 const IR::Expression *preorder(IR::Member *) override;
104 const IR::Expression *preorder(IR::ArrayIndex *) override;
105 IR::Statement *preorder(IR::Statement *) override;
106 IR::AssignmentStatement *preorder(IR::AssignmentStatement *) override;
107 IR::AssignmentStatement *postorder(IR::AssignmentStatement *) override;
108 IR::IfStatement *postorder(IR::IfStatement *) override;
109 IR::MethodCallExpression *postorder(IR::MethodCallExpression *) override;
110 IR::P4Action *preorder(IR::P4Action *) override;
111 IR::P4Action *postorder(IR::P4Action *) override;
112 IR::Function *preorder(IR::Function *) override;
113 IR::Function *postorder(IR::Function *) override;
114 IR::P4Control *preorder(IR::P4Control *) override;
115 void apply_table(TableInfo *tbl);
116 void apply_function(FuncInfo *tbl);
117 IR::P4Table *preorder(IR::P4Table *) override;
118 IR::P4Table *postorder(IR::P4Table *) override;
119 const IR::P4Parser *postorder(IR::P4Parser *) override;
120 IR::ParserState *preorder(IR::ParserState *) override;
121 IR::ParserState *postorder(IR::ParserState *) override;
122 Visitor::profile_t init_apply(const IR::Node *node) override;
123 class ElimDead;
124 class RewriteTableKeys;
125
127
128 public:
130 std::function<bool(const Context *, const IR::Expression *)> policy,
131 bool eut)
132 : refMap(refMap),
133 typeMap(typeMap),
134 tables(*new std::map<cstring, TableInfo>),
135 actions(*new std::map<cstring, FuncInfo>),
136 methods(*new std::map<cstring, FuncInfo>),
137 states(*new std::map<cstring, FuncInfo>),
138 policy(policy),
139 elimUnusedTables(eut) {}
140};
141
142class LocalCopyPropagation : public PassManager {
143 public:
145 ReferenceMap *refMap, TypeMap *typeMap, TypeChecking *typeChecking = nullptr,
146 std::function<bool(const Context *, const IR::Expression *)> policy =
147 [](const Context *, const IR::Expression *) -> bool { return true; },
148 bool elimUnusedTables = false) {
149 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap, true);
150 passes.push_back(typeChecking);
151 passes.push_back(new DoLocalCopyPropagation(refMap, typeMap, policy, elimUnusedTables));
152 }
153 LocalCopyPropagation(ReferenceMap *refMap, TypeMap *typeMap,
154 std::function<bool(const Context *, const IR::Expression *)> policy)
155 : LocalCopyPropagation(refMap, typeMap, nullptr, policy) {}
156};
157
158} // namespace P4
159
160#endif /* MIDEND_LOCAL_COPYPROP_H_ */
Definition local_copyprop.cpp:65
Definition local_copyprop.cpp:138
Definition local_copyprop.h:52
Definition local_copyprop.h:142
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeChecker.h:60
Definition typeMap.h:42
Definition source_file.h:126
Definition cstring.h:72
Definition has_side_effects.h:26
Definition applyOptionsPragmas.cpp:24