P4C
The P4 Compiler
Loading...
Searching...
No Matches
elim_cast.h
1
45#ifndef BF_P4C_MIDEND_ELIM_CAST_H_
46#define BF_P4C_MIDEND_ELIM_CAST_H_
47
48#include "bf-p4c/midend/type_checker.h"
49#include "frontends/p4/simplify.h"
50#include "frontends/p4/strengthReduction.h"
51#include "frontends/p4/typeMap.h"
52#include "ir/ir.h"
53
54namespace BFN {
55
64 public:
65 const IR::Node *preorder(IR::KeyElement *n) final {
66 prune();
67 return n;
68 }
69};
70
75 public:
76 static constexpr int MAX_CONTAINER_SIZE = 32;
77
79 const IR::Node *preorder(IR::Cast *cast) override;
80
81#if 0
82 // Caveat(hanw): do not simplify cast in apply functions.
83 // stateful_alu implementation must handle '++'
84 // before we can eliminate cast.
85 const IR::Node* preorder(IR::Function* func) override {
86 prune();
87 return func; }
88#endif
89
90 // Caveat(hanw): we do not simplify the cast in a selection expression,
91 // because we do not support '++' operator with a constant lhs.
92 // Further, simplifying the selection expression has impact on
93 // pvs control plan API generation that needs to be discussed with
94 // Steve and Yuan.
95 const IR::Node *preorder(IR::SelectExpression *expr) override {
96 prune();
97 return expr;
98 }
99};
100
115 P4::TypeMap *typeMap;
116
117 public:
118 explicit RewriteCastToReinterpretCast(P4::TypeMap *typeMap) : typeMap(typeMap) {}
119 const IR::Node *preorder(IR::Cast *expression) override;
120};
121
136 public:
138 const IR::Node *preorder(IR::Cast *expression) override;
139};
140
157 public:
159 const IR::Node *preorder(IR::Cast *expression) override;
160};
161
174 public:
176 const IR::Node *preorder(IR::Cast *expression) override;
177};
178
185 public:
186 // do not simplify '++' in apply functions.
187 const IR::Node *preorder(IR::Function *func) override {
188 prune();
189 return func;
190 }
191
192 // do not simplify '++' in deparser block.
193 const IR::Node *preorder(IR::BFN::TnaDeparser *dprsr) override {
194 prune();
195 return dprsr;
196 }
197
198 const IR::BlockStatement *preorder(IR::BlockStatement *blk) override {
199 if (blk->getAnnotation("in_hash"_cs)) prune();
200 return blk;
201 }
202 const IR::Node *preorder(IR::AssignmentStatement *stmt) override;
203 const IR::Node *postorder(IR::AssignmentStatement *stmt) override;
204 const IR::Node *preorder(IR::IfStatement *stmt) override;
205 const IR::Node *preorder(IR::Equ *eq) override;
206 const IR::Node *preorder(IR::Neq *ne) override;
207};
208
213 public:
215 passes.push_back(new BFN::TypeChecking(refMap, typeMap, true));
216 passes.push_back(new P4::DoStrengthReduction());
217 }
218};
219
225class ElimCasts : public PassManager {
226 public:
227 ElimCasts(P4::ReferenceMap *refMap, P4::TypeMap *typeMap) {
228 addPasses({
231 new RewriteCastToReinterpretCast(typeMap),
233 // EliminateWidthCasts might change some of the Type_Bits
234 // to different objects representing the same type =>
235 // rerun typechecking with empty typemap to properly
236 // unify those new types
237 new P4::ClearTypeMap(typeMap),
238 new BFN::TypeChecking(refMap, typeMap, true),
239 // FIXME -- StrengthReduction has problems with complex nested things like
240 // (0 ++ field << 1)[15:0] which causes problems for Rewrite
241 // so we repeat until a fixed point is reached.
242 new PassRepeated({new StrengthReduction(refMap, typeMap)}),
244 new P4::ClearTypeMap(typeMap),
245 new BFN::TypeChecking(refMap, typeMap, true),
246 new P4::SimplifyControlFlow(typeMap),
247 new P4::ClearTypeMap(typeMap),
248 new BFN::TypeChecking(refMap, typeMap, true),
249 });
250 }
251};
252
253} // namespace BFN
254
255#endif /* BF_P4C_MIDEND_ELIM_CAST_H_ */
Definition typeChecker.h:32
Definition strengthReduction.h:47
Definition node.h:95
Definition ir/pass_manager.h:40
Definition ir/pass_manager.h:145
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition simplify.h:70
Definition strengthReduction.h:119
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Top level PassManager that simplifies complex expression with multiple casts into simpler expression ...
Definition elim_cast.h:225
Definition elim_cast.h:74
Auxiliary transformer to avoid processing IR::KeyElement nodes.
Definition elim_cast.h:63
Pass that converts some of the IR::Casts to ReinterpretCasts.
Definition elim_cast.h:114
Pass that replaces concat ++ operations with multiple operations on slices in the contexts.
Definition elim_cast.h:184
Pass that removes nested casts when applicable.
Definition elim_cast.h:156
Pass that moves the cast on binary operation to each operand.
Definition elim_cast.h:173
Pass that removes some of the redundant casts.
Definition elim_cast.h:135
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21