P4C
The P4 Compiler
Loading...
Searching...
No Matches
simplify_key_policy.h
1
19#ifndef BF_P4C_MIDEND_SIMPLIFY_KEY_POLICY_H_
20#define BF_P4C_MIDEND_SIMPLIFY_KEY_POLICY_H_
21
22#include "midend/simplifyKey.h"
23
24namespace BFN {
25
26class IsPhase0 : public P4::KeyIsSimple {
27 public:
28 IsPhase0() {}
29
30 bool isSimple(const IR::Expression *, const Visitor::Context *ctxt) override {
31 while (true) {
32 if (ctxt == nullptr) return false;
33 auto *n = ctxt->node;
34 if (n->is<IR::P4Program>()) return false;
35 if (auto table = n->to<IR::P4Table>()) {
36 return table->hasAnnotation("phase0"_cs);
37 }
38 ctxt = ctxt->parent;
39 }
40 return false;
41 }
42};
43
44class IsSlice : public P4::KeyIsSimple {
45 public:
46 IsSlice() {}
47
48 bool isSimple(const IR::Expression *expr, const Visitor::Context *) override {
49 auto slice = expr->to<IR::Slice>();
50 if (!slice) return false;
51 auto *e = slice->e0;
52 while (e->is<IR::Member>()) e = e->to<IR::Member>()->expr;
53 return e->to<IR::PathExpression>() != nullptr;
54 }
55};
56
57// Extend P4::IsMask to handle Slices
58// E.g. hdr.ethernet.smac[15:0] & 0x1f1f
59// This is a valid key which contains a field slice and a mask
60// Frontend IsMask only handles non sliced fields on masks.
61class IsSliceMask : public P4::IsMask {
62 void postorder(const IR::Slice *) override {}
63 void postorder(const IR::Constant *) override {
64 // Only skip constants under a Slice
65 if (!getParent<IR::Slice>()) simple = false;
66 }
67
68 public:
69 IsSliceMask() { setName("IsSlicMask"); }
70
71 bool isSimple(const IR::Expression *expr, const Visitor::Context *ctxt) override {
72 return P4::IsMask::isSimple(expr, ctxt);
73 }
74};
75
77 public:
78 static P4::KeyIsSimple *getPolicy(P4::TypeMap &typeMap) {
79 return new P4::OrPolicy(
80 new P4::OrPolicy(new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask()),
81 new BFN::IsPhase0()),
83 }
84};
85
86} // namespace BFN
87
88#endif /* BF_P4C_MIDEND_SIMPLIFY_KEY_POLICY_H_ */
Definition simplify_key_policy.h:26
Definition simplify_key_policy.h:44
Definition simplify_key_policy.h:61
Definition simplifyKey.h:81
Definition simplifyKey.h:69
Definition simplifyKey.h:31
Definition simplifyKey.h:98
Definition typeMap.h:41
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
Definition visitor.h:47