P4C
The P4 Compiler
Loading...
Searching...
No Matches
flattenUnions.h
1/*
2Copyright 2022 Intel Corp.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef MIDEND_FLATTENUNIONS_H_
18#define MIDEND_FLATTENUNIONS_H_
19
20#include "./frontends/p4/parserControlFlow.h"
21#include "./frontends/p4/simplifyDefUse.h"
22#include "./frontends/p4/unusedDeclarations.h"
23#include "frontends/p4/typeChecking/typeChecker.h"
24#include "ir/ir.h"
25namespace P4 {
26
49 protected:
50 P4::ReferenceMap *refMap;
51 P4::TypeMap *typeMap;
52 std::map<cstring, std::map<cstring, cstring>> replacementMap;
53 // Replacement map needed to add element-wise header declaration in right context
54 std::map<IR::Declaration_Variable *, IR::IndexedVector<IR::Declaration>> replaceDVMap;
55
56 public:
58 : refMap(refMap), typeMap(typeMap) {}
59 const IR::Node *postorder(IR::Type_Struct *sf) override;
60 const IR::Node *postorder(IR::Declaration_Variable *dv) override;
61 const IR::Node *postorder(IR::Member *m) override;
62 const IR::Node *postorder(IR::P4Parser *parser) override;
63 const IR::Node *postorder(IR::P4Control *control) override;
64 const IR::Node *postorder(IR::P4Action *action) override;
65 bool hasHeaderUnionField(IR::Type_Struct *s);
66};
67
95 std::map<cstring, std::vector<cstring>> stackMap;
96
97 public:
99 : DoFlattenHeaderUnion(refMap, typeMap) {
100 setName("DoFlattenHeaderUnionStack");
101 }
102 const IR::Node *postorder(IR::Type_Struct *sf) override;
103 const IR::Node *postorder(IR::ArrayIndex *e) override;
104 const IR::Node *postorder(IR::Declaration_Variable *dv) override;
105 bool hasHeaderUnionStackField(IR::Type_Struct *s);
106};
107
141 P4::ReferenceMap *refMap;
142 P4::TypeMap *typeMap;
143 IR::IndexedVector<IR::Declaration> toInsert; // temporaries
144
145 public:
147 : refMap(refMap), typeMap(typeMap) {
148 setName("HandleValidityHeaderUnion");
149 }
150 const IR::Node *postorder(IR::AssignmentStatement *assn) override;
151 const IR::Node *postorder(IR::IfStatement *a) override;
152 const IR::Node *postorder(IR::SwitchStatement *a) override;
153 const IR::Node *postorder(IR::MethodCallStatement *mcs) override;
154 const IR::Node *postorder(IR::P4Parser *parser) override;
155 const IR::Node *postorder(IR::P4Control *control) override;
156 const IR::Node *postorder(IR::P4Action *action) override;
157 const IR::MethodCallStatement *processValidityForStr(const IR::Statement *s,
158 const IR::Member *m, cstring headerElement,
159 cstring setValid);
160 const IR::Node *setInvalidforRest(const IR::Statement *s, const IR::Member *m,
161 const IR::Type_HeaderUnion *hu, cstring exclude,
162 bool setValidforCurrMem);
163 const IR::Node *expandIsValid(const IR::Statement *a, const IR::MethodCallExpression *mce,
165};
166
168 const UsedDeclSet &used;
169
170 public:
171 explicit RemoveUnusedHUDeclarations(const UsedDeclSet &used) : used(used) {}
172
173 const IR::Node *preorder(IR::Type_HeaderUnion *type) override {
174 if (!used.isUsed(getOriginal<IR::IDeclaration>())) return nullptr;
175 return type;
176 }
177};
178
180 UsedDeclSet used;
181
182 public:
184 : PassManager({
185 new CollectUsedDeclarations(used),
187 }) {
188 setName("RemoveAllUnusedHUDDeclarations");
189 setStopOnError(true);
190 }
191};
192
200 public:
201 FlattenHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, bool loopsUnroll = true) {
202 passes.push_back(new P4::TypeChecking(refMap, typeMap));
203 passes.push_back(new HandleValidityHeaderUnion(refMap, typeMap));
204 // Stack flattening is only applicable if parser loops are unrolled and
205 // header union stack elements are accessed using [] notation. This pass does not handle
206 // .next .last etc accessors for stack elements.
207 if (loopsUnroll) {
208 passes.push_back(new DoFlattenHeaderUnionStack(refMap, typeMap));
210 }
211 passes.push_back(new P4::ClearTypeMap(typeMap));
212 passes.push_back(new P4::TypeInference(typeMap, false));
213 passes.push_back(new P4::TypeChecking(refMap, typeMap));
214 passes.push_back(new DoFlattenHeaderUnion(refMap, typeMap));
216 passes.push_back(new P4::RemoveAllUnusedHUDDeclarations());
217 passes.push_back(new P4::ClearTypeMap(typeMap));
218 passes.push_back(new P4::TypeChecking(nullptr, typeMap));
219 passes.push_back(new P4::RemoveParserIfs(typeMap));
220 }
221};
222} // namespace P4
223
224#endif /* MIDEND_FLATTENUNIONS_H_ */
Definition typeChecker.h:32
Collects all used declarations into @used set.
Definition unusedDeclarations.h:57
Definition flattenUnions.h:48
Definition flattenUnions.h:94
Definition flattenUnions.h:199
Definition flattenUnions.h:140
Definition node.h:52
Definition node.h:94
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:189
Definition flattenUnions.h:179
Definition parserControlFlow.h:114
Definition flattenUnions.h:167
Definition unusedDeclarations.h:48
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeChecker.h:483
Definition typeMap.h:41
Definition unusedDeclarations.h:29
bool isUsed(const IR::IDeclaration *decl) const
Definition unusedDeclarations.h:45
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24