forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplifyKey.h
184 lines (162 loc) · 5.68 KB
/
simplifyKey.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Copyright 2016 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _MIDEND_SIMPLIFYKEY_H_
#define _MIDEND_SIMPLIFYKEY_H_
#include "ir/ir.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/typeChecking/typeChecker.h"
#include "frontends/p4/sideEffects.h"
namespace P4 {
/**
* Policy used to decide whether a key expression is simple enough
* to be implemented directly.
*/
class KeyIsSimple {
public:
virtual ~KeyIsSimple() {}
virtual bool isSimple(const IR::Expression* expression, const Visitor::Context*) = 0;
};
/**
* Policy that treats a key as simple if it contains just
* PathExpression, member and ArrayIndex operations.
*/
class IsLikeLeftValue : public KeyIsSimple, public Inspector {
protected:
bool simple;
public:
IsLikeLeftValue()
{ setName("IsLikeLeftValue"); }
void postorder(const IR::Expression*) override {
// all other expressions are complicated
simple = false;
}
void postorder(const IR::Member*) override {}
void postorder(const IR::PathExpression*) override {}
void postorder(const IR::ArrayIndex*) override {}
profile_t init_apply(const IR::Node* root) override {
simple = true;
return Inspector::init_apply(root); }
bool isSimple(const IR::Expression* expression, const Visitor::Context*) override {
(void)expression->apply(*this);
return simple;
}
};
/**
* Policy that treats a key as simple if it a call to isValid().
*/
class IsValid : public KeyIsSimple {
ReferenceMap* refMap;
TypeMap* typeMap;
public:
IsValid(ReferenceMap* refMap, TypeMap* typeMap) : refMap(refMap), typeMap(typeMap)
{ CHECK_NULL(refMap); CHECK_NULL(typeMap); }
bool isSimple(const IR::Expression* expression, const Visitor::Context*);
};
/**
* Policy that treats a key as simple if it is a mask applied to an lvalue
* or just an lvalue.
*/
class IsMask : public IsLikeLeftValue {
public:
bool isSimple(const IR::Expression* expression, const Visitor::Context* ctxt) {
if (auto mask = expression->to<IR::BAnd>()) {
if (mask->right->is<IR::Constant>())
expression = mask->left;
else if (mask->left->is<IR::Constant>())
expression = mask->right; }
return IsLikeLeftValue::isSimple(expression, ctxt); }
};
/**
A KeyIsSimple policy formed by combining two
other policies with a logical 'or'.
*/
class OrPolicy : public KeyIsSimple {
KeyIsSimple* left;
KeyIsSimple* right;
public:
OrPolicy(KeyIsSimple* left, KeyIsSimple* right) : left(left), right(right)
{ CHECK_NULL(left); CHECK_NULL(right); }
bool isSimple(const IR::Expression* expression, const Visitor::Context* ctxt) {
return left->isSimple(expression, ctxt) || right->isSimple(expression, ctxt);
}
};
/**
* Transform complex table keys into simpler expressions.
*
* \code{.cpp}
* table t {
* key = { h.a + 1 : exact; }
* ...
* }
* apply {
* t.apply();
* }
* \endcode
*
* is transformed to
*
* \code{.cpp}
* bit<32> key_0;
* table t {
* key = { key_0 : exact; }
* ...
* }
* apply {
* key_0 = h.a + 1;
* t.apply();
* }
* \endcode
*
* @pre none
* @post all complex table key expressions are replaced with a simpler expression.
*/
class DoSimplifyKey : public Transform {
ReferenceMap* refMap;
TypeMap* typeMap;
KeyIsSimple* key_policy;
std::map<const IR::P4Table*, TableInsertions*> toInsert;
public:
DoSimplifyKey(ReferenceMap* refMap, TypeMap* typeMap, KeyIsSimple* key_policy)
: refMap(refMap), typeMap(typeMap), key_policy(key_policy)
{ CHECK_NULL(refMap); CHECK_NULL(typeMap); CHECK_NULL(key_policy); setName("DoSimplifyKey"); }
const IR::Node* doStatement(const IR::Statement* statement, const IR::Expression* expression);
// These should be all kinds of statements that may contain a table apply
// after the program has been simplified
const IR::Node* postorder(IR::MethodCallStatement* statement) override
{ return doStatement(statement, statement->methodCall); }
const IR::Node* postorder(IR::IfStatement* statement) override
{ return doStatement(statement, statement->condition); }
const IR::Node* postorder(IR::SwitchStatement* statement) override
{ return doStatement(statement, statement->expression); }
const IR::Node* postorder(IR::AssignmentStatement* statement) override
{ return doStatement(statement, statement->right); }
const IR::Node* postorder(IR::KeyElement* element) override;
const IR::Node* postorder(IR::P4Table* table) override;
};
/**
* This pass uses 'policy' to determine whether a key expression is too complex
* and simplifies keys which are complex according to the policy.
*/
class SimplifyKey : public PassManager {
public:
SimplifyKey(ReferenceMap* refMap, TypeMap* typeMap, KeyIsSimple* key_policy,
TypeChecking* typeChecking = nullptr) {
if (!typeChecking)
typeChecking = new TypeChecking(refMap, typeMap);
passes.push_back(typeChecking);
passes.push_back(new DoSimplifyKey(refMap, typeMap, key_policy));
setName("SimplifyKey");
}
};
} // namespace P4
#endif /* _MIDEND_SIMPLIFYKEY_H_ */