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 auto annot = table->getAnnotations();
37 if (annot->getSingle("phase0"_cs)) {
38 return true;
39 }
40 return false;
41 }
42 ctxt = ctxt->parent;
43 }
44 return false;
45 }
46};
47
48class IsSlice : public P4::KeyIsSimple {
49 public:
50 IsSlice() {}
51
52 bool isSimple(const IR::Expression *expr, const Visitor::Context *) override {
53 auto slice = expr->to<IR::Slice>();
54 if (!slice) return false;
55 auto *e = slice->e0;
56 while (e->is<IR::Member>()) e = e->to<IR::Member>()->expr;
57 return e->to<IR::PathExpression>() != nullptr;
58 }
59};
60
61// Extend P4::IsMask to handle Slices
62// E.g. hdr.ethernet.smac[15:0] & 0x1f1f
63// This is a valid key which contains a field slice and a mask
64// Frontend IsMask only handles non sliced fields on masks.
65class IsSliceMask : public P4::IsMask {
66 void postorder(const IR::Slice *) override {}
67 void postorder(const IR::Constant *) override {
68 // Only skip constants under a Slice
69 if (!getParent<IR::Slice>()) simple = false;
70 }
71
72 public:
73 IsSliceMask() { setName("IsSlicMask"); }
74
75 bool isSimple(const IR::Expression *expr, const Visitor::Context *ctxt) override {
76 return P4::IsMask::isSimple(expr, ctxt);
77 }
78};
79
81 public:
82 static P4::KeyIsSimple *getPolicy(P4::TypeMap &typeMap) {
83 return new P4::OrPolicy(
84 new P4::OrPolicy(new P4::OrPolicy(new P4::IsValid(&typeMap), new P4::IsMask()),
85 new BFN::IsPhase0()),
87 }
88};
89
90} // namespace BFN
91
92#endif /* BF_P4C_MIDEND_SIMPLIFY_KEY_POLICY_H_ */
Definition simplify_key_policy.h:26
Definition simplify_key_policy.h:48
Definition simplify_key_policy.h:65
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