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 P4::ReferenceMap *refMap;
169
170 public:
171 explicit RemoveUnusedHUDeclarations(P4::ReferenceMap *refMap) : refMap(refMap) {}
172 const IR::Node *preorder(IR::Type_HeaderUnion *type) {
173 if (!refMap->isUsed(getOriginal<IR::IDeclaration>())) {
174 return nullptr;
175 }
176 return type;
177 }
178};
179
187 public:
188 FlattenHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, bool loopsUnroll = true) {
189 passes.push_back(new P4::TypeChecking(refMap, typeMap));
190 passes.push_back(new HandleValidityHeaderUnion(refMap, typeMap));
191 // Stack flattening is only applicable if parser loops are unrolled and
192 // header union stack elements are accessed using [] notation. This pass does not handle
193 // .next .last etc accessors for stack elements.
194 if (loopsUnroll) {
195 passes.push_back(new DoFlattenHeaderUnionStack(refMap, typeMap));
196 passes.push_back(new P4::ClearTypeMap(typeMap));
197 passes.push_back(new P4::ResolveReferences(refMap));
198 passes.push_back(new P4::TypeInference(typeMap, false));
199 passes.push_back(new P4::TypeChecking(refMap, typeMap));
200 passes.push_back(new P4::RemoveAllUnusedDeclarations(refMap, RemoveUnusedPolicy()));
201 }
202 passes.push_back(new DoFlattenHeaderUnion(refMap, typeMap));
203 passes.push_back(new P4::ClearTypeMap(typeMap));
204 passes.push_back(new P4::TypeChecking(refMap, typeMap));
205 passes.push_back(new P4::RemoveAllUnusedDeclarations(refMap, RemoveUnusedPolicy()));
206 passes.push_back(new P4::RemoveUnusedHUDeclarations(refMap));
207 passes.push_back(new P4::RemoveParserIfs(typeMap));
208 }
209};
210} // namespace P4
211
212#endif /* MIDEND_FLATTENUNIONS_H_ */
Definition typeChecker.h:32
Definition flattenUnions.h:48
Definition flattenUnions.h:94
Definition flattenUnions.h:186
Definition flattenUnions.h:140
Definition node.h:52
Definition node.h:95
Definition pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
bool isUsed(const IR::IDeclaration *decl) const
Definition referenceMap.h:118
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:146
Definition parserControlFlow.h:115
Definition flattenUnions.h:167
Definition unusedDeclarations.h:28
Definition resolveReferences.h:121
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeChecker.h:481
Definition typeMap.h:41
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24